replayContext.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. ReplayConfig = '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.ReplayConfig, 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.ReplayConfig) || '{}')
  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 unMountedRef = 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 initRoot = useCallback(
  248. (root: RootElem) => {
  249. if (events === undefined) {
  250. return;
  251. }
  252. if (root === null) {
  253. return;
  254. }
  255. if (isFetching) {
  256. return;
  257. }
  258. if (replayerRef.current) {
  259. if (!hasNewEvents && !unMountedRef.current) {
  260. // Already have a player for these events, the parent node must've re-rendered
  261. return;
  262. }
  263. if (replayerRef.current.iframe.contentDocument?.body.childElementCount === 0) {
  264. // If this is true, then no need to clear old iframe as nothing was rendered
  265. return;
  266. }
  267. // We have new events, need to clear out the old iframe because a new
  268. // `Replayer` instance is about to be created
  269. while (root.firstChild) {
  270. root.removeChild(root.firstChild);
  271. }
  272. }
  273. // eslint-disable-next-line no-new
  274. const inst = new Replayer(events, {
  275. root,
  276. blockClass: 'sentry-block',
  277. // liveMode: false,
  278. // triggerFocus: false,
  279. mouseTail: {
  280. duration: 0.75 * 1000,
  281. lineCap: 'round',
  282. lineWidth: 2,
  283. strokeStyle: theme.purple200,
  284. },
  285. // unpackFn: _ => _,
  286. // plugins: [],
  287. skipInactive: savedReplayConfigRef.current.skip ?? true,
  288. speed: savedReplayConfigRef.current.speed || 1,
  289. });
  290. // @ts-expect-error: rrweb types event handlers with `unknown` parameters
  291. inst.on(ReplayerEvents.Resize, forceDimensions);
  292. inst.on(ReplayerEvents.Finish, setReplayFinished);
  293. // @ts-expect-error: rrweb types event handlers with `unknown` parameters
  294. inst.on(ReplayerEvents.SkipStart, onFastForwardStart);
  295. inst.on(ReplayerEvents.SkipEnd, onFastForwardEnd);
  296. // `.current` is marked as readonly, but it's safe to set the value from
  297. // inside a `useEffect` hook.
  298. // See: https://reactjs.org/docs/hooks-faq.html#is-there-something-like-instance-variables
  299. // @ts-expect-error
  300. replayerRef.current = inst;
  301. if (unMountedRef.current) {
  302. unMountedRef.current = false;
  303. }
  304. },
  305. [events, isFetching, theme.purple200, setReplayFinished, hasNewEvents]
  306. );
  307. const getCurrentTime = useCallback(
  308. () => (replayerRef.current ? Math.max(replayerRef.current.getCurrentTime(), 0) : 0),
  309. []
  310. );
  311. const setCurrentTime = useCallback(
  312. (requestedTimeMs: number) => {
  313. const replayer = replayerRef.current;
  314. if (!replayer) {
  315. return;
  316. }
  317. const skipInactive = replayer.config;
  318. if (skipInactive) {
  319. // If the replayer is set to skip inactive, we should turn it off before
  320. // manually scrubbing, so when the player resumes playing its not stuck
  321. replayer.setConfig({skipInactive: false});
  322. }
  323. const maxTimeMs = replayerRef.current?.getMetaData().totalTime;
  324. const time = requestedTimeMs > maxTimeMs ? 0 : requestedTimeMs;
  325. // Sometimes rrweb doesn't get to the exact target time, as long as it has
  326. // changed away from the previous time then we can hide then buffering message.
  327. setBufferTime({target: time, previous: getCurrentTime()});
  328. // Clear previous timers. Without this (but with the setTimeout) multiple
  329. // requests to set the currentTime could finish out of order and cause jumping.
  330. if (playTimer.current) {
  331. window.clearTimeout(playTimer.current);
  332. }
  333. if (skipInactive) {
  334. replayer.setConfig({skipInactive: true});
  335. }
  336. if (isPlaying) {
  337. playTimer.current = window.setTimeout(() => replayer.play(time), 0);
  338. setIsPlaying(true);
  339. } else {
  340. playTimer.current = window.setTimeout(() => replayer.pause(time), 0);
  341. setIsPlaying(false);
  342. }
  343. },
  344. [getCurrentTime, isPlaying]
  345. );
  346. const setSpeed = useCallback(
  347. (newSpeed: number) => {
  348. const replayer = replayerRef.current;
  349. savedReplayConfigRef.current = {
  350. ...savedReplayConfigRef.current,
  351. speed: newSpeed,
  352. };
  353. updateSavedReplayConfig(savedReplayConfigRef.current);
  354. if (!replayer) {
  355. return;
  356. }
  357. if (isPlaying) {
  358. replayer.pause();
  359. replayer.setConfig({speed: newSpeed});
  360. replayer.play(getCurrentTime());
  361. } else {
  362. replayer.setConfig({speed: newSpeed});
  363. }
  364. setSpeedState(newSpeed);
  365. },
  366. [getCurrentTime, isPlaying]
  367. );
  368. const togglePlayPause = useCallback(
  369. (play: boolean) => {
  370. const replayer = replayerRef.current;
  371. if (!replayer) {
  372. return;
  373. }
  374. if (play) {
  375. replayer.play(getCurrentTime());
  376. } else {
  377. replayer.pause(getCurrentTime());
  378. }
  379. setIsPlaying(play);
  380. trackAnalytics('replay.play-pause', {
  381. organization,
  382. user_email: config.user.email,
  383. play,
  384. });
  385. },
  386. [getCurrentTime, config.user.email, organization]
  387. );
  388. useEffect(() => {
  389. const handleVisibilityChange = () => {
  390. if (document.visibilityState !== 'visible') {
  391. togglePlayPause(false);
  392. }
  393. };
  394. if (replayerRef.current && events) {
  395. initRoot(replayerRef.current.wrapper.parentElement as RootElem);
  396. document.addEventListener('visibilitychange', handleVisibilityChange);
  397. }
  398. return () => {
  399. document.removeEventListener('visibilitychange', handleVisibilityChange);
  400. };
  401. }, [initRoot, events, togglePlayPause]);
  402. const restart = useCallback(() => {
  403. if (replayerRef.current) {
  404. replayerRef.current.play(0);
  405. setIsPlaying(true);
  406. }
  407. }, []);
  408. const toggleSkipInactive = useCallback((skip: boolean) => {
  409. const replayer = replayerRef.current;
  410. savedReplayConfigRef.current = {
  411. ...savedReplayConfigRef.current,
  412. skip,
  413. };
  414. updateSavedReplayConfig(savedReplayConfigRef.current);
  415. if (!replayer) {
  416. return;
  417. }
  418. if (skip !== replayer.config.skipInactive) {
  419. replayer.setConfig({skipInactive: skip});
  420. }
  421. setIsSkippingInactive(skip);
  422. }, []);
  423. // Only on pageload: set the initial playback timestamp
  424. useEffect(() => {
  425. if (initialTimeOffsetMs?.offsetMs && events && replayerRef.current) {
  426. setCurrentTime(initialTimeOffsetMs.offsetMs);
  427. }
  428. return () => {
  429. unMountedRef.current = true;
  430. };
  431. }, [events, initialTimeOffsetMs, setCurrentTime]);
  432. const currentPlayerTime = useCurrentTime(getCurrentTime);
  433. const [isBuffering, currentTime] =
  434. buffer.target !== -1 &&
  435. buffer.previous === currentPlayerTime &&
  436. buffer.target !== buffer.previous
  437. ? [true, buffer.target]
  438. : [false, currentPlayerTime];
  439. // Only on pageload: highlight the node that relates to the initialTimeOffset
  440. useEffect(() => {
  441. if (
  442. !isBuffering &&
  443. initialTimeOffsetMs?.highlight &&
  444. events &&
  445. events?.length >= 2 &&
  446. replayerRef.current
  447. ) {
  448. const highlightArgs = initialTimeOffsetMs.highlight;
  449. highlight(highlightArgs);
  450. setTimeout(() => {
  451. clearAllHighlightsCallback();
  452. highlight(highlightArgs);
  453. });
  454. }
  455. }, [
  456. clearAllHighlightsCallback,
  457. events,
  458. dimensions,
  459. highlight,
  460. initialTimeOffsetMs,
  461. isBuffering,
  462. ]);
  463. useEffect(() => {
  464. if (!isBuffering && buffer.target !== -1) {
  465. setBufferTime({target: -1, previous: -1});
  466. }
  467. }, [isBuffering, buffer.target]);
  468. return (
  469. <ReplayPlayerContext.Provider
  470. value={{
  471. clearAllHighlights: clearAllHighlightsCallback,
  472. currentHoverTime,
  473. currentTime,
  474. dimensions,
  475. fastForwardSpeed,
  476. highlight,
  477. initRoot,
  478. isBuffering,
  479. isFetching,
  480. isFinished,
  481. isPlaying,
  482. isSkippingInactive,
  483. removeHighlight,
  484. replay,
  485. restart,
  486. setCurrentHoverTime,
  487. setCurrentTime,
  488. setSpeed,
  489. speed,
  490. togglePlayPause,
  491. toggleSkipInactive,
  492. ...value,
  493. }}
  494. >
  495. {children}
  496. </ReplayPlayerContext.Provider>
  497. );
  498. }
  499. export const useReplayContext = () => useContext(ReplayPlayerContext);