index.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. >
  103. {replays?.map(
  104. (replay: ReplayListRecord | ReplayListRecordWithTx, index: number) => {
  105. return (
  106. <Row
  107. key={replay.id}
  108. isPlaying={index === selectedReplayIndex && referrerLocation !== 'replay'}
  109. >
  110. {visibleColumns.map(column => {
  111. switch (column) {
  112. case ReplayColumn.ACTIVITY:
  113. return (
  114. <ActivityCell
  115. key="activity"
  116. replay={replay}
  117. showDropdownFilters={showDropdownFilters}
  118. />
  119. );
  120. case ReplayColumn.BROWSER:
  121. return (
  122. <BrowserCell
  123. key="browser"
  124. replay={replay}
  125. showDropdownFilters={showDropdownFilters}
  126. />
  127. );
  128. case ReplayColumn.COUNT_DEAD_CLICKS:
  129. return (
  130. <DeadClickCountCell
  131. key="countDeadClicks"
  132. replay={replay}
  133. showDropdownFilters={showDropdownFilters}
  134. />
  135. );
  136. case ReplayColumn.COUNT_ERRORS:
  137. return (
  138. <ErrorCountCell
  139. key="countErrors"
  140. replay={replay}
  141. showDropdownFilters={showDropdownFilters}
  142. />
  143. );
  144. case ReplayColumn.COUNT_RAGE_CLICKS:
  145. return (
  146. <RageClickCountCell
  147. key="countRageClicks"
  148. replay={replay}
  149. showDropdownFilters={showDropdownFilters}
  150. />
  151. );
  152. case ReplayColumn.DURATION:
  153. return (
  154. <DurationCell
  155. key="duration"
  156. replay={replay}
  157. showDropdownFilters={showDropdownFilters}
  158. />
  159. );
  160. case ReplayColumn.OS:
  161. return (
  162. <OSCell
  163. key="os"
  164. replay={replay}
  165. showDropdownFilters={showDropdownFilters}
  166. />
  167. );
  168. case ReplayColumn.REPLAY:
  169. return (
  170. <ReplayCell
  171. key="session"
  172. replay={replay}
  173. eventView={eventView}
  174. organization={organization}
  175. referrer={referrer}
  176. referrer_table="main"
  177. />
  178. );
  179. case ReplayColumn.PLAY_PAUSE:
  180. return (
  181. <PlayPauseCell
  182. key="play"
  183. isSelected={selectedReplayIndex === index}
  184. handleClick={() => onClickPlay?.(index)}
  185. />
  186. );
  187. case ReplayColumn.SLOWEST_TRANSACTION:
  188. return (
  189. <TransactionCell
  190. key="slowestTransaction"
  191. replay={replay}
  192. organization={organization}
  193. />
  194. );
  195. default:
  196. return null;
  197. }
  198. })}
  199. </Row>
  200. );
  201. }
  202. )}
  203. </StyledPanelTable>
  204. );
  205. }
  206. );
  207. const StyledPanelTable = styled(PanelTable)<{
  208. visibleColumns: ReplayColumn[];
  209. gridRows?: string;
  210. }>`
  211. margin-bottom: 0;
  212. grid-template-columns: ${p =>
  213. p.visibleColumns
  214. .filter(Boolean)
  215. .map(column => (column === 'replay' ? 'minmax(100px, 1fr)' : 'max-content'))
  216. .join(' ')};
  217. ${props =>
  218. props.gridRows
  219. ? `grid-template-rows: ${props.gridRows};`
  220. : `grid-template-rows: 44px max-content;`}
  221. `;
  222. const StyledAlert = styled(Alert)`
  223. border-radius: 0;
  224. border-width: 1px 0 0 0;
  225. grid-column: 1/-1;
  226. margin-bottom: 0;
  227. `;
  228. const Row = styled('div')<{isPlaying?: boolean}>`
  229. display: contents;
  230. & > * {
  231. background-color: ${p => (p.isPlaying ? p.theme.translucentInnerBorder : 'inherit')};
  232. border-bottom: 1px solid ${p => p.theme.border};
  233. }
  234. `;
  235. export default ReplayTable;