useVirtualizedInspector.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import {MouseEvent, RefObject, useCallback} from 'react';
  2. import {CellMeasurerCache, List} from 'react-virtualized';
  3. import useVirtualListDimentionChange from 'sentry/views/replays/detail/useVirtualListDimentionChange';
  4. type Opts = {
  5. cache: CellMeasurerCache;
  6. expandPathsRef: RefObject<Map<number, Set<string>>>;
  7. listRef: RefObject<List>;
  8. };
  9. export type OnDimensionChange = (
  10. index: number,
  11. path: string,
  12. expandedState: Record<string, boolean>,
  13. event: MouseEvent<HTMLDivElement>
  14. ) => void;
  15. export default function useVirtualizedInspector({cache, listRef, expandPathsRef}: Opts) {
  16. const {handleDimensionChange} = useVirtualListDimentionChange({cache, listRef});
  17. return {
  18. expandPaths: expandPathsRef.current,
  19. handleDimensionChange: useCallback(
  20. (
  21. index: number,
  22. path: string,
  23. expandedState: Record<string, boolean>,
  24. event: MouseEvent<HTMLDivElement>
  25. ) => {
  26. const rowState = expandPathsRef.current?.get(index) || new Set();
  27. if (expandedState[path]) {
  28. rowState.add(path);
  29. } else {
  30. // Collapsed, i.e. its default state, so no need to store state
  31. rowState.delete(path);
  32. }
  33. expandPathsRef.current?.set(index, rowState);
  34. handleDimensionChange(index);
  35. event.stopPropagation();
  36. },
  37. [expandPathsRef, handleDimensionChange]
  38. ),
  39. };
  40. }