"use client";
import Link from "next/link";
import { useState } from "react";
import { buttonVariants } from "@/components/ui/button";
import { reelWalkHref } from "@/lib/hrefs";
import { cn } from "@/lib/utils";
import { STATE_LABEL, r2, reelWrite, sourceClock, type ReelRow, type ReelView } from "./reelTypes";
// ---------------------------------------------------------------------------
// WHAT THE REEL WAS CUT AGAINST (lib/report/reelAnchor.mjs): the reel's line
// ("cut against draft-6; take draft-7 changes beats 7") with the filter that
// walks only the changed beats, and per beat its state, why, and the fix --
// clamp a clipped beat into its clip's new limits, remove or re-point an
// orphan, or anchor a beat to this take once it has been looked at. Every
// fix is one write through the beats' writer, an undo point, and in the
// reel's history.
// ---------------------------------------------------------------------------
export function SourceBanner({ view, beat, changed }: { view: ReelView; beat: number; changed: boolean }) {
const st = view.status;
if (!st || !view.take) return null;
const n = st.changed.length;
return (
{st.line}
{n > 0 &&
(changed ? (
Walk every beat
) : (
Walk only the changed beats ({n})
))}
);
}
export function BeatStatusBar({ view, row, clips, onDone }: { view: ReelView; row: ReelRow; clips: string[]; onDone: () => void }) {
const st = row.status;
const [busy, setBusy] = useState(false);
const [said, setSaid] = useState(null);
const [to, setTo] = useState("");
if (!st || !view.take) return null;
const take = view.take.id;
const run = async (op: string, args: Record, done: string) => {
setBusy(true);
try {
const r = await reelWrite(view.project, view.reel, view.token, op, { ...args, take });
setSaid(r.ok ? done : r.error);
if (r.ok) onDone();
} finally {
setBusy(false);
}
};
const now = st.now as { start: number; end: number } | null | undefined;
const was = st.was as { start: number; end: number } | null | undefined;
const b = row.beat as { start?: number; end?: number; gaps?: { from: number; to: number }[] };
const clamp =
st.state === "clipped" && now && b.start !== undefined && b.end !== undefined
? (() => {
const start = r2(Math.max(b.start, now.start));
const end = r2(Math.min(b.end, now.end));
return end - start >= 0.2 ? { start, end, ...(b.gaps ? { gaps: b.gaps.filter((g) => g.from >= start + 0.5 && g.to <= end - 0.5) } : {}) } : null;
})()
: null;
const quiet = st.state === "unchanged" || st.state === "rerendered";
return (