index.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import type {ReactNode} from 'react';
  2. import {memo} from 'react';
  3. import styled from '@emotion/styled';
  4. import {Alert} from 'sentry/components/alert';
  5. import LoadingIndicator from 'sentry/components/loadingIndicator';
  6. import {PanelTable} from 'sentry/components/panels/panelTable';
  7. import {t} from 'sentry/locale';
  8. import EventView from 'sentry/utils/discover/eventView';
  9. import type {Sort} from 'sentry/utils/discover/fields';
  10. import getRouteStringFromRoutes from 'sentry/utils/getRouteStringFromRoutes';
  11. import {useLocation} from 'sentry/utils/useLocation';
  12. import useOrganization from 'sentry/utils/useOrganization';
  13. import {useRoutes} from 'sentry/utils/useRoutes';
  14. import useUrlParams from 'sentry/utils/useUrlParams';
  15. import type {ReplayListRecordWithTx} from 'sentry/views/performance/transactionSummary/transactionReplays/useReplaysWithTxData';
  16. import HeaderCell from 'sentry/views/replays/replayTable/headerCell';
  17. import {
  18. ActivityCell,
  19. BrowserCell,
  20. DeadClickCountCell,
  21. DurationCell,
  22. ErrorCountCell,
  23. OSCell,
  24. PlayPauseCell,
  25. RageClickCountCell,
  26. ReplayCell,
  27. TransactionCell,
  28. } from 'sentry/views/replays/replayTable/tableCell';
  29. import {ReplayColumn} from 'sentry/views/replays/replayTable/types';
  30. import type {ReplayListRecord} from 'sentry/views/replays/types';
  31. type Props = {
  32. fetchError: undefined | Error;
  33. isFetching: boolean;
  34. replays: undefined | ReplayListRecord[] | ReplayListRecordWithTx[];
  35. sort: Sort | undefined;
  36. visibleColumns: ReplayColumn[];
  37. emptyMessage?: ReactNode;
  38. gridRows?: string;
  39. onClickPlay?: (index: number) => void;
  40. referrerLocation?: string;
  41. showDropdownFilters?: boolean;
  42. };
  43. // Memoizing this component to avoid unnecessary re-renders
  44. const ReplayTable = memo(
  45. ({
  46. fetchError,
  47. isFetching,
  48. replays,
  49. sort,
  50. visibleColumns,
  51. emptyMessage,
  52. gridRows,
  53. showDropdownFilters,
  54. onClickPlay,
  55. referrerLocation,
  56. }: Props) => {
  57. const routes = useRoutes();
  58. const location = useLocation();
  59. const organization = useOrganization();
  60. // we may have a selected replay index in the URLs
  61. const urlParams = useUrlParams();
  62. const rawReplayIndex = urlParams.getParamValue('selected_replay_index');
  63. const selectedReplayIndex = parseInt(
  64. typeof rawReplayIndex === 'string' ? rawReplayIndex : '0',
  65. 10
  66. );
  67. const tableHeaders = visibleColumns
  68. .filter(Boolean)
  69. .map(column => <HeaderCell key={column} column={column} sort={sort} />);
  70. if (fetchError && !isFetching) {
  71. return (
  72. <StyledPanelTable
  73. headers={tableHeaders}
  74. isLoading={false}
  75. visibleColumns={visibleColumns}
  76. data-test-id="replay-table"
  77. gridRows={undefined}
  78. >
  79. <StyledAlert type="error" showIcon>
  80. {typeof fetchError === 'string'
  81. ? fetchError
  82. : t(
  83. 'Sorry, the list of replays could not be loaded. This could be due to invalid search parameters or an internal systems error.'
  84. )}
  85. </StyledAlert>
  86. </StyledPanelTable>
  87. );
  88. }
  89. const referrer = getRouteStringFromRoutes(routes);
  90. const eventView = EventView.fromLocation(location);
  91. return (
  92. <StyledPanelTable
  93. headers={tableHeaders}
  94. isEmpty={replays?.length === 0}
  95. isLoading={isFetching}
  96. visibleColumns={visibleColumns}
  97. disablePadding
  98. data-test-id="replay-table"
  99. emptyMessage={emptyMessage}
  100. gridRows={isFetching ? undefined : gridRows}
  101. loader={<LoadingIndicator style={{margin: '54px auto'}} />}
  102. disableHeaderBorderBottom
  103. >
  104. {replays?.map(
  105. (replay: ReplayListRecord | ReplayListRecordWithTx, index: number) => {
  106. return (
  107. <Row
  108. key={replay.id}
  109. isPlaying={index === selectedReplayIndex && referrerLocation !== 'replay'}
  110. onClick={() => onClickPlay?.(index)}
  111. showCursor={onClickPlay !== undefined}
  112. referrerLocation={referrerLocation}
  113. >
  114. {visibleColumns.map(column => {
  115. switch (column) {
  116. case ReplayColumn.ACTIVITY:
  117. return (
  118. <ActivityCell
  119. key="activity"
  120. replay={replay}
  121. showDropdownFilters={showDropdownFilters}
  122. />
  123. );
  124. case ReplayColumn.BROWSER:
  125. return (
  126. <BrowserCell
  127. key="browser"
  128. replay={replay}
  129. showDropdownFilters={showDropdownFilters}
  130. />
  131. );
  132. case ReplayColumn.COUNT_DEAD_CLICKS:
  133. return (
  134. <DeadClickCountCell
  135. key="countDeadClicks"
  136. replay={replay}
  137. showDropdownFilters={showDropdownFilters}
  138. />
  139. );
  140. case ReplayColumn.COUNT_ERRORS:
  141. return (
  142. <ErrorCountCell
  143. key="countErrors"
  144. replay={replay}
  145. showDropdownFilters={showDropdownFilters}
  146. />
  147. );
  148. case ReplayColumn.COUNT_RAGE_CLICKS:
  149. return (
  150. <RageClickCountCell
  151. key="countRageClicks"
  152. replay={replay}
  153. showDropdownFilters={showDropdownFilters}
  154. />
  155. );
  156. case ReplayColumn.DURATION:
  157. return (
  158. <DurationCell
  159. key="duration"
  160. replay={replay}
  161. showDropdownFilters={showDropdownFilters}
  162. />
  163. );
  164. case ReplayColumn.OS:
  165. return (
  166. <OSCell
  167. key="os"
  168. replay={replay}
  169. showDropdownFilters={showDropdownFilters}
  170. />
  171. );
  172. case ReplayColumn.REPLAY:
  173. return (
  174. <ReplayCell
  175. key="session"
  176. replay={replay}
  177. eventView={eventView}
  178. organization={organization}
  179. referrer={referrer}
  180. referrer_table="main"
  181. />
  182. );
  183. case ReplayColumn.PLAY_PAUSE:
  184. return (
  185. <PlayPauseCell
  186. key="play"
  187. isSelected={selectedReplayIndex === index}
  188. handleClick={() => onClickPlay?.(index)}
  189. />
  190. );
  191. case ReplayColumn.SLOWEST_TRANSACTION:
  192. return (
  193. <TransactionCell
  194. key="slowestTransaction"
  195. replay={replay}
  196. organization={organization}
  197. />
  198. );
  199. default:
  200. return null;
  201. }
  202. })}
  203. </Row>
  204. );
  205. }
  206. )}
  207. </StyledPanelTable>
  208. );
  209. }
  210. );
  211. const StyledPanelTable = styled(PanelTable)<{
  212. visibleColumns: ReplayColumn[];
  213. gridRows?: string;
  214. }>`
  215. margin-bottom: 0;
  216. grid-template-columns: ${p =>
  217. p.visibleColumns
  218. .filter(Boolean)
  219. .map(column => (column === 'replay' ? 'minmax(100px, 1fr)' : 'max-content'))
  220. .join(' ')};
  221. ${props =>
  222. props.gridRows
  223. ? `grid-template-rows: ${props.gridRows};`
  224. : `grid-template-rows: 44px max-content;`}
  225. `;
  226. const StyledAlert = styled(Alert)`
  227. border-radius: 0;
  228. border-width: 1px 0 0 0;
  229. grid-column: 1/-1;
  230. margin-bottom: 0;
  231. `;
  232. const Row = styled('div')<{
  233. isPlaying?: boolean;
  234. referrerLocation?: string;
  235. showCursor?: boolean;
  236. }>`
  237. ${p =>
  238. p.referrerLocation === 'replay'
  239. ? `display: contents;
  240. & > * {
  241. border-top: 1px solid ${p.theme.border};
  242. }`
  243. : `display: contents;
  244. & > * {
  245. background-color: ${p.isPlaying ? p.theme.translucentGray200 : 'inherit'};
  246. border-top: 1px solid ${p.theme.border};
  247. cursor: ${p.showCursor ? 'pointer' : 'default'};
  248. }
  249. :hover {
  250. background-color: ${p.showCursor ? p.theme.translucentInnerBorder : 'inherit'};
  251. }
  252. :active {
  253. background-color: ${p.theme.translucentGray200};
  254. }
  255. `}
  256. `;
  257. export default ReplayTable;