index.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. showDropdownFilters?: boolean;
  41. };
  42. // Memoizing this component to avoid unnecessary re-renders
  43. const ReplayTable = memo(
  44. ({
  45. fetchError,
  46. isFetching,
  47. replays,
  48. sort,
  49. visibleColumns,
  50. emptyMessage,
  51. gridRows,
  52. showDropdownFilters,
  53. onClickPlay,
  54. }: Props) => {
  55. const routes = useRoutes();
  56. const location = useLocation();
  57. const organization = useOrganization();
  58. // we may have a selected replay index in the URLs
  59. const urlParams = useUrlParams();
  60. const rawReplayIndex = urlParams.getParamValue('selected_replay_index');
  61. const selectedReplayIndex = parseInt(
  62. typeof rawReplayIndex === 'string' ? rawReplayIndex : '0',
  63. 10
  64. );
  65. const tableHeaders = visibleColumns
  66. .filter(Boolean)
  67. .map(column => <HeaderCell key={column} column={column} sort={sort} />);
  68. if (fetchError && !isFetching) {
  69. return (
  70. <StyledPanelTable
  71. headers={tableHeaders}
  72. isLoading={false}
  73. visibleColumns={visibleColumns}
  74. data-test-id="replay-table"
  75. gridRows={undefined}
  76. >
  77. <StyledAlert type="error" showIcon>
  78. {typeof fetchError === 'string'
  79. ? fetchError
  80. : t(
  81. 'Sorry, the list of replays could not be loaded. This could be due to invalid search parameters or an internal systems error.'
  82. )}
  83. </StyledAlert>
  84. </StyledPanelTable>
  85. );
  86. }
  87. const referrer = getRouteStringFromRoutes(routes);
  88. const eventView = EventView.fromLocation(location);
  89. return (
  90. <StyledPanelTable
  91. headers={tableHeaders}
  92. isEmpty={replays?.length === 0}
  93. isLoading={isFetching}
  94. visibleColumns={visibleColumns}
  95. disablePadding
  96. data-test-id="replay-table"
  97. emptyMessage={emptyMessage}
  98. gridRows={isFetching ? undefined : gridRows}
  99. loader={<LoadingIndicator style={{margin: '54px auto'}} />}
  100. >
  101. {replays?.map(
  102. (replay: ReplayListRecord | ReplayListRecordWithTx, index: number) => {
  103. return (
  104. <Row key={replay.id} isPlaying={index === selectedReplayIndex}>
  105. {visibleColumns.map(column => {
  106. switch (column) {
  107. case ReplayColumn.ACTIVITY:
  108. return (
  109. <ActivityCell
  110. key="activity"
  111. replay={replay}
  112. showDropdownFilters={showDropdownFilters}
  113. />
  114. );
  115. case ReplayColumn.BROWSER:
  116. return (
  117. <BrowserCell
  118. key="browser"
  119. replay={replay}
  120. showDropdownFilters={showDropdownFilters}
  121. />
  122. );
  123. case ReplayColumn.COUNT_DEAD_CLICKS:
  124. return (
  125. <DeadClickCountCell
  126. key="countDeadClicks"
  127. replay={replay}
  128. showDropdownFilters={showDropdownFilters}
  129. />
  130. );
  131. case ReplayColumn.COUNT_ERRORS:
  132. return (
  133. <ErrorCountCell
  134. key="countErrors"
  135. replay={replay}
  136. showDropdownFilters={showDropdownFilters}
  137. />
  138. );
  139. case ReplayColumn.COUNT_RAGE_CLICKS:
  140. return (
  141. <RageClickCountCell
  142. key="countRageClicks"
  143. replay={replay}
  144. showDropdownFilters={showDropdownFilters}
  145. />
  146. );
  147. case ReplayColumn.DURATION:
  148. return (
  149. <DurationCell
  150. key="duration"
  151. replay={replay}
  152. showDropdownFilters={showDropdownFilters}
  153. />
  154. );
  155. case ReplayColumn.OS:
  156. return (
  157. <OSCell
  158. key="os"
  159. replay={replay}
  160. showDropdownFilters={showDropdownFilters}
  161. />
  162. );
  163. case ReplayColumn.REPLAY:
  164. return (
  165. <ReplayCell
  166. key="session"
  167. replay={replay}
  168. eventView={eventView}
  169. organization={organization}
  170. referrer={referrer}
  171. referrer_table="main"
  172. />
  173. );
  174. case ReplayColumn.PLAY_PAUSE:
  175. return (
  176. <PlayPauseCell
  177. key="play"
  178. isSelected={selectedReplayIndex === index}
  179. handleClick={() => onClickPlay?.(index)}
  180. />
  181. );
  182. case ReplayColumn.SLOWEST_TRANSACTION:
  183. return (
  184. <TransactionCell
  185. key="slowestTransaction"
  186. replay={replay}
  187. organization={organization}
  188. />
  189. );
  190. default:
  191. return null;
  192. }
  193. })}
  194. </Row>
  195. );
  196. }
  197. )}
  198. </StyledPanelTable>
  199. );
  200. }
  201. );
  202. const StyledPanelTable = styled(PanelTable)<{
  203. visibleColumns: ReplayColumn[];
  204. gridRows?: string;
  205. }>`
  206. margin-bottom: 0;
  207. grid-template-columns: ${p =>
  208. p.visibleColumns
  209. .filter(Boolean)
  210. .map(column => (column === 'replay' ? 'minmax(100px, 1fr)' : 'max-content'))
  211. .join(' ')};
  212. ${props =>
  213. props.gridRows
  214. ? `grid-template-rows: ${props.gridRows};`
  215. : `grid-template-rows: 44px max-content;`}
  216. `;
  217. const StyledAlert = styled(Alert)`
  218. border-radius: 0;
  219. border-width: 1px 0 0 0;
  220. grid-column: 1/-1;
  221. margin-bottom: 0;
  222. `;
  223. const Row = styled('div')<{isPlaying?: boolean}>`
  224. display: contents;
  225. & > * {
  226. background-color: ${p => (p.isPlaying ? p.theme.translucentInnerBorder : 'inherit')};
  227. border-bottom: 1px solid ${p => p.theme.border};
  228. }
  229. `;
  230. export default ReplayTable;