scrubberMouseTracking.tsx 1023 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import {useCallback} from 'react';
  2. import {useReplayContext} from 'sentry/components/replays/replayContext';
  3. import useMouseTracking from 'sentry/utils/replays/hooks/useMouseTracking';
  4. type Props = {
  5. children: React.ReactNode;
  6. };
  7. function ScrubberMouseTracking({children}: Props) {
  8. const {duration = 0, setCurrentHoverTime} = useReplayContext();
  9. const handlePositionChange = useCallback(
  10. params => {
  11. if (!params || duration === undefined) {
  12. setCurrentHoverTime(undefined);
  13. return;
  14. }
  15. const {left, width} = params;
  16. if (left >= 0) {
  17. const percent = left / width;
  18. const time = percent * duration;
  19. setCurrentHoverTime(time);
  20. } else {
  21. setCurrentHoverTime(undefined);
  22. }
  23. },
  24. [duration, setCurrentHoverTime]
  25. );
  26. const mouseTrackingProps = useMouseTracking<HTMLDivElement>({
  27. onPositionChange: handlePositionChange,
  28. });
  29. return <div {...mouseTrackingProps}>{children}</div>;
  30. }
  31. export default ScrubberMouseTracking;