useVirtualListDimentionChange.tsx 818 B

1234567891011121314151617181920212223242526272829
  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. listRef: RefObject<List>;
  7. };
  8. export type OnDimensionChange = OnExpandCallback extends (...a: infer U) => infer R
  9. ? (index: number, ...a: U) => R
  10. : never;
  11. export default function useVirtualListDimentionChange({cache, listRef}: Opts) {
  12. const handleDimensionChange = useCallback(
  13. (index: number, event: MouseEvent<HTMLDivElement>) => {
  14. cache.clear(index, 0);
  15. listRef.current?.recomputeGridSize({rowIndex: index});
  16. listRef.current?.forceUpdateGrid();
  17. event.stopPropagation();
  18. },
  19. [cache, listRef]
  20. );
  21. return {
  22. handleDimensionChange,
  23. };
  24. }