groupReplays.tsx 8.9 KB

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