index.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import {useCallback, useMemo, useRef, useState} from 'react';
  2. import {AutoSizer, CellMeasurer, GridCellProps, MultiGrid} from 'react-virtualized';
  3. import styled from '@emotion/styled';
  4. import Placeholder from 'sentry/components/placeholder';
  5. import JumpButtons from 'sentry/components/replays/jumpButtons';
  6. import {useReplayContext} from 'sentry/components/replays/replayContext';
  7. import useJumpButtons from 'sentry/components/replays/useJumpButtons';
  8. import {t} from 'sentry/locale';
  9. import useA11yData from 'sentry/utils/replays/hooks/useA11yData';
  10. import useCrumbHandlers from 'sentry/utils/replays/hooks/useCrumbHandlers';
  11. import {useResizableDrawer} from 'sentry/utils/useResizableDrawer';
  12. import useUrlParams from 'sentry/utils/useUrlParams';
  13. import AccessibilityFilters from 'sentry/views/replays/detail/accessibility/accessibilityFilters';
  14. import AccessibilityHeaderCell, {
  15. COLUMN_COUNT,
  16. } from 'sentry/views/replays/detail/accessibility/accessibilityHeaderCell';
  17. import AccessibilityTableCell from 'sentry/views/replays/detail/accessibility/accessibilityTableCell';
  18. // import AccessibilityDetails from 'sentry/views/replays/detail/accessibility/details';
  19. import useAccessibilityFilters from 'sentry/views/replays/detail/accessibility/useAccessibilityFilters';
  20. import useSortAccessibility from 'sentry/views/replays/detail/accessibility/useSortAccessibility';
  21. import FluidHeight from 'sentry/views/replays/detail/layout/fluidHeight';
  22. import NoRowRenderer from 'sentry/views/replays/detail/noRowRenderer';
  23. import useVirtualizedGrid from 'sentry/views/replays/detail/useVirtualizedGrid';
  24. const HEADER_HEIGHT = 25;
  25. const BODY_HEIGHT = 25;
  26. const RESIZEABLE_HANDLE_HEIGHT = 105;
  27. const cellMeasurer = {
  28. defaultHeight: BODY_HEIGHT,
  29. defaultWidth: 100,
  30. fixedHeight: true,
  31. };
  32. function AccessibilityList() {
  33. const {currentTime, currentHoverTime, replay} = useReplayContext();
  34. const {onMouseEnter, onMouseLeave, onClickTimestamp} = useCrumbHandlers();
  35. const accessibilityData = useA11yData();
  36. const startTimestampMs = replay?.getReplay()?.started_at?.getTime() || 0;
  37. const [scrollToRow, setScrollToRow] = useState<undefined | number>(undefined);
  38. const filterProps = useAccessibilityFilters({
  39. accessibilityData: accessibilityData || [],
  40. });
  41. const {items: filteredItems, searchTerm, setSearchTerm} = filterProps;
  42. const clearSearchTerm = () => setSearchTerm('');
  43. const {handleSort, items, sortConfig} = useSortAccessibility({items: filteredItems});
  44. const containerRef = useRef<HTMLDivElement>(null);
  45. const gridRef = useRef<MultiGrid>(null);
  46. const deps = useMemo(() => [items, searchTerm], [items, searchTerm]);
  47. const {cache, getColumnWidth, onScrollbarPresenceChange, onWrapperResize} =
  48. useVirtualizedGrid({
  49. cellMeasurer,
  50. gridRef,
  51. columnCount: COLUMN_COUNT,
  52. dynamicColumnIndex: 2,
  53. deps,
  54. });
  55. // `initialSize` cannot depend on containerRef because the ref starts as
  56. // `undefined` which then gets set into the hook and doesn't update.
  57. const initialSize = Math.max(150, window.innerHeight * 0.4);
  58. const {size: containerSize} = useResizableDrawer({
  59. direction: 'up',
  60. initialSize,
  61. min: 0,
  62. onResize: () => {},
  63. });
  64. const {getParamValue: getDetailRow, setParamValue: setDetailRow} = useUrlParams(
  65. 'a_detail_row',
  66. ''
  67. );
  68. const detailDataIndex = getDetailRow();
  69. const maxContainerHeight =
  70. (containerRef.current?.clientHeight || window.innerHeight) - RESIZEABLE_HANDLE_HEIGHT;
  71. const splitSize =
  72. accessibilityData && detailDataIndex
  73. ? Math.min(maxContainerHeight, containerSize)
  74. : undefined;
  75. const {
  76. handleClick: onClickToJump,
  77. onSectionRendered,
  78. showJumpDownButton,
  79. showJumpUpButton,
  80. } = useJumpButtons({
  81. currentTime,
  82. frames: filteredItems,
  83. isTable: true,
  84. setScrollToRow,
  85. });
  86. const onClickCell = useCallback(
  87. ({}: {dataIndex: number; rowIndex: number}) => {
  88. // eslint-disable-line
  89. // if (getDetailRow() === String(dataIndex)) {
  90. // setDetailRow('');
  91. // } else {
  92. // setDetailRow(String(dataIndex));
  93. // setScrollToRow(rowIndex);
  94. // }
  95. },
  96. // eslint-disable-next-line
  97. [getDetailRow, setDetailRow]
  98. );
  99. const cellRenderer = ({columnIndex, rowIndex, key, style, parent}: GridCellProps) => {
  100. const a11yIssue = items[rowIndex - 1];
  101. return (
  102. <CellMeasurer
  103. cache={cache}
  104. columnIndex={columnIndex}
  105. key={key}
  106. parent={parent}
  107. rowIndex={rowIndex}
  108. >
  109. {({
  110. measure: _,
  111. registerChild,
  112. }: {
  113. measure: () => void;
  114. registerChild?: (element?: Element) => void;
  115. }) =>
  116. rowIndex === 0 ? (
  117. <AccessibilityHeaderCell
  118. ref={e => e && registerChild?.(e)}
  119. handleSort={handleSort}
  120. index={columnIndex}
  121. sortConfig={sortConfig}
  122. style={{...style, height: HEADER_HEIGHT}}
  123. />
  124. ) : (
  125. <AccessibilityTableCell
  126. columnIndex={columnIndex}
  127. currentHoverTime={currentHoverTime}
  128. currentTime={currentTime}
  129. a11yIssue={a11yIssue}
  130. onMouseEnter={onMouseEnter}
  131. onMouseLeave={onMouseLeave}
  132. onClickCell={onClickCell}
  133. onClickTimestamp={onClickTimestamp}
  134. ref={e => e && registerChild?.(e)}
  135. rowIndex={rowIndex}
  136. sortConfig={sortConfig}
  137. startTimestampMs={startTimestampMs}
  138. style={{...style, height: BODY_HEIGHT}}
  139. />
  140. )
  141. }
  142. </CellMeasurer>
  143. );
  144. };
  145. return (
  146. <FluidHeight>
  147. <AccessibilityFilters accessibilityData={accessibilityData} {...filterProps} />
  148. <AccessibilityTable
  149. ref={containerRef}
  150. data-test-id="replay-details-accessibility-tab"
  151. >
  152. <SplitPanel
  153. style={{
  154. gridTemplateRows: splitSize !== undefined ? `1fr auto ${splitSize}px` : '1fr',
  155. }}
  156. >
  157. {accessibilityData ? (
  158. <OverflowHidden>
  159. <AutoSizer onResize={onWrapperResize}>
  160. {({height, width}) => (
  161. <MultiGrid
  162. ref={gridRef}
  163. cellRenderer={cellRenderer}
  164. columnCount={COLUMN_COUNT}
  165. columnWidth={getColumnWidth(width)}
  166. deferredMeasurementCache={cache}
  167. estimatedColumnSize={100}
  168. estimatedRowSize={BODY_HEIGHT}
  169. fixedRowCount={1}
  170. height={height}
  171. noContentRenderer={() => (
  172. <NoRowRenderer
  173. unfilteredItems={accessibilityData}
  174. clearSearchTerm={clearSearchTerm}
  175. >
  176. {t('No accessibility problems detected')}
  177. </NoRowRenderer>
  178. )}
  179. onScrollbarPresenceChange={onScrollbarPresenceChange}
  180. onScroll={() => {
  181. if (scrollToRow !== undefined) {
  182. setScrollToRow(undefined);
  183. }
  184. }}
  185. onSectionRendered={onSectionRendered}
  186. overscanColumnCount={COLUMN_COUNT}
  187. overscanRowCount={5}
  188. rowCount={items.length + 1}
  189. rowHeight={({index}) => (index === 0 ? HEADER_HEIGHT : BODY_HEIGHT)}
  190. scrollToRow={scrollToRow}
  191. width={width}
  192. />
  193. )}
  194. </AutoSizer>
  195. {sortConfig.by === 'timestamp' && items.length ? (
  196. <JumpButtons
  197. jump={showJumpUpButton ? 'up' : showJumpDownButton ? 'down' : undefined}
  198. onClick={onClickToJump}
  199. tableHeaderHeight={HEADER_HEIGHT}
  200. />
  201. ) : null}
  202. </OverflowHidden>
  203. ) : (
  204. <Placeholder height="100%" />
  205. )}
  206. {/* <AccessibilityDetails
  207. {...resizableDrawerProps}
  208. item={detailDataIndex ? items[detailDataIndex] : null}
  209. onClose={() => {
  210. setDetailRow('');
  211. }}
  212. projectId="1"
  213. startTimestampMs={startTimestampMs}
  214. /> */}
  215. </SplitPanel>
  216. </AccessibilityTable>
  217. </FluidHeight>
  218. );
  219. }
  220. const SplitPanel = styled('div')`
  221. width: 100%;
  222. height: 100%;
  223. position: relative;
  224. display: grid;
  225. overflow: auto;
  226. `;
  227. const OverflowHidden = styled('div')`
  228. position: relative;
  229. height: 100%;
  230. overflow: hidden;
  231. display: grid;
  232. `;
  233. const AccessibilityTable = styled(FluidHeight)`
  234. border: 1px solid ${p => p.theme.border};
  235. border-radius: ${p => p.theme.borderRadius};
  236. .beforeHoverTime + .afterHoverTime:before {
  237. border-top: 1px solid ${p => p.theme.purple200};
  238. content: '';
  239. left: 0;
  240. position: absolute;
  241. top: 0;
  242. width: 999999999%;
  243. }
  244. .beforeHoverTime:last-child:before {
  245. border-bottom: 1px solid ${p => p.theme.purple200};
  246. content: '';
  247. right: 0;
  248. position: absolute;
  249. bottom: 0;
  250. width: 999999999%;
  251. }
  252. .beforeCurrentTime + .afterCurrentTime:before {
  253. border-top: 1px solid ${p => p.theme.purple300};
  254. content: '';
  255. left: 0;
  256. position: absolute;
  257. top: 0;
  258. width: 999999999%;
  259. }
  260. .beforeCurrentTime:last-child:before {
  261. border-bottom: 1px solid ${p => p.theme.purple300};
  262. content: '';
  263. right: 0;
  264. position: absolute;
  265. bottom: 0;
  266. width: 999999999%;
  267. }
  268. `;
  269. export default AccessibilityList;