index.tsx 4.0 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 FilterLoadingIndicator from 'sentry/views/replays/detail/filterLoadingIndicator';
  18. import FluidHeight from 'sentry/views/replays/detail/layout/fluidHeight';
  19. import NoRowRenderer from 'sentry/views/replays/detail/noRowRenderer';
  20. import TabItemContainer from 'sentry/views/replays/detail/tabItemContainer';
  21. import useVirtualizedList from 'sentry/views/replays/detail/useVirtualizedList';
  22. // Ensure this object is created once as it is an input to
  23. // `useVirtualizedList`'s memoization
  24. const cellMeasurer = {
  25. fixedWidth: true,
  26. minHeight: 82,
  27. };
  28. function useExtractedDomNodes({replay}: {replay: null | ReplayReader}) {
  29. return useQuery(['getDomNodes', replay], () => replay?.getDomNodes() ?? [], {
  30. enabled: Boolean(replay),
  31. initialData: [],
  32. cacheTime: Infinity,
  33. });
  34. }
  35. function DomMutations() {
  36. const {currentTime, currentHoverTime, replay} = useReplayContext();
  37. const {data: actions, isFetching} = useExtractedDomNodes({replay});
  38. const {onMouseEnter, onMouseLeave, onClickTimestamp} = useCrumbHandlers();
  39. const startTimestampMs = replay?.getReplay()?.started_at?.getTime() ?? 0;
  40. const filterProps = useDomFilters({actions: actions || []});
  41. const {items, setSearchTerm} = filterProps;
  42. const clearSearchTerm = () => setSearchTerm('');
  43. const listRef = useRef<ReactVirtualizedList>(null);
  44. const deps = useMemo(() => [items], [items]);
  45. const {cache, updateList} = useVirtualizedList({
  46. cellMeasurer,
  47. ref: listRef,
  48. deps,
  49. });
  50. const renderRow = ({index, key, style, parent}: ListRowProps) => {
  51. const mutation = items[index];
  52. return (
  53. <CellMeasurer
  54. cache={cache}
  55. columnIndex={0}
  56. key={key}
  57. parent={parent}
  58. rowIndex={index}
  59. >
  60. <DomMutationRow
  61. currentHoverTime={currentHoverTime}
  62. currentTime={currentTime}
  63. onMouseEnter={onMouseEnter}
  64. onMouseLeave={onMouseLeave}
  65. mutation={mutation}
  66. onClickTimestamp={onClickTimestamp}
  67. startTimestampMs={startTimestampMs}
  68. style={style}
  69. />
  70. </CellMeasurer>
  71. );
  72. };
  73. return (
  74. <FluidHeight>
  75. <FilterLoadingIndicator isLoading={isFetching}>
  76. <DomFilters actions={actions} {...filterProps} />
  77. </FilterLoadingIndicator>
  78. <TabItemContainer data-test-id="replay-details-dom-events-tab">
  79. {isFetching || !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);