import Link from "next/link"; import { notFound, redirect } from "next/navigation"; import { buttonVariants } from "@/components/ui/button"; import { clipBenchHref, openInDraftHref, playHref, postBenchHref, projectsHref } from "@/lib/hrefs"; import type { ProjectRef } from "@/lib/project-types"; import { manifestToken } from "@/lib/report/manifest.mjs"; import { isTakeId, listTakes } from "@/lib/report/takes.mjs"; import { nextVersionId, openIsNoop } from "@/lib/report/versions.mjs"; import { readWorkingCut } from "@/lib/report/workingCut.mjs"; import { BenchBody, BenchFrame, BenchTop } from "./bench/BenchShell"; // `/projects//takes//open` -- OPEN A VERSION IN THE DRAFT (umtool redesign L3; // lib/report/versions.mjs openVersion). Every edit lands in the draft (the project's manifest); a saved // version is frozen, so editing one means copying it into the draft first. When the draft already holds what // is, there is nothing to copy: a 307 straight on to the draft's bench on `clip` // (a post item's with `post=1`), else to Play of the draft. Otherwise it asks -- "Open v1 in draft? Your // draft (2 edits) is saved as v4 first." -- and the button is a plain form POST (no script needed) to // /api/report/takes/open, which lands on the same place. // // The take walk's and the take bench's old addresses (`…/takes//walk`, `…/takes//clip/`) // are served by this page too: the walk and the bench of a version are the draft's now. export default async function OpenInDraftPage({ project, take, clip = null, search = {}, }: { project: ProjectRef; take: string; clip?: string | null; search?: Record; }) { if (!isTakeId(take)) notFound(); const { takes } = await listTakes(project.dir); const t = takes.find((x) => x.id === take); if (!t) notFound(); const fromPlay = search.from === "play"; const at = Number(search.at); const atS = Number.isFinite(at) && at > 0 ? at : null; const where = clip ?? search.clip ?? null; const post = search.post === "1"; const dest = where ? post ? postBenchHref(project.id, where, fromPlay ? { play: { at: atS } } : {}) : clipBenchHref(project.id, where, fromPlay ? { play: { at: atS, take: null } } : {}) : playHref(project.id); const working = await readWorkingCut(project.dir); const edits = working.edits?.count ?? null; // Nothing to copy (the draft already holds what the take is): straight on. A 307, not a 308 -- the answer // depends on the draft as it is now, and a browser must never remember it. if (await openIsNoop(project.dir, take)) redirect(dest); const willSave = !working.parent || working.edits === null || (edits ?? 0) > 0; const next = await nextVersionId(project.dir); const here = openInDraftHref(project.id, take, { clip: where, post, play: fromPlay ? { at: atS } : null }); const draftWords = !working.parent ? "Your draft (never saved)" : edits === null ? `Your draft (from ${working.parent.id}, its edits unknown)` : `Your draft (${edits} edit${edits === 1 ? "" : "s"} since ${working.parent.id})`; return ( } >

Open {take} in draft?

{willSave ? ( <> {draftWords} is saved as {next} first. ) : ( <>Your draft has no edits since {working.parent?.id} — nothing to save. )}{" "} Then {take} — “{t.label}” — is copied into the draft{where ? <>, and the bench opens on {where} : ""}. One undo (the Cut tab's Undo last edit) puts the draft back as it was.

{search.error && (

{search.error}

)}
Cancel — play {take}

The same from a terminal: umtool takes open {project.id} {take}

); }