index.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import {memo, useMemo, useRef} from 'react';
  2. import {
  3. AutoSizer,
  4. CellMeasurer,
  5. List as ReactVirtualizedList,
  6. ListRowProps,
  7. } from 'react-virtualized';
  8. import {useQuery} from '@tanstack/react-query';
  9. import Placeholder from 'sentry/components/placeholder';
  10. import {useReplayContext} from 'sentry/components/replays/replayContext';
  11. import {t} from 'sentry/locale';
  12. import useCrumbHandlers from 'sentry/utils/replays/hooks/useCrumbHandlers';
  13. import type ReplayReader from 'sentry/utils/replays/replayReader';
  14. import DomFilters from 'sentry/views/replays/detail/domMutations/domFilters';
  15. import DomMutationRow from 'sentry/views/replays/detail/domMutations/domMutationRow';
  16. import useDomFilters from 'sentry/views/replays/detail/domMutations/useDomFilters';
  17. import FluidHeight from 'sentry/views/replays/detail/layout/fluidHeight';
  18. import NoRowRenderer from 'sentry/views/replays/detail/noRowRenderer';
  19. import TabItemContainer from 'sentry/views/replays/detail/tabItemContainer';
  20. import useVirtualizedList from 'sentry/views/replays/detail/useVirtualizedList';
  21. type Props = {
  22. replay: null | ReplayReader;
  23. startTimestampMs: number;
  24. };
  25. // Ensure this object is created once as it is an input to
  26. // `useVirtualizedList`'s memoization
  27. const cellMeasurer = {
  28. fixedWidth: true,
  29. minHeight: 82,
  30. };
  31. function useExtractedDomNodes({replay}: {replay: null | ReplayReader}) {
  32. return useQuery(['getDomNodes', replay], () => replay?.getDomNodes() ?? [], {
  33. enabled: Boolean(replay),
  34. initialData: [],
  35. cacheTime: Infinity,
  36. });
  37. }
  38. function DomMutations({replay, startTimestampMs}: Props) {
  39. const {data: actions, isLoading} = useExtractedDomNodes({replay});
  40. const {currentTime, currentHoverTime} = useReplayContext();
  41. const {onMouseEnter, onMouseLeave, onClickTimestamp} = useCrumbHandlers();
  42. const filterProps = useDomFilters({actions: actions || []});
  43. const {items, setSearchTerm} = filterProps;
  44. const clearSearchTerm = () => setSearchTerm('');
  45. const listRef = useRef<ReactVirtualizedList>(null);
  46. const deps = useMemo(() => [items], [items]);
  47. const {cache, updateList} = useVirtualizedList({
  48. cellMeasurer,
  49. ref: listRef,
  50. deps,
  51. });
  52. const renderRow = ({index, key, style, parent}: ListRowProps) => {
  53. const mutation = items[index];
  54. return (
  55. <CellMeasurer
  56. cache={cache}
  57. columnIndex={0}
  58. key={key}
  59. parent={parent}
  60. rowIndex={index}
  61. >
  62. <DomMutationRow
  63. currentHoverTime={currentHoverTime}
  64. currentTime={currentTime}
  65. onMouseEnter={onMouseEnter}
  66. onMouseLeave={onMouseLeave}
  67. mutation={mutation}
  68. onClickTimestamp={onClickTimestamp}
  69. startTimestampMs={startTimestampMs}
  70. style={style}
  71. />
  72. </CellMeasurer>
  73. );
  74. };
  75. return (
  76. <FluidHeight>
  77. <DomFilters actions={actions} {...filterProps} />
  78. <TabItemContainer data-test-id="replay-details-dom-events-tab">
  79. {isLoading || !actions ? (
  80. <Placeholder height="100%" />
  81. ) : (
  82. <AutoSizer onResize={updateList}>
  83. {({width, height}) => (
  84. <ReactVirtualizedList
  85. deferredMeasurementCache={cache}
  86. height={height}
  87. noRowsRenderer={() => (
  88. <NoRowRenderer
  89. unfilteredItems={actions}
  90. clearSearchTerm={clearSearchTerm}
  91. >
  92. {t('No DOM events recorded')}
  93. </NoRowRenderer>
  94. )}
  95. overscanRowCount={5}
  96. ref={listRef}
  97. rowCount={items.length}
  98. rowHeight={cache.rowHeight}
  99. rowRenderer={renderRow}
  100. width={width}
  101. />
  102. )}
  103. </AutoSizer>
  104. )}
  105. </TabItemContainer>
  106. </FluidHeight>
  107. );
  108. }
  109. export default memo(DomMutations);