details.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import {Fragment, useEffect} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import Alert from 'sentry/components/alert';
  4. import {Flex} from 'sentry/components/container/flex';
  5. import DetailedError from 'sentry/components/errors/detailedError';
  6. import NotFound from 'sentry/components/errors/notFound';
  7. import * as Layout from 'sentry/components/layouts/thirds';
  8. import List from 'sentry/components/list';
  9. import ListItem from 'sentry/components/list/listItem';
  10. import {LocalStorageReplayPreferences} from 'sentry/components/replays/preferences/replayPreferences';
  11. import {Provider as ReplayContextProvider} from 'sentry/components/replays/replayContext';
  12. import {IconDelete} from 'sentry/icons';
  13. import {t} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import {decodeScalar} from 'sentry/utils/queryString';
  16. import type {TimeOffsetLocationQueryParams} from 'sentry/utils/replays/hooks/useInitialTimeOffsetMs';
  17. import useInitialTimeOffsetMs from 'sentry/utils/replays/hooks/useInitialTimeOffsetMs';
  18. import useLogReplayDataLoaded from 'sentry/utils/replays/hooks/useLogReplayDataLoaded';
  19. import useMarkReplayViewed from 'sentry/utils/replays/hooks/useMarkReplayViewed';
  20. import useReplayPageview from 'sentry/utils/replays/hooks/useReplayPageview';
  21. import useReplayReader from 'sentry/utils/replays/hooks/useReplayReader';
  22. import useRouteAnalyticsEventNames from 'sentry/utils/routeAnalytics/useRouteAnalyticsEventNames';
  23. import useRouteAnalyticsParams from 'sentry/utils/routeAnalytics/useRouteAnalyticsParams';
  24. import {useLocation} from 'sentry/utils/useLocation';
  25. import useOrganization from 'sentry/utils/useOrganization';
  26. import {useUser} from 'sentry/utils/useUser';
  27. import ReplaysLayout from 'sentry/views/replays/detail/layout';
  28. import Page from 'sentry/views/replays/detail/page';
  29. import ReplayTransactionContext from 'sentry/views/replays/detail/trace/replayTransactionContext';
  30. type Props = RouteComponentProps<
  31. {replaySlug: string},
  32. {},
  33. any,
  34. TimeOffsetLocationQueryParams
  35. >;
  36. function ReplayDetails({params: {replaySlug}}: Props) {
  37. const user = useUser();
  38. const location = useLocation();
  39. const organization = useOrganization();
  40. useReplayPageview('replay.details-time-spent');
  41. useRouteAnalyticsEventNames('replay_details.viewed', 'Replay Details: Viewed');
  42. useRouteAnalyticsParams({
  43. organization,
  44. referrer: decodeScalar(location.query.referrer),
  45. user_email: user.email,
  46. tab: location.query.t_main,
  47. });
  48. const {slug: orgSlug} = organization;
  49. // TODO: replayId is known ahead of time and useReplayData is parsing it from the replaySlug
  50. // once we fix the route params and links we should fix this to accept replayId and stop returning it
  51. const {
  52. errors,
  53. fetchError,
  54. fetching,
  55. onRetry,
  56. projectSlug,
  57. replay,
  58. replayId,
  59. replayRecord,
  60. } = useReplayReader({
  61. replaySlug,
  62. orgSlug,
  63. });
  64. const replayErrors = errors.filter(e => e.title !== 'User Feedback');
  65. useLogReplayDataLoaded({fetchError, fetching, projectSlug, replay});
  66. const {mutate: markAsViewed} = useMarkReplayViewed();
  67. useEffect(() => {
  68. if (
  69. !fetchError &&
  70. replayRecord &&
  71. !replayRecord.has_viewed &&
  72. projectSlug &&
  73. !fetching &&
  74. replayId
  75. ) {
  76. markAsViewed({projectSlug, replayId});
  77. }
  78. }, [
  79. fetchError,
  80. fetching,
  81. markAsViewed,
  82. organization,
  83. projectSlug,
  84. replayId,
  85. replayRecord,
  86. ]);
  87. const initialTimeOffsetMs = useInitialTimeOffsetMs({
  88. orgSlug,
  89. projectSlug,
  90. replayId,
  91. replayStartTimestampMs: replayRecord?.started_at?.getTime(),
  92. });
  93. const rrwebFrames = replay?.getRRWebFrames();
  94. // The replay data takes a while to load in, which causes `isVideoReplay`
  95. // to return an early `false`, which used to cause UI jumping.
  96. // One way to check whether it's finished loading is by checking the length
  97. // of the rrweb frames, which should always be > 2 for any given replay.
  98. // By default, the 2 frames are replay.start and replay.end
  99. const isLoading = !rrwebFrames || (rrwebFrames && rrwebFrames.length <= 2);
  100. if (replayRecord?.is_archived) {
  101. return (
  102. <Page
  103. orgSlug={orgSlug}
  104. replayRecord={replayRecord}
  105. projectSlug={projectSlug}
  106. replayErrors={replayErrors}
  107. >
  108. <Layout.Page>
  109. <Alert system type="warning" data-test-id="replay-deleted">
  110. <Flex gap={space(0.5)}>
  111. <IconDelete color="gray500" size="sm" />
  112. {t('This replay has been deleted.')}
  113. </Flex>
  114. </Alert>
  115. </Layout.Page>
  116. </Page>
  117. );
  118. }
  119. if (fetchError) {
  120. if (fetchError.status === 404) {
  121. return (
  122. <Page
  123. orgSlug={orgSlug}
  124. replayRecord={replayRecord}
  125. projectSlug={projectSlug}
  126. replayErrors={replayErrors}
  127. >
  128. <Layout.Page withPadding>
  129. <NotFound />
  130. </Layout.Page>
  131. </Page>
  132. );
  133. }
  134. const reasons = [
  135. t('The replay is still processing'),
  136. t('The replay has been deleted by a member in your organization'),
  137. t('There is an internal systems error'),
  138. ];
  139. return (
  140. <Page
  141. orgSlug={orgSlug}
  142. replayRecord={replayRecord}
  143. projectSlug={projectSlug}
  144. replayErrors={replayErrors}
  145. >
  146. <Layout.Page>
  147. <DetailedError
  148. onRetry={onRetry}
  149. hideSupportLinks
  150. heading={t('There was an error while fetching this Replay')}
  151. message={
  152. <Fragment>
  153. <p>{t('This could be due to these reasons:')}</p>
  154. <List symbol="bullet">
  155. {reasons.map((reason, i) => (
  156. <ListItem key={i}>{reason}</ListItem>
  157. ))}
  158. </List>
  159. </Fragment>
  160. }
  161. />
  162. </Layout.Page>
  163. </Page>
  164. );
  165. }
  166. const isVideoReplay = Boolean(
  167. organization.features.includes('session-replay-mobile-player') &&
  168. replay?.isVideoReplay()
  169. );
  170. return (
  171. <ReplayContextProvider
  172. analyticsContext="replay_details"
  173. initialTimeOffsetMs={initialTimeOffsetMs}
  174. isFetching={fetching}
  175. prefsStrategy={LocalStorageReplayPreferences}
  176. replay={replay}
  177. >
  178. <ReplayTransactionContext replayRecord={replayRecord}>
  179. <Page
  180. isVideoReplay={isVideoReplay}
  181. orgSlug={orgSlug}
  182. replayRecord={replayRecord}
  183. projectSlug={projectSlug}
  184. replayErrors={replayErrors}
  185. isLoading={isLoading}
  186. >
  187. <ReplaysLayout
  188. isVideoReplay={isVideoReplay}
  189. replayRecord={replayRecord}
  190. isLoading={isLoading}
  191. />
  192. </Page>
  193. </ReplayTransactionContext>
  194. </ReplayContextProvider>
  195. );
  196. }
  197. export default ReplayDetails;