groupReplays.tsx 8.3 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. statsPeriod: '90d',
  143. });
  144. const replayListData = useReplayList({
  145. eventView,
  146. location: locationForFetching,
  147. organization,
  148. queryReferrer: 'issueReplays',
  149. });
  150. const {replays} = replayListData;
  151. const rawReplayIndex = urlParams.getParamValue('selected_replay_index');
  152. const selectedReplayIndex = parseInt(
  153. typeof rawReplayIndex === 'string' ? rawReplayIndex : '0',
  154. 10
  155. );
  156. const setSelectedReplayIndex = useCallback(
  157. (index: number) => {
  158. browserHistory.replace({
  159. pathname: location.pathname,
  160. query: {...location.query, selected_replay_index: index},
  161. });
  162. },
  163. [location]
  164. );
  165. const selectedReplay = replays?.[selectedReplayIndex];
  166. // If the selected replay changes, we want to force the replay to hide to cause the
  167. // replay context to unmount and reset
  168. const [previousReplayIndex, setPreviousReplayIndex] =
  169. useState<number>(selectedReplayIndex);
  170. const [forceHideReplay, setForceHideReplay] = useState<boolean>(false);
  171. useEffect(() => {
  172. if (selectedReplayIndex !== previousReplayIndex) {
  173. setPreviousReplayIndex(selectedReplayIndex);
  174. setForceHideReplay(true);
  175. }
  176. }, [selectedReplayIndex, previousReplayIndex]);
  177. useEffect(() => {
  178. if (forceHideReplay) {
  179. setForceHideReplay(false);
  180. }
  181. }, [forceHideReplay]);
  182. const replayCount = getReplayCountForIssue(group.id, group.issueCategory);
  183. const nextReplay = replays?.[selectedReplayIndex + 1];
  184. const nextReplayText = nextReplay?.id
  185. ? `${nextReplay.user.display_name || t('Anonymous User')}`
  186. : undefined;
  187. const overlayContent =
  188. nextReplayText && replayCount && replayCount > 1 ? (
  189. <Fragment>
  190. <UpNext>{t('Up Next')}</UpNext>
  191. <OverlayText>{nextReplayText}</OverlayText>
  192. <Button
  193. onClick={() => {
  194. setSelectedReplayIndex(selectedReplayIndex + 1);
  195. }}
  196. icon={<IconPlay size="md" />}
  197. analyticsEventKey="issue_details.replay_tab.play_next_replay"
  198. analyticsEventName="Issue Details: Replay Tab Clicked Play Next Replay"
  199. >
  200. {t('Play Now')}
  201. </Button>
  202. </Fragment>
  203. ) : undefined;
  204. const inner =
  205. selectedReplay && !forceHideReplay ? (
  206. <GroupReplaysTableInner
  207. setSelectedReplayIndex={setSelectedReplayIndex}
  208. selectedReplayIndex={selectedReplayIndex}
  209. overlayContent={overlayContent}
  210. organization={organization}
  211. group={group}
  212. replaySlug={selectedReplay.id}
  213. pageLinks={replayListData.pageLinks}
  214. fetchError={replayListData.fetchError}
  215. isFetching={replayListData.isFetching}
  216. replays={replays}
  217. />
  218. ) : (
  219. <ReplayTable
  220. sort={undefined}
  221. visibleColumns={VISIBLE_COLUMNS}
  222. showDropdownFilters={false}
  223. onClickPlay={setSelectedReplayIndex}
  224. fetchError={replayListData.fetchError}
  225. isFetching={replayListData.isFetching}
  226. replays={replays}
  227. />
  228. );
  229. return (
  230. <StyledLayoutPage withPadding>
  231. <ReplayCountHeader>
  232. <StyledIconUser size="sm" />
  233. {t(
  234. 'Replay captured %s users experiencing this issue across %s events.',
  235. replayCount,
  236. group.count
  237. )}
  238. </ReplayCountHeader>
  239. {inner}
  240. </StyledLayoutPage>
  241. );
  242. }
  243. const StyledLayoutPage = styled(Layout.Page)`
  244. box-shadow: 0px 0px 1px ${p => p.theme.gray200};
  245. background-color: ${p => p.theme.background};
  246. gap: ${space(2)};
  247. `;
  248. const ReplayCountHeader = styled('div')`
  249. display: flex;
  250. align-items: center;
  251. gap: ${space(1)};
  252. `;
  253. const StyledIconUser = styled(IconUser)`
  254. margin-right: ${p => p.theme.grid}px;
  255. height: 16px;
  256. width: 16px;
  257. `;
  258. const OverlayText = styled('div')`
  259. font-size: ${p => p.theme.fontSizeExtraLarge};
  260. `;
  261. const UpNext = styled('div')`
  262. line-height: 0;
  263. `;
  264. export default GroupReplays;