index.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 FilterLoadingIndicator from 'sentry/views/replays/detail/filterLoadingIndicator';
  22. import FluidHeight from 'sentry/views/replays/detail/layout/fluidHeight';
  23. import NoRowRenderer from 'sentry/views/replays/detail/noRowRenderer';
  24. import useVirtualizedGrid from 'sentry/views/replays/detail/useVirtualizedGrid';
  25. const HEADER_HEIGHT = 25;
  26. const BODY_HEIGHT = 25;
  27. const RESIZEABLE_HANDLE_HEIGHT = 105;
  28. const cellMeasurer = {
  29. defaultHeight: BODY_HEIGHT,
  30. defaultWidth: 100,
  31. fixedHeight: true,
  32. };
  33. function AccessibilityList() {
  34. const {currentTime, currentHoverTime} = useReplayContext();
  35. const {onMouseEnter, onMouseLeave, onClickTimestamp} = useCrumbHandlers();
  36. const {data: accessibilityData, isLoading} = useA11yData();
  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, ...resizableDrawerProps} = 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, rowIndex}: {dataIndex: number; rowIndex: number}) => {
  88. if (getDetailRow() === String(dataIndex)) {
  89. setDetailRow('');
  90. } else {
  91. setDetailRow(String(dataIndex));
  92. setScrollToRow(rowIndex);
  93. }
  94. },
  95. [getDetailRow, setDetailRow]
  96. );
  97. const cellRenderer = ({columnIndex, rowIndex, key, style, parent}: GridCellProps) => {
  98. const a11yIssue = items[rowIndex - 1];
  99. return (
  100. <CellMeasurer
  101. cache={cache}
  102. columnIndex={columnIndex}
  103. key={key}
  104. parent={parent}
  105. rowIndex={rowIndex}
  106. >
  107. {({
  108. measure: _,
  109. registerChild,
  110. }: {
  111. measure: () => void;
  112. registerChild?: (element?: Element) => void;
  113. }) =>
  114. rowIndex === 0 ? (
  115. <AccessibilityHeaderCell
  116. ref={e => e && registerChild?.(e)}
  117. handleSort={handleSort}
  118. index={columnIndex}
  119. sortConfig={sortConfig}
  120. style={{...style, height: HEADER_HEIGHT}}
  121. />
  122. ) : (
  123. <AccessibilityTableCell
  124. columnIndex={columnIndex}
  125. currentHoverTime={currentHoverTime}
  126. currentTime={currentTime}
  127. a11yIssue={a11yIssue}
  128. onMouseEnter={onMouseEnter}
  129. onMouseLeave={onMouseLeave}
  130. onClickCell={onClickCell}
  131. onClickTimestamp={onClickTimestamp}
  132. ref={e => e && registerChild?.(e)}
  133. rowIndex={rowIndex}
  134. sortConfig={sortConfig}
  135. style={{...style, height: BODY_HEIGHT}}
  136. />
  137. )
  138. }
  139. </CellMeasurer>
  140. );
  141. };
  142. return (
  143. <FluidHeight>
  144. <FilterLoadingIndicator isLoading={isLoading}>
  145. <AccessibilityFilters accessibilityData={accessibilityData} {...filterProps} />
  146. </FilterLoadingIndicator>
  147. <AccessibilityTable
  148. ref={containerRef}
  149. data-test-id="replay-details-accessibility-tab"
  150. >
  151. <SplitPanel
  152. style={{
  153. gridTemplateRows: splitSize !== undefined ? `1fr auto ${splitSize}px` : '1fr',
  154. }}
  155. >
  156. {accessibilityData ? (
  157. <OverflowHidden>
  158. <AutoSizer onResize={onWrapperResize}>
  159. {({height, width}) => (
  160. <MultiGrid
  161. ref={gridRef}
  162. cellRenderer={cellRenderer}
  163. columnCount={COLUMN_COUNT}
  164. columnWidth={getColumnWidth(width)}
  165. deferredMeasurementCache={cache}
  166. estimatedColumnSize={100}
  167. estimatedRowSize={BODY_HEIGHT}
  168. fixedRowCount={1}
  169. height={height}
  170. noContentRenderer={() => (
  171. <NoRowRenderer
  172. unfilteredItems={accessibilityData}
  173. clearSearchTerm={clearSearchTerm}
  174. >
  175. {t('No accessibility problems detected')}
  176. </NoRowRenderer>
  177. )}
  178. onScrollbarPresenceChange={onScrollbarPresenceChange}
  179. onScroll={() => {
  180. if (scrollToRow !== undefined) {
  181. setScrollToRow(undefined);
  182. }
  183. }}
  184. onSectionRendered={onSectionRendered}
  185. overscanColumnCount={COLUMN_COUNT}
  186. overscanRowCount={5}
  187. rowCount={items.length + 1}
  188. rowHeight={({index}) => (index === 0 ? HEADER_HEIGHT : BODY_HEIGHT)}
  189. scrollToRow={scrollToRow}
  190. width={width}
  191. />
  192. )}
  193. </AutoSizer>
  194. {sortConfig.by === 'timestamp' && items.length ? (
  195. <JumpButtons
  196. jump={showJumpUpButton ? 'up' : showJumpDownButton ? 'down' : undefined}
  197. onClick={onClickToJump}
  198. tableHeaderHeight={HEADER_HEIGHT}
  199. />
  200. ) : null}
  201. </OverflowHidden>
  202. ) : (
  203. <Placeholder height="100%" />
  204. )}
  205. <AccessibilityDetails
  206. {...resizableDrawerProps}
  207. item={detailDataIndex ? items[detailDataIndex] : null}
  208. onClose={() => {
  209. setDetailRow('');
  210. }}
  211. />
  212. </SplitPanel>
  213. </AccessibilityTable>
  214. </FluidHeight>
  215. );
  216. }
  217. const SplitPanel = styled('div')`
  218. width: 100%;
  219. height: 100%;
  220. position: relative;
  221. display: grid;
  222. overflow: auto;
  223. `;
  224. const OverflowHidden = styled('div')`
  225. position: relative;
  226. height: 100%;
  227. overflow: hidden;
  228. display: grid;
  229. `;
  230. const AccessibilityTable = styled(FluidHeight)`
  231. border: 1px solid ${p => p.theme.border};
  232. border-radius: ${p => p.theme.borderRadius};
  233. .beforeHoverTime + .afterHoverTime:before {
  234. border-top: 1px solid ${p => p.theme.purple200};
  235. content: '';
  236. left: 0;
  237. position: absolute;
  238. top: 0;
  239. width: 999999999%;
  240. }
  241. .beforeHoverTime:last-child:before {
  242. border-bottom: 1px solid ${p => p.theme.purple200};
  243. content: '';
  244. right: 0;
  245. position: absolute;
  246. bottom: 0;
  247. width: 999999999%;
  248. }
  249. .beforeCurrentTime + .afterCurrentTime:before {
  250. border-top: 1px solid ${p => p.theme.purple300};
  251. content: '';
  252. left: 0;
  253. position: absolute;
  254. top: 0;
  255. width: 999999999%;
  256. }
  257. .beforeCurrentTime:last-child:before {
  258. border-bottom: 1px solid ${p => p.theme.purple300};
  259. content: '';
  260. right: 0;
  261. position: absolute;
  262. bottom: 0;
  263. width: 999999999%;
  264. }
  265. `;
  266. export default AccessibilityList;