groupReplays.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import {Fragment, useCallback, useEffect} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import {Button} from 'sentry/components/button';
  5. import * as Layout from 'sentry/components/layouts/thirds';
  6. import Placeholder from 'sentry/components/placeholder';
  7. import {StaticReplayPreferences} from 'sentry/components/replays/preferences/replayPreferences';
  8. import {Provider as ReplayContextProvider} from 'sentry/components/replays/replayContext';
  9. import {IconPlay, IconUser} from 'sentry/icons';
  10. import {t, tn} from 'sentry/locale';
  11. import {space} from 'sentry/styles/space';
  12. import type {Group} from 'sentry/types/group';
  13. import type {Organization} from 'sentry/types/organization';
  14. import {trackAnalytics} from 'sentry/utils/analytics';
  15. import {browserHistory} from 'sentry/utils/browserHistory';
  16. import type EventView from 'sentry/utils/discover/eventView';
  17. import useReplayCountForIssues from 'sentry/utils/replayCount/useReplayCountForIssues';
  18. import useReplayList from 'sentry/utils/replays/hooks/useReplayList';
  19. import useReplayReader from 'sentry/utils/replays/hooks/useReplayReader';
  20. import {useLocation} from 'sentry/utils/useLocation';
  21. import useOrganization from 'sentry/utils/useOrganization';
  22. import useUrlParams from 'sentry/utils/useUrlParams';
  23. import ReplayTable from 'sentry/views/replays/replayTable';
  24. import {ReplayColumn} from 'sentry/views/replays/replayTable/types';
  25. import type {ReplayListLocationQuery, ReplayListRecord} from 'sentry/views/replays/types';
  26. import {ReplayClipPreviewWrapper} from './replayClipPreviewWrapper';
  27. import useReplaysFromIssue from './useReplaysFromIssue';
  28. type Props = {
  29. group: Group;
  30. };
  31. const VISIBLE_COLUMNS = [
  32. ReplayColumn.REPLAY,
  33. ReplayColumn.OS,
  34. ReplayColumn.BROWSER,
  35. ReplayColumn.DURATION,
  36. ReplayColumn.COUNT_ERRORS,
  37. ReplayColumn.ACTIVITY,
  38. ];
  39. const VISIBLE_COLUMNS_WITH_PLAY = [ReplayColumn.PLAY_PAUSE, ...VISIBLE_COLUMNS];
  40. function GroupReplays({group}: Props) {
  41. const organization = useOrganization();
  42. const location = useLocation<ReplayListLocationQuery>();
  43. const {eventView, fetchError, isFetching, pageLinks} = useReplaysFromIssue({
  44. group,
  45. location,
  46. organization,
  47. });
  48. useEffect(() => {
  49. trackAnalytics('replay.render-issues-group-list', {
  50. project_id: group.project.id,
  51. platform: group.project.platform,
  52. organization,
  53. });
  54. // we only want to fire this event once
  55. // eslint-disable-next-line react-hooks/exhaustive-deps
  56. }, []);
  57. if (!eventView) {
  58. // Shown on load and no replay data available
  59. return (
  60. <StyledLayoutPage withPadding>
  61. <ReplayCountHeader>
  62. <IconUser size="sm" />
  63. {isFetching ? (
  64. <Placeholder height="18px" width="400px" />
  65. ) : (
  66. t('No replay data available.')
  67. )}
  68. </ReplayCountHeader>
  69. <ReplayTable
  70. fetchError={fetchError}
  71. isFetching={isFetching}
  72. replays={[]}
  73. sort={undefined}
  74. visibleColumns={VISIBLE_COLUMNS}
  75. showDropdownFilters={false}
  76. />
  77. </StyledLayoutPage>
  78. );
  79. }
  80. return (
  81. <GroupReplaysTable
  82. eventView={eventView}
  83. organization={organization}
  84. pageLinks={pageLinks}
  85. visibleColumns={VISIBLE_COLUMNS}
  86. group={group}
  87. />
  88. );
  89. }
  90. function GroupReplaysTableInner({
  91. children,
  92. organization,
  93. group,
  94. replaySlug,
  95. setSelectedReplayIndex,
  96. selectedReplayIndex,
  97. overlayContent,
  98. replays,
  99. pageLinks,
  100. }: {
  101. children: React.ReactNode;
  102. group: Group;
  103. organization: Organization;
  104. pageLinks: string | null;
  105. replaySlug: string;
  106. replays: ReplayListRecord[] | undefined;
  107. selectedReplayIndex: number;
  108. setSelectedReplayIndex: (index: number) => void;
  109. overlayContent?: React.ReactNode;
  110. }) {
  111. const orgSlug = organization.slug;
  112. const {fetching, replay} = useReplayReader({
  113. orgSlug,
  114. replaySlug,
  115. group,
  116. });
  117. return (
  118. <ReplayContextProvider
  119. analyticsContext="replay_tab"
  120. isFetching={fetching}
  121. prefsStrategy={StaticReplayPreferences}
  122. replay={replay}
  123. autoStart
  124. >
  125. <ReplayClipPreviewWrapper
  126. orgSlug={orgSlug}
  127. replaySlug={replaySlug}
  128. group={group}
  129. pageLinks={pageLinks}
  130. selectedReplayIndex={selectedReplayIndex}
  131. setSelectedReplayIndex={setSelectedReplayIndex}
  132. visibleColumns={VISIBLE_COLUMNS_WITH_PLAY}
  133. overlayContent={overlayContent}
  134. replays={replays}
  135. />
  136. {children}
  137. </ReplayContextProvider>
  138. );
  139. }
  140. const locationForFetching = {query: {}} as Location<ReplayListLocationQuery>;
  141. function GroupReplaysTable({
  142. eventView,
  143. organization,
  144. group,
  145. }: {
  146. eventView: EventView;
  147. group: Group;
  148. organization: Organization;
  149. pageLinks: string | null;
  150. visibleColumns: ReplayColumn[];
  151. }) {
  152. const location = useLocation();
  153. const urlParams = useUrlParams();
  154. const {getReplayCountForIssue} = useReplayCountForIssues({
  155. statsPeriod: '90d',
  156. });
  157. const replayListData = useReplayList({
  158. eventView,
  159. location: locationForFetching,
  160. organization,
  161. queryReferrer: 'issueReplays',
  162. });
  163. const {replays} = replayListData;
  164. const rawReplayIndex = urlParams.getParamValue('selected_replay_index');
  165. const selectedReplayIndex = parseInt(
  166. typeof rawReplayIndex === 'string' ? rawReplayIndex : '0',
  167. 10
  168. );
  169. const setSelectedReplayIndex = useCallback(
  170. (index: number) => {
  171. browserHistory.replace({
  172. pathname: location.pathname,
  173. query: {...location.query, selected_replay_index: index},
  174. });
  175. },
  176. [location]
  177. );
  178. const selectedReplay = replays?.[selectedReplayIndex];
  179. const replayCount = getReplayCountForIssue(group.id, group.issueCategory);
  180. const nextReplay = replays?.[selectedReplayIndex + 1];
  181. const nextReplayText = nextReplay?.id
  182. ? `${nextReplay.user.display_name || t('Anonymous User')}`
  183. : undefined;
  184. const overlayContent =
  185. nextReplayText && replayCount && replayCount > 1 ? (
  186. <Fragment>
  187. <UpNext>{t('Up Next')}</UpNext>
  188. <OverlayText>{nextReplayText}</OverlayText>
  189. <Button
  190. onClick={() => {
  191. setSelectedReplayIndex(selectedReplayIndex + 1);
  192. }}
  193. icon={<IconPlay size="md" />}
  194. analyticsEventKey="issue_details.replay_tab.play_next_replay"
  195. analyticsEventName="Issue Details: Replay Tab Clicked Play Next Replay"
  196. >
  197. {t('Play Now')}
  198. </Button>
  199. </Fragment>
  200. ) : undefined;
  201. const replayTable = (
  202. <ReplayTable
  203. sort={undefined}
  204. visibleColumns={selectedReplay ? VISIBLE_COLUMNS_WITH_PLAY : VISIBLE_COLUMNS}
  205. showDropdownFilters={false}
  206. onClickPlay={setSelectedReplayIndex}
  207. fetchError={replayListData.fetchError}
  208. isFetching={replayListData.isFetching}
  209. replays={replays}
  210. />
  211. );
  212. const inner = selectedReplay ? (
  213. <GroupReplaysTableInner
  214. // Use key to force unmount/remount of component to reset the context and replay iframe
  215. key={selectedReplay.id}
  216. setSelectedReplayIndex={setSelectedReplayIndex}
  217. selectedReplayIndex={selectedReplayIndex}
  218. overlayContent={overlayContent}
  219. organization={organization}
  220. group={group}
  221. replaySlug={selectedReplay.id}
  222. pageLinks={replayListData.pageLinks}
  223. replays={replays}
  224. >
  225. {replayTable}
  226. </GroupReplaysTableInner>
  227. ) : (
  228. replayTable
  229. );
  230. return (
  231. <StyledLayoutPage withPadding>
  232. <ReplayCountHeader>
  233. <IconUser size="sm" />
  234. {t(
  235. 'Replay captured %s experiencing this issue across %s.',
  236. tn('%s user', '%s users', replayCount ?? 0),
  237. tn('%s event', '%s events', group.count)
  238. )}
  239. </ReplayCountHeader>
  240. {inner}
  241. </StyledLayoutPage>
  242. );
  243. }
  244. const StyledLayoutPage = styled(Layout.Page)`
  245. box-shadow: 0px 0px 1px ${p => p.theme.gray200};
  246. background-color: ${p => p.theme.background};
  247. gap: ${space(2)};
  248. `;
  249. const ReplayCountHeader = styled('div')`
  250. display: flex;
  251. align-items: center;
  252. gap: ${space(1)};
  253. `;
  254. const OverlayText = styled('div')`
  255. font-size: ${p => p.theme.fontSizeExtraLarge};
  256. `;
  257. const UpNext = styled('div')`
  258. line-height: 0;
  259. `;
  260. export default GroupReplays;