replayPlayer.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import {Fragment, useCallback, useEffect, useRef, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {useResizeObserver} from '@react-aria/utils';
  4. import NegativeSpaceContainer from 'sentry/components/container/negativeSpaceContainer';
  5. import LoadingIndicator from 'sentry/components/loadingIndicator';
  6. import BufferingOverlay from 'sentry/components/replays/player/bufferingOverlay';
  7. import FastForwardBadge from 'sentry/components/replays/player/fastForwardBadge';
  8. import {useReplayContext} from 'sentry/components/replays/replayContext';
  9. import {trackAnalytics} from 'sentry/utils/analytics';
  10. import useOrganization from 'sentry/utils/useOrganization';
  11. import PlayerDOMAlert from './playerDOMAlert';
  12. type Dimensions = ReturnType<typeof useReplayContext>['dimensions'];
  13. interface Props {
  14. className?: string;
  15. isPreview?: boolean;
  16. overlayContent?: React.ReactNode;
  17. }
  18. function useVideoSizeLogger({
  19. videoDimensions,
  20. windowDimensions,
  21. }: {
  22. videoDimensions: Dimensions;
  23. windowDimensions: Dimensions;
  24. }) {
  25. const organization = useOrganization();
  26. const [didLog, setDidLog] = useState<boolean>(false);
  27. const {analyticsContext} = useReplayContext();
  28. useEffect(() => {
  29. if (didLog || (videoDimensions.width === 0 && videoDimensions.height === 0)) {
  30. return;
  31. }
  32. const aspect_ratio =
  33. videoDimensions.width > videoDimensions.height ? 'landscape' : 'portrait';
  34. const scale = Math.min(
  35. windowDimensions.width / videoDimensions.width,
  36. windowDimensions.height / videoDimensions.height,
  37. 1
  38. );
  39. const scale_bucket = (Math.floor(scale * 10) * 10) as Parameters<
  40. typeof trackAnalytics<'replay.render-player'>
  41. >[1]['scale_bucket'];
  42. trackAnalytics('replay.render-player', {
  43. organization,
  44. aspect_ratio,
  45. context: analyticsContext,
  46. scale_bucket,
  47. });
  48. setDidLog(true);
  49. }, [organization, windowDimensions, videoDimensions, didLog, analyticsContext]);
  50. }
  51. function BasePlayerRoot({className, overlayContent, isPreview = false}: Props) {
  52. const {
  53. dimensions: videoDimensions,
  54. fastForwardSpeed,
  55. initRoot,
  56. isBuffering,
  57. isFetching,
  58. isFinished,
  59. } = useReplayContext();
  60. const windowEl = useRef<HTMLDivElement>(null);
  61. const viewEl = useRef<HTMLDivElement>(null);
  62. const [windowDimensions, setWindowDimensions] = useState<Dimensions>({
  63. width: 0,
  64. height: 0,
  65. });
  66. useVideoSizeLogger({videoDimensions, windowDimensions});
  67. // Create the `rrweb` instance which creates an iframe inside `viewEl`
  68. useEffect(() => initRoot(viewEl.current), [initRoot]);
  69. // Read the initial width & height where the player will be inserted, this is
  70. // so we can shrink the video into the available space.
  71. // If the size of the container changes, we can re-calculate the scaling factor
  72. const updateWindowDimensions = useCallback(
  73. () =>
  74. setWindowDimensions({
  75. width: windowEl.current?.clientWidth || 0,
  76. height: windowEl.current?.clientHeight || 0,
  77. }),
  78. [setWindowDimensions]
  79. );
  80. useResizeObserver({ref: windowEl, onResize: updateWindowDimensions});
  81. // If your browser doesn't have ResizeObserver then set the size once.
  82. useEffect(() => {
  83. if (typeof window.ResizeObserver !== 'undefined') {
  84. return;
  85. }
  86. updateWindowDimensions();
  87. }, [updateWindowDimensions]);
  88. // Update the scale of the view whenever dimensions have changed.
  89. useEffect(() => {
  90. if (viewEl.current) {
  91. const scale = Math.min(
  92. windowDimensions.width / videoDimensions.width,
  93. windowDimensions.height / videoDimensions.height,
  94. 1
  95. );
  96. if (scale) {
  97. viewEl.current.style['transform-origin'] = 'top left';
  98. viewEl.current.style.transform = `scale(${scale})`;
  99. viewEl.current.style.width = `${videoDimensions.width * scale}px`;
  100. viewEl.current.style.height = `${videoDimensions.height * scale}px`;
  101. }
  102. }
  103. }, [windowDimensions, videoDimensions]);
  104. return (
  105. <Fragment>
  106. {isFinished && overlayContent && (
  107. <Overlay>
  108. <OverlayInnerWrapper>{overlayContent}</OverlayInnerWrapper>
  109. </Overlay>
  110. )}
  111. <StyledNegativeSpaceContainer ref={windowEl} className="sentry-block">
  112. <div ref={viewEl} className={className} />
  113. {fastForwardSpeed ? <PositionedFastForward speed={fastForwardSpeed} /> : null}
  114. {isBuffering ? <PositionedBuffering /> : null}
  115. {isPreview ? null : <PlayerDOMAlert />}
  116. {isFetching ? <PositionedLoadingIndicator /> : null}
  117. </StyledNegativeSpaceContainer>
  118. </Fragment>
  119. );
  120. }
  121. const PositionedFastForward = styled(FastForwardBadge)`
  122. position: absolute;
  123. left: 0;
  124. bottom: 0;
  125. `;
  126. const PositionedBuffering = styled(BufferingOverlay)`
  127. position: absolute;
  128. top: 0;
  129. left: 0;
  130. right: 0;
  131. bottom: 0;
  132. `;
  133. const PositionedLoadingIndicator = styled(LoadingIndicator)`
  134. position: absolute;
  135. `;
  136. // Base styles, to make the Replayer instance work
  137. const PlayerRoot = styled(BasePlayerRoot)`
  138. .replayer-wrapper {
  139. user-select: none;
  140. }
  141. .replayer-wrapper > .replayer-mouse {
  142. pointer-events: none;
  143. }
  144. .replayer-wrapper > .replayer-mouse-tail {
  145. position: absolute;
  146. pointer-events: none;
  147. }
  148. /* Override default user-agent styles */
  149. .replayer-wrapper > iframe {
  150. border: none;
  151. background: white;
  152. /* Set pointer-events to make it easier to right-click & inspect */
  153. pointer-events: initial !important;
  154. }
  155. `;
  156. // Sentry-specific styles for the player.
  157. // The elements we have to work with are:
  158. // ```css
  159. // div.replayer-wrapper {}
  160. // div.replayer-wrapper > div.replayer-mouse {}
  161. // div.replayer-wrapper > canvas.replayer-mouse-tail {}
  162. // div.replayer-wrapper > iframe {}
  163. // ```
  164. // The mouse-tail is also configured for color/size in `app/components/replays/replayContext.tsx`
  165. const SentryPlayerRoot = styled(PlayerRoot)`
  166. .replayer-mouse {
  167. position: absolute;
  168. width: 32px;
  169. height: 32px;
  170. transition:
  171. left 0.05s linear,
  172. top 0.05s linear;
  173. background-size: contain;
  174. background-repeat: no-repeat;
  175. background-image: url('data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTIiIGhlaWdodD0iMTkiIHZpZXdCb3g9IjAgMCAxMiAxOSIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZD0iTTAgMTZWMEwxMS42IDExLjZINC44TDQuNCAxMS43TDAgMTZaIiBmaWxsPSJ3aGl0ZSIvPgo8cGF0aCBkPSJNOS4xIDE2LjdMNS41IDE4LjJMMC43OTk5OTkgNy4xTDQuNSA1LjZMOS4xIDE2LjdaIiBmaWxsPSJ3aGl0ZSIvPgo8cGF0aCBkPSJNNC42NzQ1MSA4LjYxODUxTDIuODMwMzEgOS4zOTI3MUw1LjkyNzExIDE2Ljc2OTVMNy43NzEzMSAxNS45OTUzTDQuNjc0NTEgOC42MTg1MVoiIGZpbGw9ImJsYWNrIi8+CjxwYXRoIGQ9Ik0xIDIuNFYxMy42TDQgMTAuN0w0LjQgMTAuNkg5LjJMMSAyLjRaIiBmaWxsPSJibGFjayIvPgo8L3N2Zz4K');
  176. border-color: transparent;
  177. }
  178. .replayer-mouse:after {
  179. content: '';
  180. display: inline-block;
  181. width: 32px;
  182. height: 32px;
  183. background: ${p => p.theme.purple300};
  184. border-radius: 100%;
  185. transform: translate(-50%, -50%);
  186. opacity: 0.3;
  187. }
  188. .replayer-mouse.active:after {
  189. animation: click 0.2s ease-in-out 1;
  190. }
  191. .replayer-mouse.touch-device {
  192. background-image: none;
  193. width: 70px;
  194. height: 70px;
  195. border-radius: 100%;
  196. margin-left: -37px;
  197. margin-top: -37px;
  198. border: 4px solid rgba(73, 80, 246, 0);
  199. transition:
  200. left 0s linear,
  201. top 0s linear,
  202. border-color 0.2s ease-in-out;
  203. }
  204. .replayer-mouse.touch-device.touch-active {
  205. border-color: ${p => p.theme.purple200};
  206. transition:
  207. left 0.25s linear,
  208. top 0.25s linear,
  209. border-color 0.2s ease-in-out;
  210. }
  211. .replayer-mouse.touch-device:after {
  212. opacity: 0;
  213. }
  214. .replayer-mouse.touch-device.active:after {
  215. animation: touch-click 0.2s ease-in-out 1;
  216. }
  217. @keyframes click {
  218. 0% {
  219. opacity: 0.3;
  220. width: 20px;
  221. height: 20px;
  222. }
  223. 50% {
  224. opacity: 0.5;
  225. width: 10px;
  226. height: 10px;
  227. }
  228. }
  229. @keyframes touch-click {
  230. 0% {
  231. opacity: 0;
  232. width: 20px;
  233. height: 20px;
  234. }
  235. 50% {
  236. opacity: 0.5;
  237. width: 10px;
  238. height: 10px;
  239. }
  240. }
  241. `;
  242. const Overlay = styled('div')`
  243. position: absolute;
  244. top: 0;
  245. left: 0;
  246. right: 0;
  247. bottom: 0;
  248. background-color: rgba(0, 0, 0, 0.5);
  249. z-index: 1;
  250. `;
  251. const OverlayInnerWrapper = styled('div')`
  252. position: absolute;
  253. top: 50%;
  254. left: 50%;
  255. transform: translate(-50%, -50%);
  256. color: white;
  257. display: flex;
  258. justify-content: center;
  259. flex-direction: column;
  260. align-items: center;
  261. gap: 9px;
  262. `;
  263. const StyledNegativeSpaceContainer = styled(NegativeSpaceContainer)`
  264. position: relative;
  265. width: 100%;
  266. height: 100%;
  267. `;
  268. export default SentryPlayerRoot;