useScrollToCurrentItem.tsx 978 B

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