useVirtualizedInspector.tsx 1.3 KB

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