index.tsx 9.6 KB

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