"use client"; import Link from "next/link"; import { useCallback, useEffect, useRef, useState } from "react"; import { Mark } from "@/components/shell/Wordmark"; import { buttonVariants } from "@/components/ui/button"; import { cn } from "@/lib/utils"; import { openInDraftHref, playHref, projectHref, projectsHref, takesHref, versionReviewHref, workspaceHref, type WorkspaceMode } from "@/lib/hrefs"; import { PIECE_TABS, PIECE_VIEWS } from "@/lib/pieces/tabs.mjs"; import CopyShareLine from "../CopyShareLine"; import { startTakeJob } from "../NewTakePanel"; import { announceManifestChanged } from "../timelineApi"; // --------------------------------------------------------------------------- // THE WORKSPACE'S TOP BAR (umtool redesign L7a), one row: // // ← Projects / · Draft · from vN · N edits · Versions ▾ · Edit | Review pass | Present ⛶ // · Look Basic|Deck · Render ▾ · Share · Undo Redo · Piece ▾ // // Versions ▾ lists every saved version (and legacy take): Play it read-only, Open it in the draft (it asks // first, and saves the draft as a version when it has edits -- OpenInDraftPage), and Save version (the // draft frozen as the next vN: `umtool takes save`). The mode switch (L7b) is the workspace's own: Edit, the // Review pass (`?mode=review`), Present (`?mode=present`) -- links, so a new tab opens each. Look writes render.mode (`umtool look // <p> mode=basic|deck`); Render queues a take of the draft (`umtool take`); Share copies the share line // of the selected clip (`umtool share`). Piece ▾ is the piece page's tabs (Sources, Deliver, History …). // --------------------------------------------------------------------------- type Version = { id: string; label: string; made: boolean }; const btn = (variant: "outline" | "quiet" | "pressed" = "quiet") => cn(buttonVariants({ variant, size: "sm" })); /** A <details> popover that closes on a click outside and on Escape. */ function Menu({ label, testid, children, align = "left", title }: { label: React.ReactNode; testid: string; children: (close: () => void) => React.ReactNode; align?: "left" | "right"; title?: string }) { const ref = useRef<HTMLDetailsElement | null>(null); const [open, setOpen] = useState(false); const close = useCallback(() => { if (ref.current) ref.current.open = false; setOpen(false); }, []); useEffect(() => { if (!open) return; const onDown = (e: MouseEvent) => { if (ref.current && !ref.current.contains(e.target as Node)) close(); }; const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") close(); }; window.addEventListener("mousedown", onDown); window.addEventListener("keydown", onKey); return () => { window.removeEventListener("mousedown", onDown); window.removeEventListener("keydown", onKey); }; }, [open, close]); return ( <details ref={ref} className="relative" {...{ [`data-ws-${testid}`]: open ? "open" : "" }} onToggle={(e) => setOpen((e.currentTarget as HTMLDetailsElement).open)}> <summary className={cn(btn("quiet"), "cursor-pointer list-none")} title={title}> {label} </summary> <div className={cn("absolute top-full z-50 mt-1 min-w-[260px] max-w-[min(420px,92vw)] rounded-[3px] border border-seam-2 bg-panel p-1.5 text-[12px] shadow-lg", align === "right" ? "right-0" : "left-0")}> {open ? children(close) : null} </div> </details> ); } function VersionsMenu({ project, versions, onSaved }: { project: string; versions: Version[]; onSaved: () => void }) { const [state, setState] = useState<{ busy: boolean; said: string | null; error: boolean }>({ busy: false, said: null, error: false }); const save = async () => { setState({ busy: true, said: null, error: false }); const r = await fetch("/api/report/takes/save", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ project }) }); const j = (await r.json().catch(() => ({}))) as { saved?: boolean; id?: string; why?: string; error?: string }; if (!r.ok) return setState({ busy: false, said: j.error ?? `HTTP ${r.status}`, error: true }); setState({ busy: false, said: j.saved ? `saved as ${j.id}` : (j.why ?? "nothing to save"), error: false }); onSaved(); }; return ( <Menu label={<>Versions{versions.length ? ` (${versions.length})` : ""} ▾</>} testid="versions" title="saved versions of the draft: play one read-only, open one in the draft, save the draft as the next"> {() => ( <div className="flex flex-col gap-1"> <div className="flex items-center gap-2 border-b border-seam px-1 pb-1.5"> <button type="button" className={btn("outline")} disabled={state.busy} onClick={() => void save()} data-save-version="" title="freeze the working draft as the next version (v1, v2, …): it never changes after, and Open in draft brings it back (umtool takes save)"> {state.busy ? "Saving…" : "Save version"} </button> {state.said && ( <span role="status" className={cn("text-[11px]", state.error ? "text-reject-text" : "text-muted")} data-save-version-said={state.error ? "error" : "ok"}> {state.said} </span> )} </div> {versions.length === 0 ? ( <p className="m-0 px-1 py-1 text-muted">No saved version yet.</p> ) : ( <ul className="m-0 flex max-h-[50vh] list-none flex-col overflow-auto p-0" data-ws-version-list=""> {versions .slice() .reverse() .map((v) => ( <li key={v.id} className="flex items-baseline gap-2 rounded-[2px] px-1 py-0.5 hover:bg-panel-2" data-ws-version={v.id}> <span className={cn("font-mono text-text", !v.made && "italic")}>{v.id}</span> <span className="min-w-0 flex-1 truncate text-muted" title={v.label}> {v.label} </span> <Link href={versionReviewHref(project, v.id)} prefetch={false} className="ink-link" data-ws-version-review={v.id} title="its review pass: watched block by block, read-only, its verdict at the end"> Review </Link> <Link href={playHref(project, { take: v.id })} prefetch={false} className="ink-link" data-ws-version-play={v.id} title="play it read-only"> Play </Link> <Link href={openInDraftHref(project, v.id)} prefetch={false} className="ink-link" data-ws-version-open={v.id} title="copy it into the draft to edit it (it asks first; your draft is saved as a version when it has edits)"> Open in draft </Link> </li> ))} </ul> )} </div> )} </Menu> ); } function LookSwitch({ project, basic, onChanged }: { project: string; basic: boolean; onChanged: () => void }) { const [busy, setBusy] = useState(false); const [error, setError] = useState<string | null>(null); const set = async (mode: "basic" | "deck") => { if ((mode === "basic") === basic) return; setBusy(true); setError(null); try { const g = await fetch(`/api/report/render?project=${encodeURIComponent(project)}`, { cache: "no-store" }); const v = (await g.json().catch(() => ({}))) as { token?: string | null; error?: string }; if (!g.ok) throw new Error(v.error ?? `HTTP ${g.status}`); const r = await fetch("/api/report/render", { method: "PUT", headers: { "content-type": "application/json" }, body: JSON.stringify({ project, token: v.token ?? null, patch: { mode } }) }); const j = (await r.json().catch(() => ({}))) as { error?: string; errors?: string[] }; if (!r.ok) throw new Error((j.errors ?? [j.error ?? `HTTP ${r.status}`]).join("; ")); announceManifestChanged(project); onChanged(); } catch (e) { setError(e instanceof Error ? e.message : String(e)); } finally { setBusy(false); } }; return ( <span className="inline-flex items-center gap-1.5" data-ws-look={basic ? "basic" : "deck"}> <span className="text-[11.5px] text-muted">Look</span> <span role="group" aria-label="Output mode" className="inline-flex overflow-hidden rounded-[3px] border border-seam-2"> {(["basic", "deck"] as const).map((m) => ( <button key={m} type="button" data-ws-look-mode={m} aria-pressed={(m === "basic") === basic} disabled={busy} onClick={() => void set(m)} title={m === "basic" ? "the footage alone: no deck, chrome, intro, QR, cards or brand (umtool look <p> mode=basic)" : "the deck and the look's chrome (umtool look <p> mode=deck)"} className={(m === "basic") === basic ? "bg-[var(--color-text)] px-2 py-0.5 text-[11.5px] font-semibold text-[var(--color-ground)]" : "bg-transparent px-2 py-0.5 text-[11.5px] text-text-2 hover:text-text"} > {m === "basic" ? "Basic" : "Deck"} </button> ))} </span> {error && ( <span role="alert" className="text-[11px] text-reject-text"> {error} </span> )} </span> ); } function RenderMenu({ project }: { project: string }) { const [said, setSaid] = useState<string | null>(null); const [busy, setBusy] = useState(false); const go = async (window: "full" | { at: number; dur: number }) => { setBusy(true); setSaid(null); const r = await startTakeJob({ project, take: { label: window === "full" ? "Draft, full" : "Draft, 60 s", window } }); setBusy(false); setSaid(r.error ?? "rendering — the job is on the jobs bar; the take lands under Versions"); }; return ( <Menu label="Render ▾" testid="render" title="render the draft as a take (umtool take): a 60 s preview or the whole cut"> {() => ( <div className="flex flex-col gap-1.5 p-1"> <div className="flex flex-wrap gap-1.5"> <button type="button" className={btn("outline")} disabled={busy} onClick={() => void go({ at: 0, dur: 60 })} data-ws-render="60" title="umtool take <p> --at 0 --dur 60"> Render 60 s </button> <button type="button" className={btn("outline")} disabled={busy} onClick={() => void go("full")} data-ws-render="full" title="umtool take <p> --full"> Render full </button> </div> {said && ( <span role="status" className="text-[11px] text-muted" data-ws-render-said=""> {said} </span> )} <Link href={takesHref(project)} prefetch={false} className="ink-link text-[11.5px]"> Takes and renders </Link> </div> )} </Menu> ); } export type UndoState = { busy: boolean; said: string | null; error: boolean; canRedo: boolean }; const MODES: { id: "edit" | WorkspaceMode; label: string; title: string }[] = [ { id: "edit", label: "Edit", title: "the draft: its blocks, the inspector" }, { id: "review", label: "Review pass", title: "the draft block by block: Keep · Fix · Note each (k f n), a verdict at the end" }, { id: "present", label: "Present ⛶", title: "the cut as slides, full screen, navigated like a deck (Esc back)" }, ]; export default function WorkspaceTop({ project, mode = "edit", onMode, version = null, readOnly = false, title, draftLine, draftParent, draftEdits, versions, basic, firstClip, shareClip, undo, onUndo, onRedo, onChanged, help, }: { project: string; title: string; draftLine: string; draftParent: string | null; draftEdits: number | null; versions: Version[]; basic: boolean; /** The cut's first clip (unused since L7b's review pass; kept for callers). */ firstClip: string | null; /** The clip whose share line Share copies: the selected one, else the one on screen. */ shareClip: string | null; undo: UndoState; onUndo: () => void; onRedo: () => void; /** Something the bar wrote (a version, the look): the workspace reads the draft again. */ onChanged: () => void; help: () => void; mode?: "edit" | WorkspaceMode; onMode?: (m: "edit" | WorkspaceMode) => void; /** A saved version reviewed or presented read-only (`?take=`): nothing here writes the draft. */ version?: { id: string; label: string } | null; /** The workspace's one read-only flag (readOnly.ts): nothing on the bar writes the draft. */ readOnly?: boolean; }) { return ( <header data-app-header="workspace" data-ws-top="" className="flex flex-wrap items-center gap-x-2.5 gap-y-1 border-b border-seam bg-panel px-3 py-1.5"> <Link href="/" prefetch={false} aria-label="umtool home" className="shrink-0"> <Mark size={18} /> </Link> <nav aria-label="Breadcrumb" className="flex min-w-0 items-center gap-1.5 text-[12.5px]"> <Link href={projectsHref()} prefetch={false} className="hidden text-muted-2 hover:text-text xl:inline"> Projects </Link> <span className="hidden text-seam-2 xl:inline">/</span> <h1 className="m-0 min-w-0 max-w-[22vw] truncate font-display text-[14.5px] font-semibold text-text" title={`${title} — ${project}`} data-ws-title=""> {title} </h1> </nav> {version ? ( <span className="inline-flex items-baseline gap-2 font-mono text-[11.5px] text-text-2" data-ws-version-shown={version.id} title={version.label}> {version.id} · read-only <Link href={openInDraftHref(project, version.id)} prefetch={false} className="ink-link font-sans" data-ws-version-open-draft=""> Open in draft </Link> <Link href={workspaceHref(project)} prefetch={false} className="ink-link font-sans" data-ws-version-back=""> the draft </Link> </span> ) : ( <span className="font-mono text-[11.5px] text-text-2" data-ws-draft={draftParent ?? ""} data-ws-draft-edits={draftEdits ?? ""} title="the working draft: every edit lands in it (video.manifest.json)"> {draftLine} </span> )} <VersionsMenu project={project} versions={versions} onSaved={onChanged} /> <nav aria-label="Mode" className="inline-flex items-center rounded-[3px] border border-seam-2 p-0.5 text-[12px]" data-ws-mode={mode}> {MODES.map((m) => { const on = m.id === mode; const href = m.id === "edit" ? workspaceHref(project) : workspaceHref(project, { mode: m.id, take: version?.id ?? null }); return ( <a key={m.id} href={href} aria-current={on ? "page" : undefined} {...{ [`data-ws-mode-${m.id}`]: "" }} title={m.title} onClick={(e) => { if (!onMode || e.metaKey || e.ctrlKey || e.shiftKey || e.button !== 0) return; e.preventDefault(); onMode(m.id); }} className={on ? "rounded-[2px] bg-panel-2 px-2 py-0.5 font-medium text-text" : "rounded-[2px] px-2 py-0.5 text-muted-2 hover:text-text"} > {m.label} </a> ); })} </nav> {!readOnly && <LookSwitch project={project} basic={basic} onChanged={onChanged} />} {!readOnly && <RenderMenu project={project} />} {shareClip && <CopyShareLine key={shareClip} project={project} clip={shareClip} testid="ws" />} <span className="ml-auto inline-flex items-center gap-1"> {!readOnly && ( <> <button type="button" className={btn("quiet")} disabled={undo.busy} onClick={onUndo} data-ws-undo="" title="take back the newest edit to the draft (⌘Z / Ctrl+Z; umtool timeline <p> undo)"> Undo </button> <button type="button" className={btn("quiet")} disabled={undo.busy || !undo.canRedo} onClick={onRedo} data-ws-redo="" title="put back what Undo took back (⌘⇧Z / Ctrl+Y; umtool timeline <p> redo)"> Redo </button> {undo.said && ( <span role="status" className={cn("max-w-[220px] truncate text-[11px]", undo.error ? "text-reject-text" : "text-muted")} data-ws-undo-said={undo.error ? "error" : "ok"} title={undo.said}> {undo.said} </span> )} </> )} <Menu label="Piece ▾" testid="piece" align="right" title="the piece page's tabs: Sources, Deliver, History (and the views the workspace does not hold yet)"> {(close) => ( <ul className="m-0 flex list-none flex-col p-0"> {/* lib/pieces/tabs.mjs PIECE_TABS: this screen is the first of them. */} {PIECE_TABS.filter((t) => t.id !== "workspace").map((t) => ( <li key={t.id}> <Link href={projectHref(project, { tab: t.id })} prefetch={false} onClick={close} className="block rounded-[2px] px-2 py-1 text-text-2 hover:bg-panel-2 hover:text-text" data-ws-piece-tab={t.id}> {t.label} </Link> </li> ))} <li aria-hidden className="my-1 border-t border-seam" /> {PIECE_VIEWS.map((t) => ( <li key={t.id}> <Link href={projectHref(project, { tab: t.id })} prefetch={false} onClick={close} className="block rounded-[2px] px-2 py-1 text-muted-2 hover:bg-panel-2 hover:text-text" data-ws-piece-tab={t.id}> {t.label} </Link> </li> ))} </ul> )} </Menu> <button type="button" onClick={help} className={cn(btn("outline"), "min-w-8 font-mono")} aria-label="keys" title="keys (?)" data-ws-help=""> ? </button> </span> </header> ); }