"use client";
import { useCallback, useEffect, useState } from "react";
import { buttonVariants } from "@/components/ui/button";
import { Field, Input, Select } from "@/components/ui/field";
import { Panel } from "@/components/ui/panel";
import { ROOM, ROOM_BOUNDS, SLIDE_BOUNDS, SLIDE_DEFAULTS } from "yt-dlp-transcript-common/lib/report/look.mjs";
import AssistBox from "./AssistBox";
// THE LOOK (release 36 lane C): the render keys a person sets for a cut with
// no deck -- the header, the timeline footer, the QR, the crossfade's length
// and style, the loudness target, the speaker captions, room at the clips'
// edges (release 36 lane E) -- written through
// /api/report/render (lib/report/manifest.mjs updateRender, the build's own
// checks). Nothing here calls a model. A deck cut keeps its own settings
// (the Media tab); a montage levels with render.montage.
// The blurred clips (render.blur): "uncensored" renders every clip's picture
// as recorded -- the other version of a cut whose clips ask for a blur, kept
// as a take (`umtool take
--id uncensored --set render.blur=off`).
// Release 38 lane E: the blur slide's knobs (travel, smear, grain) and the
// room's numbers (how far the tail may run, the silence that ends it) are
// fields too, so every op the video assistant can propose is one here; the
// assistant's Ask box (AssistBox: optional, its proposals applied only on
// Apply) and Preview join sit under the fields.
// The brand intro (render.intro, report-to-video intro.mjs): the sting the
// cut opens with, by style; "none" is no intro. An intro written with its
// other knobs (ground, sound, seconds) keeps them when the style changes.
// The OUTPUT MODE (render.mode, lane output-modes): **Basic | Deck** -- basic is
// the footage alone (report-to-video basic.mjs: no deck, chrome, intro, QR,
// cards or brand; post items and teasers left out), deck everything else. The
// switch shows the mode as the build resolves it (over the cut's look preset)
// and saves with the rest (`umtool look
mode=basic|deck`).
// CHANNEL NAMES (render.channelNames, lane channel-title): what every clip of a
// channel is called on screen in this cut, one field per channel its clips are
// from -- empty is the record's own name (shown as the placeholder; two when the
// recordings disagree). A clip's own channel field (the clip bench) still wins.
// `umtool look
channelNames.="…"` (`channelNames.=` clears it).
type Look = {
headerHeight?: number;
footerHeight?: number;
qr?: unknown;
transition?: number;
transitionStyle?: string;
loudness?: { target: number } | null;
captions?: { style: "speakers"; labels?: { host?: string | null; subject?: string | null } | null; size?: "m" | "l" } | null;
room?: { tail?: boolean; head?: boolean; minGap?: number; maxS?: number } | null;
slide?: { travel?: number; blur?: number; grain?: number } | null;
blur?: string;
intro?: string | { style?: string; [k: string]: unknown } | null;
promo?: Promo | null;
zooms?: { soften?: number } | null;
desktop?: { tag?: boolean; qr?: boolean | { corner?: string; size?: number; links?: string }; captions?: { size?: string; lines?: number }; [k: string]: unknown } | null;
channelNames?: Record | null;
};
/** A channel the cut's clips are from (the render route's `channels`). */
type CutChannel = { slug: string; clips: number; recordNames: string[]; name: string | null };
type Mode = "deck" | "basic";
/** The channel promo (render.promo, report-to-video promo-card.mjs): the card a per-clip build ends on. */
type Promo = { name?: string | string[]; url?: string; qr?: string | false; portrait?: { file: string; box?: number[] }; kicker?: string; palette?: Record; lead?: number; fade?: number; hold?: number; captions?: boolean; [k: string]: unknown };
const PROMO_DEFAULTS = { lead: 4.5, fade: 0.7, hold: 1.5 } as const;
type View = { look: Look; mode?: Mode; preset?: string | null; deck: boolean; montage: boolean; styles: string[]; introStyles?: string[]; errors: string[]; token: string | null; channels?: CutChannel[]; warnings?: string[] };
/** The intro's style as written: a bare name or the object's `style`; "" for none. */
const introStyleOf = (v: Look["intro"]) => (typeof v === "string" ? v : v && typeof v === "object" ? String(v.style ?? "") : "");
const STYLE_WORDS: Record = { fade: "crossfade", fadeblack: "dip to black", fadewhite: "dip to white", dissolve: "dissolve", blurslide: "blur slide" };
type Draft = {
header: boolean;
footer: boolean;
qr: boolean;
qrCorner: string;
qrLinks: string;
transition: string;
style: string;
loudness: "off" | "-16" | "custom";
target: string;
captions: boolean;
host: string;
subject: string;
size: "m" | "l";
roomTail: boolean;
roomHead: boolean;
roomMaxS: string;
roomMinGap: string;
travel: string;
smear: string;
grain: string;
blur: "on" | "off";
intro: string;
mode: Mode;
deskTag: boolean;
deskQr: string;
deskSize: string;
promoOn: boolean;
promoName1: string;
promoName2: string;
promoUrl: string;
promoQr: boolean;
promoLead: string;
promoFade: string;
promoHold: string;
promoCaptions: boolean;
zoomSoft: string;
/** render.channelNames as typed: slug → name ("" is none). */
names: Record;
};
/** A desktop clip's code as the panel shows it: off, or its corner (br when on with none). */
const deskQrOf = (d: Look["desktop"]) => (!d?.qr ? "off" : typeof d.qr === "object" ? (d.qr.corner ?? "br") : "br");
/** render.desktop as the draft would write it: only what is off its default; null for none. */
function desktopNext(was: Look["desktop"], d: Draft): Look["desktop"] {
const { tag: _t, qr: _q, captions: _c, ...rest } = was ?? {};
const next: NonNullable = { ...rest };
if (!d.deskTag) next.tag = false;
if (d.deskQr !== "off") {
const old = was?.qr && typeof was.qr === "object" ? was.qr : {};
const { corner: _k, ...keep } = old;
next.qr = d.deskQr === "br" && !Object.keys(keep).length ? true : { ...keep, ...(d.deskQr !== "br" ? { corner: d.deskQr } : {}) };
}
const caps = { ...(was?.captions ?? {}) };
if (d.deskSize === "l") delete caps.size;
else caps.size = d.deskSize;
if (Object.keys(caps).length) next.captions = caps;
return Object.keys(next).length ? next : null;
}
/** The promo's fields as the panel edits them. */
function promoDraftOf(p: Look["promo"]) {
const lines = Array.isArray(p?.name) ? p.name : typeof p?.name === "string" ? [p.name] : [];
return {
promoOn: !!p,
promoName1: lines[0] ?? "",
promoName2: lines[1] ?? "",
promoUrl: p?.url ?? "",
promoQr: p?.qr !== false,
promoLead: String(p?.lead ?? PROMO_DEFAULTS.lead),
promoFade: String(p?.fade ?? PROMO_DEFAULTS.fade),
promoHold: String(p?.hold ?? PROMO_DEFAULTS.hold),
promoCaptions: p?.captions !== false,
};
}
/** render.promo as the draft would write it (the portrait, colours and kicker kept as written); null: none. */
function promoNext(was: Look["promo"], d: Draft): Look["promo"] {
if (!d.promoOn) return null;
const { name: _n, url: _u, qr: _q, lead: _l, fade: _f, hold: _h, captions: _c, ...keep } = was ?? {};
const n1 = d.promoName1.trim();
const n2 = d.promoName2.trim();
const next: Promo = { ...keep, name: n2 ? [n1, n2] : n1, url: d.promoUrl.trim() };
if (!d.promoQr) next.qr = false;
else if (typeof was?.qr === "string") next.qr = was.qr;
for (const [k, v] of [["lead", d.promoLead], ["fade", d.promoFade], ["hold", d.promoHold]] as const) {
const n = Number(v);
if (n !== PROMO_DEFAULTS[k]) next[k] = n;
}
if (!d.promoCaptions) next.captions = false;
return next;
}
/** One of render.qr's settings (the clip kit: corner, links), or its default. */
const qrField = (qr: unknown, k: "corner" | "links", dflt: string) =>
qr && typeof qr === "object" && typeof (qr as Record)[k] === "string" ? String((qr as Record)[k]) : dflt;
function draftOf(l: Look, mode: Mode = "deck"): Draft {
const t = l.loudness?.target;
return {
header: l.headerHeight !== 0,
footer: l.footerHeight !== 0,
qr: l.qr !== false,
qrCorner: qrField(l.qr, "corner", "br"),
qrLinks: qrField(l.qr, "links", "cite"),
transition: String(l.transition ?? 0.5),
style: l.transitionStyle ?? "fade",
loudness: typeof t !== "number" ? "off" : t === -16 ? "-16" : "custom",
target: typeof t === "number" ? String(t) : "-16",
captions: !!l.captions,
host: l.captions?.labels?.host ?? "",
subject: l.captions?.labels?.subject ?? "",
size: l.captions?.size ?? "m",
roomTail: l.room?.tail === true,
roomHead: l.room?.head === true,
roomMaxS: String(l.room?.maxS ?? ROOM.maxS),
roomMinGap: String(l.room?.minGap ?? ROOM.minGap),
travel: String(l.slide?.travel ?? SLIDE_DEFAULTS.travel),
smear: String(l.slide?.blur ?? SLIDE_DEFAULTS.blur),
grain: String(l.slide?.grain ?? SLIDE_DEFAULTS.grain),
blur: l.blur === "off" ? "off" : "on",
intro: introStyleOf(l.intro),
mode,
deskTag: l.desktop?.tag !== false,
deskQr: deskQrOf(l.desktop),
deskSize: l.desktop?.captions?.size ?? "l",
...promoDraftOf(l.promo),
zoomSoft: String(l.zooms?.soften ?? 1),
names: { ...(l.channelNames ?? {}) },
};
}
/** The channelNames patch that takes the saved names to the draft's: a slug set, or null to clear it; null when none changed. */
function namesPatchOf(was: Look["channelNames"], d: Draft): Record | null {
const saved = was ?? {};
const p: Record = {};
for (const slug of new Set([...Object.keys(saved), ...Object.keys(d.names)])) {
const next = (d.names[slug] ?? "").trim();
if (next === (saved[slug] ?? "")) continue;
p[slug] = next || null;
}
return Object.keys(p).length ? p : null;
}
const SLIDE_KEYS = [
["travel", "travel"],
["blur", "smear"],
["grain", "grain"],
] as const;
/** The patch that takes the saved look to the draft: only what changed, `null` to remove a key. */
function patchOf(was: Look, d: Draft, view: View): Record {
const p: Record = {};
if (d.header !== (was.headerHeight !== 0)) p.headerHeight = d.header ? null : 0;
if (d.footer !== (was.footerHeight !== 0)) p.footerHeight = d.footer ? null : 0;
if (d.qr !== (was.qr !== false)) p.qr = d.qr;
// The code's corner and link (the clip kit): merged into render.qr, a default written as no key.
if (d.qr && (d.qrCorner !== qrField(was.qr, "corner", "br") || d.qrLinks !== qrField(was.qr, "links", "cite"))) {
p.qr = { corner: d.qrCorner === "br" ? null : d.qrCorner, links: d.qrLinks === "cite" ? null : d.qrLinks };
}
const tr = Number(d.transition);
if (tr !== (was.transition ?? 0.5)) p.transition = tr;
if (d.style !== (was.transitionStyle ?? "fade")) p.transitionStyle = d.style === "fade" ? null : d.style;
if (!view.montage) {
const target = d.loudness === "off" ? null : d.loudness === "-16" ? -16 : Number(d.target);
if (target !== (was.loudness?.target ?? null)) p.loudness = target === null ? null : { target };
}
if (!view.montage && !view.deck) {
const labels = { ...(d.host.trim() ? { host: d.host.trim() } : {}), ...(d.subject.trim() ? { subject: d.subject.trim() } : {}) };
const next = d.captions ? { style: "speakers", ...(Object.keys(labels).length ? { labels } : {}), ...(d.size === "l" ? { size: "l" } : {}) } : null;
if (JSON.stringify(next) !== JSON.stringify(was.captions ?? null)) p.captions = next;
}
if (!view.montage) {
// Room at the clips' edges (release 36 lane E): its settings kept as written.
const was0 = was.room ?? null;
const on = d.roomTail || d.roomHead;
// The numbers (release 38): written only when they differ from the build's defaults.
const nums: Record = {};
const maxS = Number(d.roomMaxS);
const minGap = Number(d.roomMinGap);
if (maxS !== ROOM.maxS || was0?.maxS !== undefined) nums.maxS = maxS;
if (minGap !== ROOM.minGap || was0?.minGap !== undefined) nums.minGap = minGap;
const { maxS: _m, minGap: _g, ...edges } = was0 ?? {};
const next = on ? { ...edges, tail: d.roomTail, head: d.roomHead, ...nums } : null;
const same =
(was0?.tail === true) === d.roomTail &&
(was0?.head === true) === d.roomHead &&
(!on || ((was0?.maxS ?? ROOM.maxS) === maxS && (was0?.minGap ?? ROOM.minGap) === minGap));
if (!same) p.room = next;
}
// The channel promo (render.promo): the card a per-clip build ends on.
{
const next = promoNext(was.promo, d);
if (JSON.stringify(next ?? null) !== JSON.stringify(was.promo ?? null)) p.promo = next;
}
// The zooms' soft limit (render.zooms.soften, clip-zoom.mjs): 1 (strict) is no key.
if (Number(d.zoomSoft) !== (was.zooms?.soften ?? 1)) p.zooms = Number(d.zoomSoft) === 1 ? null : { soften: Number(d.zoomSoft) };
// The desktop clips (render.desktop, the clip kit): the source line, a code, the captions' size.
{
const next = desktopNext(was.desktop, d);
if (JSON.stringify(next ?? null) !== JSON.stringify(was.desktop ?? null)) p.desktop = next;
}
// The cut's names for its channels (render.channelNames): merged by the writer, a slug null clears it.
{
const next = namesPatchOf(was.channelNames, d);
if (next) p.channelNames = next;
}
// The output mode (render.mode): the writer keeps "deck" as no key unless a look preset's must be beaten.
if (d.mode !== (view.mode ?? "deck")) p.mode = d.mode;
// The blurred clips (render.blur): "on" is the default, written as no key.
if (d.blur !== (was.blur === "off" ? "off" : "on")) p.blur = d.blur === "off" ? "off" : null;
// The brand intro: none removes the key; a style keeps an object's other knobs.
if (d.intro !== introStyleOf(was.intro)) {
p.intro = !d.intro ? null : was.intro && typeof was.intro === "object" ? { ...was.intro, style: d.intro } : d.intro;
}
// The blur slide's knobs (release 38): only the ones off their defaults are written.
{
const cur = { travel: Number(d.travel), blur: Number(d.smear), grain: Number(d.grain) };
const had = { ...SLIDE_DEFAULTS, ...(was.slide ?? {}) };
if (SLIDE_KEYS.some(([k]) => cur[k] !== had[k])) {
const next = Object.fromEntries(SLIDE_KEYS.filter(([k]) => cur[k] !== SLIDE_DEFAULTS[k]).map(([k]) => [k, cur[k]]));
p.slide = Object.keys(next).length ? next : null;
}
}
return p;
}
export default function LookPanel({ project }: { project: string }) {
const [view, setView] = useState(null);
const [draft, setDraft] = useState(null);
const [busy, setBusy] = useState(false);
const [error, setError] = useState(null);
const [status, setStatus] = useState(null);
// Bumped when this panel writes the manifest, so the Ask box re-reads its token.
const [version, setVersion] = useState(0);
// The channel promo's card drawn (its two images asked for again with this number).
const [cardsAt, setCardsAt] = useState(0);
const load = useCallback(async () => {
const r = await fetch(`/api/report/render?project=${encodeURIComponent(project)}`, { cache: "no-store" });
const j = (await r.json()) as View & { error?: string };
if (!r.ok) throw new Error(j.error ?? `HTTP ${r.status}`);
setView(j);
setDraft(draftOf(j.look, j.mode ?? "deck"));
}, [project]);
useEffect(() => {
load().catch((e) => setError(e instanceof Error ? e.message : String(e)));
}, [load]);
if (!view || !draft) {
return (
Output
{(["basic", "deck"] as const).map((m) => (
))}
{draft.mode === "basic" && (
the footage alone — post items, teasers and cards are left out; the rest below applies to a deck render
)}
{view.preset && look preset: {view.preset}}
{view.look.promo?.portrait?.file ? `Portrait: ${view.look.promo.portrait.file}` : "No portrait yet"} — set it from the clip editor’s Short panel (Use this frame as the promo portrait)