index.tsx 8.9 KB

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