"use client"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { buttonVariants } from "@/components/ui/button"; // THE MODULE PANEL (release 39, the video modules): every video module with // a `ui` descriptor (report-to-video/modules/.mjs), drawn from that // descriptor -- its fields, on/off for a module that is an object of its own, // its validator's sentences, a save through the writers (PUT // /api/report/modules) and a preview of the module ALONE (POST // /api/report/chrome/preview { layers: [id] }). The bespoke editors above // (the deck's settings, the markers, the claim checks) stay; this covers // every module, the ones without one first -- the story modules, the // finale's contents, the intro's brand and site. And the cut's look preset. type Field = { key: string; type: string; label: string; options?: unknown[]; min?: number; max?: number; step?: number; default?: unknown; item?: Field[]; when?: Record; }; type ModuleRow = { id: string; title: string; summary: string; kind: string; slot: string; z: number; configKey: string; base: string; ui: Field[]; toggle: boolean; enabled: boolean; fields: Record; effective: Record; preview: boolean; problems: string[]; }; type Preset = { name: string; description: string; error?: string }; type State = { preset: string | null; presetError: string | null; presets: Preset[]; modules: ModuleRow[]; token: string | null }; type Layer = { id: string; region?: string | null; geometry?: { x: number; y: number; width: number; height: number } | null; src?: string; off?: string; error?: string; windows?: { segment: string | null; from: number; to?: number; src?: string; error?: string }[]; }; 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)]"; // The panel lists the modules without a bespoke editor first. const FIRST = ["pair", "chapter", "status", "finale", "intro", "counter"]; const LIST_TYPES = new Set(["list", "series", "stats", "markers"]); // The rows of a list type whose descriptor names no item fields. const ITEMS: Record = { stats: [ { key: "label", type: "text", label: "Label" }, { key: "value", type: "text", label: "Value" }, { key: "note", type: "text", label: "Note" }, { key: "color", type: "color", label: "Colour" }, ], markers: [ { key: "after", type: "clip", label: "After" }, { key: "label", type: "text", label: "Label" }, { key: "color", type: "text", label: "Colour" }, { key: "align", type: "enum", label: "Align", options: ["auto", "left", "right", "center"] }, ], }; const textOf = (v: unknown) => (v === undefined || v === null ? "" : typeof v === "object" ? JSON.stringify(v) : String(v)); function getPath(obj: unknown, dotted: string): unknown { return dotted.split(".").reduce((o, k) => (o && typeof o === "object" && !Array.isArray(o) ? (o as Record)[k] : undefined), obj); } function setPath(obj: Record, dotted: string, value: unknown): Record { const out = structuredClone(obj); const keys = dotted.split("."); let cur: Record = out; for (const k of keys.slice(0, -1)) { if (!cur[k] || typeof cur[k] !== "object" || Array.isArray(cur[k])) cur[k] = {}; cur = cur[k] as Record; } const last = keys[keys.length - 1]; if (value === "" || value === undefined || value === null) delete cur[last]; else cur[last] = value; return out; } /** One scalar field's input. */ function Scalar({ f, value, placeholder, onChange, clips, testid }: { f: Field; value: unknown; placeholder: unknown; onChange: (v: unknown) => void; clips: string[]; testid: string; }) { if (f.type === "boolean") { const v = value === undefined ? "" : String(value); return ( ); } if (f.type === "enum") { const opts = (f.options ?? []).map((o) => String(o)); return ( ); } if (f.type === "number") { return ( onChange(e.target.value === "" ? null : Number(e.target.value))} /> ); } if (f.type === "color") { return onChange(e.target.value)} />; } if (f.type === "clip") { return onChange(e.target.value)} />; } if (f.type === "date") { return onChange(e.target.value)} />; } if (f.type === "text") { return onChange(e.target.value)} />; } // Anything else (a list of tokens, the stats cards, markers, an object): its JSON. return ; } function JsonField({ value, placeholder, onChange, testid }: { value: unknown; placeholder: unknown; onChange: (v: unknown) => void; testid: string }) { const [text, setText] = useState(() => (value === undefined ? "" : JSON.stringify(value))); const [bad, setBad] = useState(false); return (