2026-01-31 00:20:04 +01:00
|
|
|
import { useEffect, useState } from "react";
|
2026-02-27 16:33:57 +01:00
|
|
|
import { timelineTimeToSnappedPixels } from "@/lib/timeline";
|
2026-03-25 16:47:22 +01:00
|
|
|
import { TRACK_LABELS_WIDTH_PX } from "@/constants/timeline-constants";
|
2026-01-31 00:20:04 +01:00
|
|
|
interface UseSnapIndicatorPositionParams {
|
|
|
|
|
snapPoint: { time: number } | null;
|
|
|
|
|
zoomLevel: number;
|
2026-02-01 11:06:20 +01:00
|
|
|
timelineRef: React.RefObject<HTMLDivElement | null>;
|
|
|
|
|
tracksScrollRef: React.RefObject<HTMLDivElement | null>;
|
2026-01-31 00:20:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface SnapIndicatorPosition {
|
|
|
|
|
leftPosition: number;
|
|
|
|
|
topPosition: number;
|
|
|
|
|
height: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useSnapIndicatorPosition({
|
|
|
|
|
snapPoint,
|
|
|
|
|
zoomLevel,
|
|
|
|
|
timelineRef,
|
|
|
|
|
tracksScrollRef,
|
|
|
|
|
}: UseSnapIndicatorPositionParams): SnapIndicatorPosition {
|
|
|
|
|
const [scrollLeft, setScrollLeft] = useState(0);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const tracksViewport = tracksScrollRef.current;
|
|
|
|
|
|
|
|
|
|
if (!tracksViewport) return;
|
|
|
|
|
|
|
|
|
|
const handleScroll = () => {
|
|
|
|
|
setScrollLeft(tracksViewport.scrollLeft);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
setScrollLeft(tracksViewport.scrollLeft);
|
|
|
|
|
|
|
|
|
|
tracksViewport.addEventListener("scroll", handleScroll);
|
|
|
|
|
return () => tracksViewport.removeEventListener("scroll", handleScroll);
|
|
|
|
|
}, [tracksScrollRef]);
|
|
|
|
|
|
|
|
|
|
const timelineContainerHeight = timelineRef.current?.offsetHeight || 400;
|
|
|
|
|
const totalHeight = timelineContainerHeight - 8; // 8px padding from edges
|
|
|
|
|
|
2026-02-27 16:33:57 +01:00
|
|
|
const timelinePosition = timelineTimeToSnappedPixels({
|
|
|
|
|
time: snapPoint?.time ?? 0,
|
|
|
|
|
zoomLevel,
|
|
|
|
|
});
|
2026-03-25 16:47:22 +01:00
|
|
|
const leftPosition = TRACK_LABELS_WIDTH_PX + timelinePosition - scrollLeft;
|
2026-01-31 00:20:04 +01:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
leftPosition,
|
|
|
|
|
topPosition: 0,
|
|
|
|
|
height: totalHeight,
|
|
|
|
|
};
|
|
|
|
|
}
|