useVirtualizedInspector.tsx 1.2 KB

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