useVirtualizedInspector.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  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 default function useVirtualizedInspector({cache, listRef, expandPathsRef}: Opts) {
  10. const {handleDimensionChange} = useVirtualListDimentionChange({cache, listRef});
  11. return {
  12. expandPaths: expandPathsRef.current,
  13. handleDimensionChange: useCallback(
  14. (
  15. index: number,
  16. path: string,
  17. expandedState: Record<string, boolean>,
  18. event: MouseEvent<HTMLDivElement>
  19. ) => {
  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, event);
  29. },
  30. [expandPathsRef, handleDimensionChange]
  31. ),
  32. };
  33. }