index.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import {useCallback, useEffect, useMemo, useRef, useState} from 'react';
  2. import type {GridCellProps} from 'react-virtualized';
  3. import {AutoSizer, CellMeasurer, MultiGrid} from 'react-virtualized';
  4. import styled from '@emotion/styled';
  5. import Placeholder from 'sentry/components/placeholder';
  6. import JumpButtons from 'sentry/components/replays/jumpButtons';
  7. import {useReplayContext} from 'sentry/components/replays/replayContext';
  8. import useJumpButtons from 'sentry/components/replays/useJumpButtons';
  9. import {GridTable} from 'sentry/components/replays/virtualizedGrid/gridTable';
  10. import {OverflowHidden} from 'sentry/components/replays/virtualizedGrid/overflowHidden';
  11. import {SplitPanel} from 'sentry/components/replays/virtualizedGrid/splitPanel';
  12. import useDetailsSplit from 'sentry/components/replays/virtualizedGrid/useDetailsSplit';
  13. import {t} from 'sentry/locale';
  14. import {trackAnalytics} from 'sentry/utils/analytics';
  15. import useA11yData from 'sentry/utils/replays/hooks/useA11yData';
  16. import useCrumbHandlers from 'sentry/utils/replays/hooks/useCrumbHandlers';
  17. import useOrganization from 'sentry/utils/useOrganization';
  18. import AccessibilityFilters from 'sentry/views/replays/detail/accessibility/accessibilityFilters';
  19. import AccessibilityHeaderCell, {
  20. COLUMN_COUNT,
  21. } from 'sentry/views/replays/detail/accessibility/accessibilityHeaderCell';
  22. import AccessibilityRefetchBanner from 'sentry/views/replays/detail/accessibility/accessibilityRefetchBanner';
  23. import AccessibilityTableCell from 'sentry/views/replays/detail/accessibility/accessibilityTableCell';
  24. import AccessibilityDetails from 'sentry/views/replays/detail/accessibility/details';
  25. import useAccessibilityFilters from 'sentry/views/replays/detail/accessibility/useAccessibilityFilters';
  26. import useSortAccessibility from 'sentry/views/replays/detail/accessibility/useSortAccessibility';
  27. import FilterLoadingIndicator from 'sentry/views/replays/detail/filterLoadingIndicator';
  28. import FluidHeight from 'sentry/views/replays/detail/layout/fluidHeight';
  29. import NoRowRenderer from 'sentry/views/replays/detail/noRowRenderer';
  30. import useVirtualizedGrid from 'sentry/views/replays/detail/useVirtualizedGrid';
  31. const HEADER_HEIGHT = 25;
  32. const BODY_HEIGHT = 25;
  33. const RESIZEABLE_HANDLE_HEIGHT = 105;
  34. const cellMeasurer = {
  35. defaultHeight: BODY_HEIGHT,
  36. defaultWidth: 100,
  37. fixedHeight: true,
  38. };
  39. function AccessibilityList() {
  40. const organization = useOrganization();
  41. const {currentTime, currentHoverTime} = useReplayContext();
  42. const {onMouseEnter, onMouseLeave, onClickTimestamp} = useCrumbHandlers();
  43. const {
  44. dataOffsetMs,
  45. data: accessibilityData,
  46. isLoading,
  47. isRefetching,
  48. refetch,
  49. } = useA11yData();
  50. const [scrollToRow, setScrollToRow] = useState<undefined | number>(undefined);
  51. const filterProps = useAccessibilityFilters({
  52. accessibilityData: accessibilityData || [],
  53. });
  54. const {items: filteredItems, searchTerm, setSearchTerm} = filterProps;
  55. const clearSearchTerm = () => setSearchTerm('');
  56. const {handleSort, items, sortConfig} = useSortAccessibility({items: filteredItems});
  57. const containerRef = useRef<HTMLDivElement>(null);
  58. const gridRef = useRef<MultiGrid>(null);
  59. const deps = useMemo(() => [items, searchTerm], [items, searchTerm]);
  60. const {cache, getColumnWidth, onScrollbarPresenceChange, onWrapperResize} =
  61. useVirtualizedGrid({
  62. cellMeasurer,
  63. gridRef,
  64. columnCount: COLUMN_COUNT,
  65. dynamicColumnIndex: 2,
  66. deps,
  67. });
  68. const {
  69. onClickCell,
  70. onCloseDetailsSplit,
  71. resizableDrawerProps,
  72. selectedIndex,
  73. splitSize,
  74. } = useDetailsSplit({
  75. containerRef,
  76. handleHeight: RESIZEABLE_HANDLE_HEIGHT,
  77. frames: accessibilityData,
  78. urlParamName: 'a_detail_row',
  79. onShowDetails: useCallback(
  80. ({dataIndex, rowIndex}) => {
  81. setScrollToRow(rowIndex);
  82. const item = items[dataIndex];
  83. trackAnalytics('replay.accessibility-issue-clicked', {
  84. organization,
  85. issue_description: item.description,
  86. issue_impact: item.impact,
  87. });
  88. },
  89. [items, organization]
  90. ),
  91. });
  92. useEffect(() => {
  93. if (isRefetching) {
  94. onCloseDetailsSplit();
  95. }
  96. }, [isRefetching, onCloseDetailsSplit]);
  97. const {
  98. handleClick: onClickToJump,
  99. onSectionRendered,
  100. showJumpDownButton,
  101. showJumpUpButton,
  102. } = useJumpButtons({
  103. currentTime,
  104. frames: filteredItems,
  105. isTable: true,
  106. setScrollToRow,
  107. });
  108. const cellRenderer = ({columnIndex, rowIndex, key, style, parent}: GridCellProps) => {
  109. const a11yIssue = items[rowIndex - 1];
  110. return (
  111. <CellMeasurer
  112. cache={cache}
  113. columnIndex={columnIndex}
  114. key={key}
  115. parent={parent}
  116. rowIndex={rowIndex}
  117. >
  118. {({measure: _, registerChild}) =>
  119. rowIndex === 0 ? (
  120. <AccessibilityHeaderCell
  121. ref={e => e && registerChild?.(e)}
  122. handleSort={handleSort}
  123. index={columnIndex}
  124. sortConfig={sortConfig}
  125. style={{...style, height: HEADER_HEIGHT}}
  126. />
  127. ) : (
  128. <AccessibilityTableCell
  129. columnIndex={columnIndex}
  130. currentHoverTime={currentHoverTime}
  131. currentTime={currentTime}
  132. a11yIssue={a11yIssue}
  133. onMouseEnter={onMouseEnter}
  134. onMouseLeave={onMouseLeave}
  135. onClickCell={onClickCell}
  136. onClickTimestamp={onClickTimestamp}
  137. ref={e => e && registerChild?.(e)}
  138. rowIndex={rowIndex}
  139. sortConfig={sortConfig}
  140. style={{...style, height: BODY_HEIGHT}}
  141. />
  142. )
  143. }
  144. </CellMeasurer>
  145. );
  146. };
  147. return (
  148. <FluidHeight>
  149. <FilterLoadingIndicator isLoading={isLoading || isRefetching}>
  150. <AccessibilityFilters accessibilityData={accessibilityData} {...filterProps} />
  151. </FilterLoadingIndicator>
  152. <AccessibilityRefetchBanner initialOffsetMs={dataOffsetMs} refetch={refetch} />
  153. <StyledGridTable ref={containerRef} data-test-id="replay-details-accessibility-tab">
  154. <SplitPanel
  155. style={{
  156. gridTemplateRows: splitSize !== undefined ? `1fr auto ${splitSize}px` : '1fr',
  157. }}
  158. >
  159. {accessibilityData && !isRefetching ? (
  160. <OverflowHidden>
  161. <AutoSizer onResize={onWrapperResize}>
  162. {({height, width}) => (
  163. <MultiGrid
  164. ref={gridRef}
  165. cellRenderer={cellRenderer}
  166. columnCount={COLUMN_COUNT}
  167. columnWidth={getColumnWidth(width)}
  168. deferredMeasurementCache={cache}
  169. estimatedColumnSize={100}
  170. estimatedRowSize={BODY_HEIGHT}
  171. fixedRowCount={1}
  172. height={height}
  173. noContentRenderer={() => (
  174. <NoRowRenderer
  175. unfilteredItems={accessibilityData}
  176. clearSearchTerm={clearSearchTerm}
  177. >
  178. {t('No accessibility problems detected')}
  179. </NoRowRenderer>
  180. )}
  181. onScrollbarPresenceChange={onScrollbarPresenceChange}
  182. onScroll={() => {
  183. if (scrollToRow !== undefined) {
  184. setScrollToRow(undefined);
  185. }
  186. }}
  187. onSectionRendered={onSectionRendered}
  188. overscanColumnCount={COLUMN_COUNT}
  189. overscanRowCount={5}
  190. rowCount={items.length + 1}
  191. rowHeight={({index}) => (index === 0 ? HEADER_HEIGHT : BODY_HEIGHT)}
  192. scrollToRow={scrollToRow}
  193. width={width}
  194. />
  195. )}
  196. </AutoSizer>
  197. {sortConfig.by === 'timestamp' && items.length ? (
  198. <JumpButtons
  199. jump={showJumpUpButton ? 'up' : showJumpDownButton ? 'down' : undefined}
  200. onClick={onClickToJump}
  201. tableHeaderHeight={HEADER_HEIGHT}
  202. />
  203. ) : null}
  204. </OverflowHidden>
  205. ) : (
  206. <Placeholder height="100%" />
  207. )}
  208. <AccessibilityDetails
  209. {...resizableDrawerProps}
  210. item={selectedIndex ? items[selectedIndex] : null}
  211. onClose={onCloseDetailsSplit}
  212. />
  213. </SplitPanel>
  214. </StyledGridTable>
  215. </FluidHeight>
  216. );
  217. }
  218. const StyledGridTable = styled(GridTable)`
  219. border-radius: ${p => p.theme.borderRadiusBottom};
  220. border-top: none;
  221. `;
  222. export default AccessibilityList;