replayContext.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. import {createContext, useCallback, useContext, useEffect, useRef, useState} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import {Replayer, ReplayerEvents} from '@sentry-internal/rrweb';
  4. import ConfigStore from 'sentry/stores/configStore';
  5. import {useLegacyStore} from 'sentry/stores/useLegacyStore';
  6. import {trackAnalytics} from 'sentry/utils/analytics';
  7. import localStorage from 'sentry/utils/localStorage';
  8. import {
  9. clearAllHighlights,
  10. highlightNode,
  11. removeHighlightedNode,
  12. } from 'sentry/utils/replays/highlightNode';
  13. import type useInitialOffsetMs from 'sentry/utils/replays/hooks/useInitialTimeOffsetMs';
  14. import useRAF from 'sentry/utils/replays/hooks/useRAF';
  15. import type ReplayReader from 'sentry/utils/replays/replayReader';
  16. import useOrganization from 'sentry/utils/useOrganization';
  17. import usePrevious from 'sentry/utils/usePrevious';
  18. enum ReplayLocalstorageKeys {
  19. REPLAY_CONFIG = 'replay-config',
  20. }
  21. type ReplayConfig = {
  22. skip?: boolean;
  23. speed?: number;
  24. };
  25. type Dimensions = {height: number; width: number};
  26. type RootElem = null | HTMLDivElement;
  27. // See also: Highlight in static/app/views/replays/types.tsx
  28. type HighlightParams = {
  29. nodeId: number;
  30. annotation?: string;
  31. spotlight?: boolean;
  32. };
  33. // Important: Don't allow context Consumers to access `Replayer` directly.
  34. // It has state that, when changed, will not trigger a react render.
  35. // Instead only expose methods that wrap `Replayer` and manage state.
  36. type ReplayPlayerContextProps = {
  37. /**
  38. * Clear all existing highlights in replay
  39. */
  40. clearAllHighlights: () => void;
  41. /**
  42. * The time, in milliseconds, where the user focus is.
  43. * The user focus can be reported by any collaborating object, usually on
  44. * hover.
  45. */
  46. currentHoverTime: undefined | number;
  47. /**
  48. * The current time of the video, in milliseconds
  49. * The value is updated on every animation frame, about every 16.6ms
  50. */
  51. currentTime: number;
  52. /**
  53. * Original dimensions in pixels of the captured browser window
  54. */
  55. dimensions: Dimensions;
  56. /**
  57. * The calculated speed of the player when fast-forwarding through idle moments in the video
  58. * The value is set to `0` when the video is not fast-forwarding
  59. * The speed is automatically determined by the length of each idle period
  60. */
  61. fastForwardSpeed: number;
  62. /**
  63. * Highlight a node in the replay
  64. */
  65. highlight: (args: HighlightParams) => void;
  66. /**
  67. * Required to be called with a <div> Ref
  68. * Represents the location in the DOM where the iframe video should be mounted
  69. *
  70. * @param _root
  71. */
  72. initRoot: (root: RootElem) => void;
  73. /**
  74. * Set to true while the library is reconstructing the DOM
  75. */
  76. isBuffering: boolean;
  77. /**
  78. * Is the data inside the `replay` complete, or are we waiting for more.
  79. */
  80. isFetching;
  81. /**
  82. * Set to true when the replay finish event is fired
  83. */
  84. isFinished: boolean;
  85. /**
  86. * Whether the video is currently playing
  87. */
  88. isPlaying: boolean;
  89. /**
  90. * Whether fast-forward mode is enabled if RRWeb detects idle moments in the video
  91. */
  92. isSkippingInactive: boolean;
  93. /**
  94. * Removes a highlighted node from the replay
  95. */
  96. removeHighlight: ({nodeId}: {nodeId: number}) => void;
  97. /**
  98. * The core replay data
  99. */
  100. replay: ReplayReader | null;
  101. /**
  102. * Restart the replay
  103. */
  104. restart: () => void;
  105. /**
  106. * Set the currentHoverTime so collaborating components can highlight related
  107. * information
  108. */
  109. setCurrentHoverTime: (time: undefined | number) => void;
  110. /**
  111. * Jump the video to a specific time
  112. */
  113. setCurrentTime: (time: number) => void;
  114. /**
  115. * Set speed for normal playback
  116. */
  117. setSpeed: (speed: number) => void;
  118. /**
  119. * The speed for normal playback
  120. */
  121. speed: number;
  122. /**
  123. * Start or stop playback
  124. *
  125. * @param play
  126. */
  127. togglePlayPause: (play: boolean) => void;
  128. /**
  129. * Allow RRWeb to use Fast-Forward mode for idle moments in the video
  130. *
  131. * @param skip
  132. */
  133. toggleSkipInactive: (skip: boolean) => void;
  134. };
  135. const ReplayPlayerContext = createContext<ReplayPlayerContextProps>({
  136. clearAllHighlights: () => {},
  137. currentHoverTime: undefined,
  138. currentTime: 0,
  139. dimensions: {height: 0, width: 0},
  140. fastForwardSpeed: 0,
  141. highlight: () => {},
  142. initRoot: () => {},
  143. isBuffering: false,
  144. isFetching: false,
  145. isFinished: false,
  146. isPlaying: false,
  147. isSkippingInactive: true,
  148. removeHighlight: () => {},
  149. replay: null,
  150. restart: () => {},
  151. setCurrentHoverTime: () => {},
  152. setCurrentTime: () => {},
  153. setSpeed: () => {},
  154. speed: 1,
  155. togglePlayPause: () => {},
  156. toggleSkipInactive: () => {},
  157. });
  158. type Props = {
  159. children: React.ReactNode;
  160. /**
  161. * Is the data inside the `replay` complete, or are we waiting for more.
  162. */
  163. isFetching: boolean;
  164. replay: ReplayReader | null;
  165. /**
  166. * Time, in seconds, when the video should start
  167. */
  168. initialTimeOffsetMs?: ReturnType<typeof useInitialOffsetMs>;
  169. /**
  170. * Override return fields for testing
  171. */
  172. value?: Partial<ReplayPlayerContextProps>;
  173. };
  174. function useCurrentTime(callback: () => number) {
  175. const [currentTime, setCurrentTime] = useState(0);
  176. useRAF(() => setCurrentTime(callback));
  177. return currentTime;
  178. }
  179. function updateSavedReplayConfig(config: ReplayConfig) {
  180. localStorage.setItem(ReplayLocalstorageKeys.REPLAY_CONFIG, JSON.stringify(config));
  181. }
  182. export function Provider({
  183. children,
  184. initialTimeOffsetMs,
  185. isFetching,
  186. replay,
  187. value = {},
  188. }: Props) {
  189. const config = useLegacyStore(ConfigStore);
  190. const organization = useOrganization();
  191. const events = replay?.getRRWebEvents();
  192. const savedReplayConfigRef = useRef<ReplayConfig>(
  193. JSON.parse(localStorage.getItem(ReplayLocalstorageKeys.REPLAY_CONFIG) || '{}')
  194. );
  195. const theme = useTheme();
  196. const oldEvents = usePrevious(events);
  197. // Note we have to check this outside of hooks, see `usePrevious` comments
  198. const hasNewEvents = events !== oldEvents;
  199. const replayerRef = useRef<Replayer>(null);
  200. const [dimensions, setDimensions] = useState<Dimensions>({height: 0, width: 0});
  201. const [currentHoverTime, setCurrentHoverTime] = useState<undefined | number>();
  202. const [isPlaying, setIsPlaying] = useState(false);
  203. const [finishedAtMS, setFinishedAtMS] = useState<number>(-1);
  204. const [isSkippingInactive, setIsSkippingInactive] = useState(
  205. savedReplayConfigRef.current.skip ?? true
  206. );
  207. const [speed, setSpeedState] = useState(savedReplayConfigRef.current.speed || 1);
  208. const [fastForwardSpeed, setFFSpeed] = useState(0);
  209. const [buffer, setBufferTime] = useState({target: -1, previous: -1});
  210. const playTimer = useRef<number | undefined>(undefined);
  211. const didApplyInitialOffset = useRef(false);
  212. const isFinished = replayerRef.current?.getCurrentTime() === finishedAtMS;
  213. const forceDimensions = (dimension: Dimensions) => {
  214. setDimensions(dimension);
  215. };
  216. const onFastForwardStart = (e: {speed: number}) => {
  217. setFFSpeed(e.speed);
  218. };
  219. const onFastForwardEnd = () => {
  220. setFFSpeed(0);
  221. };
  222. const highlight = useCallback(({nodeId, annotation, spotlight}: HighlightParams) => {
  223. const replayer = replayerRef.current;
  224. if (!replayer) {
  225. return;
  226. }
  227. highlightNode({replayer, nodeId, annotation, spotlight});
  228. }, []);
  229. const clearAllHighlightsCallback = useCallback(() => {
  230. const replayer = replayerRef.current;
  231. if (!replayer) {
  232. return;
  233. }
  234. clearAllHighlights({replayer});
  235. }, []);
  236. const removeHighlight = useCallback(({nodeId}: {nodeId: number}) => {
  237. const replayer = replayerRef.current;
  238. if (!replayer) {
  239. return;
  240. }
  241. removeHighlightedNode({replayer, nodeId});
  242. }, []);
  243. const setReplayFinished = useCallback(() => {
  244. setFinishedAtMS(replayerRef.current?.getCurrentTime() ?? -1);
  245. setIsPlaying(false);
  246. }, []);
  247. const getCurrentTime = useCallback(
  248. () => (replayerRef.current ? Math.max(replayerRef.current.getCurrentTime(), 0) : 0),
  249. []
  250. );
  251. const privateSetCurrentTime = useCallback(
  252. (requestedTimeMs: number) => {
  253. const replayer = replayerRef.current;
  254. if (!replayer) {
  255. return;
  256. }
  257. const skipInactive = replayer.config;
  258. if (skipInactive) {
  259. // If the replayer is set to skip inactive, we should turn it off before
  260. // manually scrubbing, so when the player resumes playing its not stuck
  261. replayer.setConfig({skipInactive: false});
  262. }
  263. const maxTimeMs = replayerRef.current?.getMetaData().totalTime;
  264. const time = requestedTimeMs > maxTimeMs ? 0 : requestedTimeMs;
  265. // Sometimes rrweb doesn't get to the exact target time, as long as it has
  266. // changed away from the previous time then we can hide then buffering message.
  267. setBufferTime({target: time, previous: getCurrentTime()});
  268. // Clear previous timers. Without this (but with the setTimeout) multiple
  269. // requests to set the currentTime could finish out of order and cause jumping.
  270. if (playTimer.current) {
  271. window.clearTimeout(playTimer.current);
  272. }
  273. if (skipInactive) {
  274. replayer.setConfig({skipInactive: true});
  275. }
  276. if (isPlaying) {
  277. playTimer.current = window.setTimeout(() => replayer.play(time), 0);
  278. setIsPlaying(true);
  279. } else {
  280. playTimer.current = window.setTimeout(() => replayer.pause(time), 0);
  281. setIsPlaying(false);
  282. }
  283. },
  284. [getCurrentTime, isPlaying]
  285. );
  286. const setCurrentTime = useCallback(
  287. (requestedTimeMs: number) => {
  288. privateSetCurrentTime(requestedTimeMs);
  289. clearAllHighlightsCallback();
  290. },
  291. [privateSetCurrentTime, clearAllHighlightsCallback]
  292. );
  293. const applyInitialOffset = useCallback(() => {
  294. if (
  295. !didApplyInitialOffset.current &&
  296. initialTimeOffsetMs &&
  297. events &&
  298. replayerRef.current
  299. ) {
  300. const {highlight: highlightArgs, offsetMs} = initialTimeOffsetMs;
  301. if (offsetMs > 0) {
  302. privateSetCurrentTime(offsetMs);
  303. }
  304. if (highlightArgs) {
  305. highlight(highlightArgs);
  306. setTimeout(() => {
  307. clearAllHighlightsCallback();
  308. highlight(highlightArgs);
  309. });
  310. }
  311. didApplyInitialOffset.current = true;
  312. }
  313. }, [
  314. clearAllHighlightsCallback,
  315. events,
  316. highlight,
  317. initialTimeOffsetMs,
  318. privateSetCurrentTime,
  319. ]);
  320. useEffect(clearAllHighlightsCallback, [clearAllHighlightsCallback, isPlaying]);
  321. const initRoot = useCallback(
  322. (root: RootElem) => {
  323. if (events === undefined || root === null || isFetching) {
  324. return;
  325. }
  326. if (replayerRef.current) {
  327. if (!hasNewEvents) {
  328. return;
  329. }
  330. if (replayerRef.current.iframe.contentDocument?.body.childElementCount === 0) {
  331. // If this is true, then no need to clear old iframe as nothing was rendered
  332. return;
  333. }
  334. // We have new events, need to clear out the old iframe because a new
  335. // `Replayer` instance is about to be created
  336. while (root.firstChild) {
  337. root.removeChild(root.firstChild);
  338. }
  339. }
  340. // eslint-disable-next-line no-new
  341. const inst = new Replayer(events, {
  342. root,
  343. blockClass: 'sentry-block',
  344. // liveMode: false,
  345. // triggerFocus: false,
  346. mouseTail: {
  347. duration: 0.75 * 1000,
  348. lineCap: 'round',
  349. lineWidth: 2,
  350. strokeStyle: theme.purple200,
  351. },
  352. // unpackFn: _ => _,
  353. // plugins: [],
  354. skipInactive: savedReplayConfigRef.current.skip ?? true,
  355. speed: savedReplayConfigRef.current.speed || 1,
  356. });
  357. // @ts-expect-error: rrweb types event handlers with `unknown` parameters
  358. inst.on(ReplayerEvents.Resize, forceDimensions);
  359. inst.on(ReplayerEvents.Finish, setReplayFinished);
  360. // @ts-expect-error: rrweb types event handlers with `unknown` parameters
  361. inst.on(ReplayerEvents.SkipStart, onFastForwardStart);
  362. inst.on(ReplayerEvents.SkipEnd, onFastForwardEnd);
  363. // `.current` is marked as readonly, but it's safe to set the value from
  364. // inside a `useEffect` hook.
  365. // See: https://reactjs.org/docs/hooks-faq.html#is-there-something-like-instance-variables
  366. // @ts-expect-error
  367. replayerRef.current = inst;
  368. applyInitialOffset();
  369. },
  370. [
  371. events,
  372. isFetching,
  373. theme.purple200,
  374. setReplayFinished,
  375. hasNewEvents,
  376. applyInitialOffset,
  377. ]
  378. );
  379. const setSpeed = useCallback(
  380. (newSpeed: number) => {
  381. const replayer = replayerRef.current;
  382. savedReplayConfigRef.current = {
  383. ...savedReplayConfigRef.current,
  384. speed: newSpeed,
  385. };
  386. updateSavedReplayConfig(savedReplayConfigRef.current);
  387. if (!replayer) {
  388. return;
  389. }
  390. if (isPlaying) {
  391. replayer.pause();
  392. replayer.setConfig({speed: newSpeed});
  393. replayer.play(getCurrentTime());
  394. } else {
  395. replayer.setConfig({speed: newSpeed});
  396. }
  397. setSpeedState(newSpeed);
  398. },
  399. [getCurrentTime, isPlaying]
  400. );
  401. const togglePlayPause = useCallback(
  402. (play: boolean) => {
  403. const replayer = replayerRef.current;
  404. if (!replayer) {
  405. return;
  406. }
  407. if (play) {
  408. replayer.play(getCurrentTime());
  409. } else {
  410. replayer.pause(getCurrentTime());
  411. }
  412. setIsPlaying(play);
  413. trackAnalytics('replay.play-pause', {
  414. organization,
  415. user_email: config.user.email,
  416. play,
  417. });
  418. },
  419. [getCurrentTime, config.user.email, organization]
  420. );
  421. useEffect(() => {
  422. const handleVisibilityChange = () => {
  423. if (document.visibilityState !== 'visible') {
  424. togglePlayPause(false);
  425. }
  426. };
  427. if (replayerRef.current && events) {
  428. initRoot(replayerRef.current.wrapper.parentElement as RootElem);
  429. document.addEventListener('visibilitychange', handleVisibilityChange);
  430. }
  431. return () => {
  432. document.removeEventListener('visibilitychange', handleVisibilityChange);
  433. };
  434. }, [initRoot, events, togglePlayPause]);
  435. const restart = useCallback(() => {
  436. if (replayerRef.current) {
  437. replayerRef.current.play(0);
  438. setIsPlaying(true);
  439. }
  440. }, []);
  441. const toggleSkipInactive = useCallback((skip: boolean) => {
  442. const replayer = replayerRef.current;
  443. savedReplayConfigRef.current = {
  444. ...savedReplayConfigRef.current,
  445. skip,
  446. };
  447. updateSavedReplayConfig(savedReplayConfigRef.current);
  448. if (!replayer) {
  449. return;
  450. }
  451. if (skip !== replayer.config.skipInactive) {
  452. replayer.setConfig({skipInactive: skip});
  453. }
  454. setIsSkippingInactive(skip);
  455. }, []);
  456. const currentPlayerTime = useCurrentTime(getCurrentTime);
  457. const [isBuffering, currentTime] =
  458. buffer.target !== -1 &&
  459. buffer.previous === currentPlayerTime &&
  460. buffer.target !== buffer.previous
  461. ? [true, buffer.target]
  462. : [false, currentPlayerTime];
  463. useEffect(() => {
  464. if (!isBuffering && events && events.length >= 2 && replayerRef.current) {
  465. applyInitialOffset();
  466. }
  467. }, [isBuffering, events, applyInitialOffset]);
  468. useEffect(() => {
  469. if (!isBuffering && buffer.target !== -1) {
  470. setBufferTime({target: -1, previous: -1});
  471. }
  472. }, [isBuffering, buffer.target]);
  473. return (
  474. <ReplayPlayerContext.Provider
  475. value={{
  476. clearAllHighlights: clearAllHighlightsCallback,
  477. currentHoverTime,
  478. currentTime,
  479. dimensions,
  480. fastForwardSpeed,
  481. highlight,
  482. initRoot,
  483. isBuffering,
  484. isFetching,
  485. isFinished,
  486. isPlaying,
  487. isSkippingInactive,
  488. removeHighlight,
  489. replay,
  490. restart,
  491. setCurrentHoverTime,
  492. setCurrentTime,
  493. setSpeed,
  494. speed,
  495. togglePlayPause,
  496. toggleSkipInactive,
  497. ...value,
  498. }}
  499. >
  500. {children}
  501. </ReplayPlayerContext.Provider>
  502. );
  503. }
  504. export const useReplayContext = () => useContext(ReplayPlayerContext);