"use client"; import { useCallback, useEffect, useRef, useState } from "react"; import { buttonVariants } from "@/components/ui/button"; // --------------------------------------------------------------------------- // TWO TAKES IN STEP (the compare page, UI notes item 19): A leads, B follows. // One transport for both -- play, pause, the seek bar, ±5 s -- because two // players each with its own controls drift apart the moment one is touched. // While they play, B is pulled back to A whenever it strays past 0.15 s (a // seek into a file that is only partly loaded starts late). ONE AUDIO SOURCE, // as on the Takes page: A or B, the other muted. // --------------------------------------------------------------------------- type Side = { project: string; take: string; label: string; seconds: number | null; hasPreview: boolean }; const src = (s: Side) => `/api/report/takes/preview?${new URLSearchParams({ project: s.project, take: s.take })}`; const clock = (t: number) => { const s = Math.max(0, t); const m = Math.floor(s / 60); return `${m}:${(s - m * 60).toFixed(1).padStart(4, "0")}`; }; /** How far B may stray from A while playing before it is pulled back (s). */ const DRIFT = 0.15; export default function TakesComparePlayers({ a, b }: { a: Side; b: Side }) { const va = useRef(null); const vb = useRef(null); const [ready, setReady] = useState(false); const [playing, setPlaying] = useState(false); const [t, setT] = useState(0); const [drift, setDrift] = useState(0); const [sound, setSound] = useState<"a" | "b">("a"); const length = Math.max(a.seconds ?? 0, b.seconds ?? 0); const [dur, setDur] = useState(length); useEffect(() => setReady(true), []); useEffect(() => { if (va.current) va.current.muted = sound !== "a"; if (vb.current) vb.current.muted = sound !== "b"; }, [sound]); const both = () => [va.current, vb.current].filter((v): v is HTMLVideoElement => !!v); const seek = useCallback((to: number) => { const at = Math.max(0, to); for (const v of both()) v.currentTime = Math.min(at, Number.isFinite(v.duration) ? v.duration : at); setT(at); }, []); const play = useCallback(async () => { const lead = va.current ?? vb.current; if (!lead) return; const at = lead.currentTime; for (const v of both()) if (Math.abs(v.currentTime - at) > 0.01) v.currentTime = Math.min(at, Number.isFinite(v.duration) ? v.duration : at); await Promise.allSettled(both().map((v) => v.play())); setPlaying(true); }, []); const pause = useCallback(() => { for (const v of both()) v.pause(); setPlaying(false); const lead = va.current ?? vb.current; if (lead && vb.current && va.current) vb.current.currentTime = Math.min(lead.currentTime, Number.isFinite(vb.current.duration) ? vb.current.duration : lead.currentTime); }, []); // The clock and the leash, four times a second. useEffect(() => { const id = window.setInterval(() => { const x = va.current; const y = vb.current; const lead = x ?? y; if (!lead) return; setT(lead.currentTime); if (x && y) { const d = Math.abs(y.currentTime - x.currentTime); // B past its own end is not drift: it simply has less. const past = Number.isFinite(y.duration) && x.currentTime >= y.duration - 0.05; setDrift(past ? 0 : d); if (!x.paused && !past && d > DRIFT) y.currentTime = x.currentTime; } if (lead.ended || (x?.paused && y?.paused && playing)) setPlaying(false); }, 250); return () => window.clearInterval(id); }, [playing]); // The keys: space plays / pauses, ← → five seconds. useEffect(() => { const onKey = (e: KeyboardEvent) => { const el = e.target as HTMLElement | null; if (el?.closest?.("input, textarea, select, [contenteditable='true']")) return; if (e.key === " ") { e.preventDefault(); void (playing ? pause() : play()); } else if (e.key === "ArrowLeft") seek(t - 5); else if (e.key === "ArrowRight") seek(t + 5); }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, [playing, play, pause, seek, t]); const player = (s: Side, side: "a" | "b") => (
{side.toUpperCase()} {s.project.split("/").pop()} {s.take} {s.label}
{s.hasPreview ? (
); return (
{player(a, "a")} {player(b, "b")}
seek(Number(e.target.value))} className="min-w-[10rem] flex-1" /> {clock(t)} / {clock(dur)} sound {(["a", "b"] as const).map((s) => ( ))} space plays · ← → 5 s · B follows A
); }