useScrollToCurrentItem.tsx 1005 B

123456789101112131415161718192021222324252627282930313233
  1. import type {RefObject} from 'react';
  2. import {useEffect, useMemo} from 'react';
  3. import type {List as ReactVirtualizedList} from 'react-virtualized';
  4. import {useReplayContext} from 'sentry/components/replays/replayContext';
  5. import {getPrevReplayFrame} from 'sentry/utils/replays/getReplayEvent';
  6. import type {ReplayFrame} from 'sentry/utils/replays/types';
  7. interface Opts {
  8. frames: undefined | ReplayFrame[];
  9. ref: RefObject<ReactVirtualizedList>;
  10. }
  11. function useScrollToCurrentItem({frames, ref}: Opts) {
  12. const {currentTime} = useReplayContext();
  13. const currentItem = useMemo(
  14. () =>
  15. getPrevReplayFrame({
  16. frames: frames || [],
  17. targetOffsetMs: currentTime,
  18. }),
  19. [frames, currentTime]
  20. );
  21. useEffect(() => {
  22. if (ref.current && currentItem) {
  23. const index = frames?.findIndex(frame => frame === currentItem);
  24. ref.current?.scrollToRow(index ? index + 1 : undefined);
  25. }
  26. }, [frames, currentItem, ref]);
  27. }
  28. export default useScrollToCurrentItem;