groupReplays.tsx 8.4 KB

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