groupReplays.tsx 8.4 KB

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