"use client"; import { useEffect, useRef, useState } from "react"; import { MARKER_LIMITS } from "umtool-report-to-video/marker-limits"; import { buttonVariants } from "@/components/ui/button"; // THE DECK'S MARKERS (video knobs, plans/video-knobs-ui.md): the flags on the // pip track, `render.chrome.deck.markers` -- each after a clip (a picker of the // cut's clips), a label of at most 24 characters, a colour (a palette key or // #hex), an alignment, a level (a section, or a subsection of the one before // it) -- at most MARKER_LIMITS.count (marker-limits.mjs, the build's own // number), added, removed and re-ordered here; and // `deck.markerLayout`, what the deck does when labels crowd: stagger them on a // second row, or truncate. One save writes the list and the layout and nothing // else of the deck (PUT /api/report/chrome { markers, markerLayout }), the // writer `umtool look deck.markers=…` uses. // `label` is optional: a marker without one is a tick at its boundary (and, // with `pip.color: "section"`, where the rail's colour changes). `level: 2` is // a subsection: a section to every edit, drawn inside its parent on the rail // (a shorter tick, its parent's colour, a quieter label; "Section › Sub" in // the section title). The cut's first marker must be a section. export type Marker = { after: string; label?: string; color?: string; align?: string; level?: number }; type Row = { after: string; label: string; color: string; align: string; level: 1 | 2 }; export const MARKER_MAX: number = MARKER_LIMITS.count; export const MARKER_LABEL_MAX: number = MARKER_LIMITS.label; const PALETTE = ["amber", "accent", "fg", "muted", "dim"]; const input = "rounded border border-[var(--color-line)] bg-[var(--color-ink)] px-1.5 py-0.5 text-[11px] text-[var(--color-text)] outline-none focus:border-[var(--color-sel)]"; const rowOf = (m: Marker): Row => ({ after: m.after, label: m.label ?? "", color: m.color ?? "", align: m.align ?? "auto", level: m.level === 2 ? 2 : 1 }); const markerOf = (r: Row): Marker => ({ after: r.after, ...(r.label.trim() ? { label: r.label.trim() } : {}), ...(r.color.trim() ? { color: r.color.trim() } : {}), ...(r.align && r.align !== "auto" ? { align: r.align } : {}), ...(r.level === 2 ? { level: 2 } : {}), }); /** The row whose marker comes first in the cut (by its clip's place, then the list's order), or -1. */ function firstInCut(rows: Row[], clips: { id: string }[]): number { const pos = new Map(clips.map((c, i) => [c.id, i])); let best = -1; rows.forEach((r, i) => { const p = pos.get(r.after); if (p === undefined) return; if (best < 0 || p < (pos.get(rows[best].after) as number)) best = i; }); return best; } export default function MarkersEditor({ markers, markerLayout, clips, aligns, layouts, busy, save, }: { markers: Marker[]; markerLayout: string | null; clips: { id: string; label: string }[]; aligns: string[]; layouts: string[]; busy: boolean; save: (patch: { markers: Marker[]; markerLayout: string | null }) => Promise; }) { const savedKey = JSON.stringify([markers, markerLayout]); const [rows, setRows] = useState(() => markers.map(rowOf)); const [layout, setLayout] = useState(markerLayout ?? ""); const [dirty, setDirty] = useState(false); // An edit made while a save re-reads the deck is kept (DeckClaimsEditor.tsx, the same rule): // edits are counted, and the saved value replaces the rows only when it accounts for all of them. const edits = useRef(0); const saved = useRef(0); useEffect(() => { // The saved value, by its text: a re-read with the same markers keeps the edits. if (edits.current !== saved.current) return; setRows(markers.map(rowOf)); setLayout(markerLayout ?? ""); setDirty(false); // eslint-disable-next-line react-hooks/exhaustive-deps }, [savedKey]); const edited = () => { edits.current += 1; setDirty(true); }; const change = (next: Row[]) => { setRows(next); edited(); }; const onSave = () => { const at = edits.current; saved.current = at; void save({ markers: rows.map(markerOf), markerLayout: layout || null }).then((ok) => { if (!ok) saved.current = -1; else if (edits.current === at) setDirty(false); }); }; const put = (i: number, patch: Partial) => change(rows.map((r, j) => (j === i ? { ...r, ...patch } : r))); const move = (i: number, d: number) => { const j = i + d; if (j < 0 || j >= rows.length) return; const next = [...rows]; [next[i], next[j]] = [next[j], next[i]]; change(next); }; const add = () => { const used = new Set(rows.map((r) => r.after)); const after = clips.find((c) => !used.has(c.id))?.id ?? clips[0]?.id ?? ""; change([...rows, { after, label: "", color: "", align: "auto", level: 1 }]); }; const labelLong = rows.some((r) => [...r.label.trim()].length > MARKER_LABEL_MAX); // A subsection sits inside the section before it: the cut's first marker // must open a section. The build refuses it too (validateDeckMarkers); said // here before a save. const lead = firstInCut(rows, clips); const leadSub = lead >= 0 && rows[lead].level === 2 ? lead : -1; return (
markers {rows.length} of {MARKER_MAX} {dirty && unsaved}
{leadSub >= 0 && (

Marker {leadSub + 1} (after {rows[leadSub].after}) is the cut's first, and a subsection needs a section before it: make it a section, or put a section before it.

)} {rows.length === 0 ? (

No markers: the pip track has no flags.

) : (
    {rows.map((r, i) => { const n = [...r.label.trim()].length; return (
  1. {i + 1} {r.level === 2 && } put(i, { label: e.target.value })} className={`${input} w-44`} /> MARKER_LABEL_MAX ? "text-[var(--color-bad)]" : ""}`}> {n}/{MARKER_LABEL_MAX} put(i, { color: e.target.value })} className={`${input} w-20 font-mono`} />
  2. ); })}
)} {PALETTE.map((p) => (
); }