replayContext.tsx 13 KB

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