useVirtualizedInspector.tsx 1.4 KB

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