"use client";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { useState } from "react";
import DeletePlan from "@/components/DeletePlan";
import RenderRecord from "@/components/queue/RenderRecord";
import { useDisplay } from "yt-dlp-transcript-common/components/PrivacyProvider";
import type { RenderView } from "@/lib/queue/view";
import { buttonVariants } from "@/components/ui/button";
import { takesCompareHref, takesHref } from "@/lib/hrefs";
// ---------------------------------------------------------------------------
// TAKE HOUSEKEEPING, the rest of it (UI notes 2026-10-30, item 19): Cancel a
// take while it renders (lib/report/takeCancel.mjs), Fork as variant
// (variantFork.mjs: a project beside this one), Compare with… (two takes of
// the project and its variants side by side: /projects/
/takes/compare).
// All through POST /api/report/takes/housekeeping, as relabel and supersede.
// ---------------------------------------------------------------------------
export type CompareOption = { ref: string; label: string };
export type WorkDirs = {
rendering: Array<{ id: string; label: string | null; pid: number | null; startedAt: string | null; from: string | null }>;
cancelled: Array<{ id: string; label: string | null; at: string; by: string }>;
/** First renders that failed: their own record says so (lib/report/renderRecord.mjs). */
failed?: Array<{ id: string; label: string | null; record: RenderView }>;
};
const sm = buttonVariants({ size: "sm" });
const smOutline = buttonVariants({ variant: "outline", size: "sm" });
async function housekeeping(body: Record) {
const r = await fetch("/api/report/takes/housekeeping", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(body),
cache: "no-store",
});
const j = (await r.json().catch(() => ({}))) as Record & { error?: string };
return { ok: r.ok, j };
}
export type CancelSaid = { take: string; text: string; ok: boolean };
/**
* Cancel: asks the process rendering the take to stop, and waits for it (up to ~25 s).
* The answer goes to `onSaid` -- held by a parent that outlives this button, because a
* render that stopped is no longer rendering, and the refresh takes the button away.
*/
export function CancelRender({ project, take, ready, onSaid }: { project: string; take: string; ready: boolean; onSaid: (s: CancelSaid) => void }) {
const router = useRouter();
const [busy, setBusy] = useState(false);
return (
);
}
/** What the last Cancel render said. */
export function CancelSaidLine({ said }: { said: CancelSaid | null }) {
if (!said) return null;
return (
{said.text}
);
}
/** Fork as variant: the take as a project beside this one (default name v-). */
export function ForkVariant({ project, take, ready }: { project: string; take: string; ready: boolean }) {
const [open, setOpen] = useState(false);
const [name, setName] = useState(`v-${take}`);
const [busy, setBusy] = useState(false);
const [done, setDone] = useState<{ id: string; take: { id: string; linked: boolean } | null } | null>(null);
const [error, setError] = useState(null);
if (done) {
return (
Forked as{" "}
{done.id}
{done.take?.linked ? ` — with this take in it` : ""} ·{" "}
compare
);
}
return (
{!open ? (
) : (
<>
setName(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Escape") setOpen(false);
}}
className="w-40 rounded border border-[var(--color-line)] bg-[var(--color-ink)] px-1.5 py-0.5 font-mono text-[11px] text-[var(--color-text)]"
/>
>
)}
{error && (
{error}
)}
);
}
/** Compare with…: another take of the project or of one of its variants, side by side. */
export function CompareWith({ project, take, options }: { project: string; take: string; options: CompareOption[] }) {
const mine = `${project}:${take}`;
const others = options.filter((o) => o.ref !== mine);
const [other, setOther] = useState(others[0]?.ref ?? "");
if (!others.length) return null;
return (
Compare
);
}
/**
* The take directories that are not takes (yet): a first render in progress,
* with Cancel, and first renders cancelled -- folded away, each deletable.
*/
export function TakeWorkDirs({ project, dirs, ready }: { project: string; dirs: WorkDirs; ready: boolean }) {
const router = useRouter();
const { displayText } = useDisplay();
const [show, setShow] = useState(false);
const [said, setSaid] = useState(null);
const failed = dirs.failed ?? [];
if (!dirs.rendering.length && !dirs.cancelled.length && !failed.length && !said) return null;
return (
{dirs.rendering.map((r) => (
rendering now{r.id}
{r.label && {r.label}}
{r.startedAt ? `since ${r.startedAt.slice(11, 16)}` : ""}
{r.pid ? ` · pid ${r.pid}` : ""}
{r.from ? ` · from ${r.from}` : ""} · its first render: it becomes a take when it is done
))}
{failed.map((f) => (
// A first render that failed: never a take, its error and its log in the open (not folded).
first render failed{f.id}
{f.label && {f.label}}
never finished: no take · umtool take status {project} --id {f.id} router.refresh()} />