"use client"; import { useCallback, useEffect, useState, useTransition } from "react"; import { useRouter } from "next/navigation"; import { Button } from "yt-dlp-transcript-common/components/ui/button"; import type { FrameReadingView } from "yt-dlp-transcript-common/controller/frameCorrections"; import { correctFrameReadingAction, frameReadingAction } from "../frameScanActions"; // A person corrects one frame reading (release 32): open the reading at a // time (or from an event's "Correct"), set a field to what the frame shows — // or set the whole reading aside as a misread — and the scan is derived again // at once with the person's values. The model's reading is kept as it was and // shown beside each field that differs. type Values = Record; function toText(v: unknown, kind: string): string { if (v === null || v === undefined) return ""; if (Array.isArray(v)) return v.join(kind === "lines" ? "\n" : " "); return String(v); } /** "1:02:03", "4:05" or "125" → seconds; null when it is none of them. */ export function parseClock(s: string): number | null { const parts = s.trim().split(":"); if (!parts.length || parts.length > 3 || parts.some((p) => !/^\d+(\.\d+)?$/.test(p))) return null; return parts.reduce((acc, p) => acc * 60 + Number(p), 0); } function clock(t: number): string { const s = Math.max(0, Math.round(t)); const h = Math.floor(s / 3600); const m = Math.floor((s % 3600) / 60); const sec = s % 60; return h > 0 ? `${h}:${String(m).padStart(2, "0")}:${String(sec).padStart(2, "0")}` : `${m}:${String(sec).padStart(2, "0")}`; } export function FrameReadingEditor({ slug, videoId, profile, initialT, }: { slug: string; videoId: string; profile: string; initialT: number | null; }) { const router = useRouter(); const [hydrated, setHydrated] = useState(false); const [at, setAt] = useState(initialT !== null ? clock(initialT) : ""); const [view, setView] = useState(null); const [values, setValues] = useState({}); const [note, setNote] = useState(""); const [status, setStatus] = useState(null); const [error, setError] = useState(null); const [pending, start] = useTransition(); const show = useCallback((v: FrameReadingView) => { setView(v); setAt(clock(v.t)); setValues(Object.fromEntries(v.fields.map((f) => [f.path, toText(f.value, f.kind)]))); setNote(v.correction?.note ?? ""); }, []); const open = useCallback( (t: number) => { setError(null); setStatus(null); start(async () => { const res = await frameReadingAction(slug, videoId, profile, t); if (!res.ok) setError(res.error); else show(res.view); }); }, [slug, videoId, profile, show], ); useEffect(() => { setHydrated(true); if (initialT !== null) open(initialT); }, [initialT, open]); const save = (req: { misread?: boolean; clear?: boolean }) => { if (!view) return; setError(null); setStatus(null); start(async () => { const set = req.clear || req.misread ? undefined : values; const res = await correctFrameReadingAction(slug, videoId, profile, { t: view.t, ...(set ? { set } : {}), ...(req.misread ? { misread: true } : {}), ...(req.clear ? { clear: true } : {}), ...(note.trim() ? { note } : {}), }); if (!res.ok) { setError(res.error); return; } show(res.view); const { before: b, after: a } = res.rederived; setStatus( `${res.correction ? (res.correction.misread ? "Set aside as a misread" : "Correction saved") : "No correction kept"}; ` + `derived again: events ${b.events} → ${a.events}, tournaments ${b.tournaments} → ${a.tournaments}`, ); router.refresh(); }); }; const disabled = !hydrated || pending; return (

Correct a reading

{ e.preventDefault(); const t = parseClock(at); if (t === null) setError("give a time: 1:02:03, 4:05 or seconds"); else open(t); }} > setAt(e.target.value)} aria-label="reading time" placeholder="h:mm:ss" className="rounded border border-border bg-card px-2 py-1 text-sm w-28 font-mono" /> {view && ( <> )}
{view && (

Reading at {clock(view.t)} {view.copyOf !== null && ` (a duplicate of the frame at ${clock(view.copyOf)})`} {view.misread && " · set aside as a misread"} {view.correction && !view.misread && " · corrected by a person"}

{view.fields.map((f) => (