"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
//
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 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(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 (
setOpen((e.currentTarget as HTMLDetailsElement).open)}>
{label}
{open ? children(close) : null}
);
}
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 (
);
}
function LookSwitch({ project, basic, onChanged }: { project: string; basic: boolean; onChanged: () => void }) {
const [busy, setBusy] = useState(false);
const [error, setError] = useState(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 (
Look
{(["basic", "deck"] as const).map((m) => (
))}
{error && (
{error}
)}
);
}
function RenderMenu({ project }: { project: string }) {
const [said, setSaid] = useState(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 (
);
}
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 (
{version ? (
{version.id} · read-only
Open in draft
the draft
) : (
{draftLine}
)}
{!readOnly && }
{!readOnly && }
{shareClip && }
{!readOnly && (
<>
{undo.said && (
{undo.said}
)}
>
)}
{/* lib/pieces/tabs.mjs PIECE_TABS: this screen is the first of them. */}
{PIECE_TABS.filter((t) => t.id !== "workspace").map((t) => (