index.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import {useCallback, useEffect, 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 {GridTable} from 'sentry/components/replays/virtualizedGrid/gridTable';
  9. import {OverflowHidden} from 'sentry/components/replays/virtualizedGrid/overflowHidden';
  10. import {SplitPanel} from 'sentry/components/replays/virtualizedGrid/splitPanel';
  11. import useDetailsSplit from 'sentry/components/replays/virtualizedGrid/useDetailsSplit';
  12. import {t} from 'sentry/locale';
  13. import {trackAnalytics} from 'sentry/utils/analytics';
  14. import useA11yData from 'sentry/utils/replays/hooks/useA11yData';
  15. import useCrumbHandlers from 'sentry/utils/replays/hooks/useCrumbHandlers';
  16. import useOrganization from 'sentry/utils/useOrganization';
  17. import AccessibilityFilters from 'sentry/views/replays/detail/accessibility/accessibilityFilters';
  18. import AccessibilityHeaderCell, {
  19. COLUMN_COUNT,
  20. } from 'sentry/views/replays/detail/accessibility/accessibilityHeaderCell';
  21. import AccessibilityRefetchBanner from 'sentry/views/replays/detail/accessibility/accessibilityRefetchBanner';
  22. import AccessibilityTableCell from 'sentry/views/replays/detail/accessibility/accessibilityTableCell';
  23. import AccessibilityDetails from 'sentry/views/replays/detail/accessibility/details';
  24. import useAccessibilityFilters from 'sentry/views/replays/detail/accessibility/useAccessibilityFilters';
  25. import useSortAccessibility from 'sentry/views/replays/detail/accessibility/useSortAccessibility';
  26. import FilterLoadingIndicator from 'sentry/views/replays/detail/filterLoadingIndicator';
  27. import FluidHeight from 'sentry/views/replays/detail/layout/fluidHeight';
  28. import NoRowRenderer from 'sentry/views/replays/detail/noRowRenderer';
  29. import useVirtualizedGrid from 'sentry/views/replays/detail/useVirtualizedGrid';
  30. const HEADER_HEIGHT = 25;
  31. const BODY_HEIGHT = 25;
  32. const RESIZEABLE_HANDLE_HEIGHT = 105;
  33. const cellMeasurer = {
  34. defaultHeight: BODY_HEIGHT,
  35. defaultWidth: 100,
  36. fixedHeight: true,
  37. };
  38. function AccessibilityList() {
  39. const organization = useOrganization();
  40. const {currentTime, currentHoverTime} = useReplayContext();
  41. const {onMouseEnter, onMouseLeave, onClickTimestamp} = useCrumbHandlers();
  42. const {
  43. dataOffsetMs,
  44. data: accessibilityData,
  45. isLoading,
  46. isRefetching,
  47. refetch,
  48. } = useA11yData();
  49. const [scrollToRow, setScrollToRow] = useState<undefined | number>(undefined);
  50. const filterProps = useAccessibilityFilters({
  51. accessibilityData: accessibilityData || [],
  52. });
  53. const {items: filteredItems, searchTerm, setSearchTerm} = filterProps;
  54. const clearSearchTerm = () => setSearchTerm('');
  55. const {handleSort, items, sortConfig} = useSortAccessibility({items: filteredItems});
  56. const containerRef = useRef<HTMLDivElement>(null);
  57. const gridRef = useRef<MultiGrid>(null);
  58. const deps = useMemo(() => [items, searchTerm], [items, searchTerm]);
  59. const {cache, getColumnWidth, onScrollbarPresenceChange, onWrapperResize} =
  60. useVirtualizedGrid({
  61. cellMeasurer,
  62. gridRef,
  63. columnCount: COLUMN_COUNT,
  64. dynamicColumnIndex: 2,
  65. deps,
  66. });
  67. const {
  68. onClickCell,
  69. onCloseDetailsSplit,
  70. resizableDrawerProps,
  71. selectedIndex,
  72. splitSize,
  73. } = useDetailsSplit({
  74. containerRef,
  75. handleHeight: RESIZEABLE_HANDLE_HEIGHT,
  76. frames: accessibilityData,
  77. urlParamName: 'a_detail_row',
  78. onShowDetails: useCallback(
  79. ({dataIndex, rowIndex}) => {
  80. setScrollToRow(rowIndex);
  81. const item = items[dataIndex];
  82. trackAnalytics('replay.accessibility-issue-clicked', {
  83. organization,
  84. issue_description: item.description,
  85. issue_impact: item.impact,
  86. });
  87. },
  88. [items, organization]
  89. ),
  90. });
  91. useEffect(() => {
  92. if (isRefetching) {
  93. onCloseDetailsSplit();
  94. }
  95. }, [isRefetching, onCloseDetailsSplit]);
  96. const {
  97. handleClick: onClickToJump,
  98. onSectionRendered,
  99. showJumpDownButton,
  100. showJumpUpButton,
  101. } = useJumpButtons({
  102. currentTime,
  103. frames: filteredItems,
  104. isTable: true,
  105. setScrollToRow,
  106. });
  107. const cellRenderer = ({columnIndex, rowIndex, key, style, parent}: GridCellProps) => {
  108. const a11yIssue = items[rowIndex - 1];
  109. return (
  110. <CellMeasurer
  111. cache={cache}
  112. columnIndex={columnIndex}
  113. key={key}
  114. parent={parent}
  115. rowIndex={rowIndex}
  116. >
  117. {({measure: _, registerChild}) =>
  118. rowIndex === 0 ? (
  119. <AccessibilityHeaderCell
  120. ref={e => e && registerChild?.(e)}
  121. handleSort={handleSort}
  122. index={columnIndex}
  123. sortConfig={sortConfig}
  124. style={{...style, height: HEADER_HEIGHT}}
  125. />
  126. ) : (
  127. <AccessibilityTableCell
  128. columnIndex={columnIndex}
  129. currentHoverTime={currentHoverTime}
  130. currentTime={currentTime}
  131. a11yIssue={a11yIssue}
  132. onMouseEnter={onMouseEnter}
  133. onMouseLeave={onMouseLeave}
  134. onClickCell={onClickCell}
  135. onClickTimestamp={onClickTimestamp}
  136. ref={e => e && registerChild?.(e)}
  137. rowIndex={rowIndex}
  138. sortConfig={sortConfig}
  139. style={{...style, height: BODY_HEIGHT}}
  140. />
  141. )
  142. }
  143. </CellMeasurer>
  144. );
  145. };
  146. return (
  147. <FluidHeight>
  148. <FilterLoadingIndicator isLoading={isLoading || isRefetching}>
  149. <AccessibilityFilters accessibilityData={accessibilityData} {...filterProps} />
  150. </FilterLoadingIndicator>
  151. <AccessibilityRefetchBanner initialOffsetMs={dataOffsetMs} refetch={refetch} />
  152. <StyledGridTable ref={containerRef} data-test-id="replay-details-accessibility-tab">
  153. <SplitPanel
  154. style={{
  155. gridTemplateRows: splitSize !== undefined ? `1fr auto ${splitSize}px` : '1fr',
  156. }}
  157. >
  158. {accessibilityData && !isRefetching ? (
  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={selectedIndex ? items[selectedIndex] : null}
  210. onClose={onCloseDetailsSplit}
  211. />
  212. </SplitPanel>
  213. </StyledGridTable>
  214. </FluidHeight>
  215. );
  216. }
  217. const StyledGridTable = styled(GridTable)`
  218. border-radius: ${p => p.theme.borderRadiusBottom};
  219. border-top: none;
  220. `;
  221. export default AccessibilityList;