"use client"; import { useRouter } from "next/navigation"; import { useEffect, useRef, useState } from "react"; import { badgeVariants, type BadgeVariants } from "@/components/ui/badge"; import { buttonVariants } from "@/components/ui/button"; import { queueLogHref } from "@/lib/hrefs"; import type { RunnerView, StepView } from "@/lib/queue/view"; import { useDisplay } from "yt-dlp-transcript-common/components/PrivacyProvider"; // THE DURABLE QUEUE (lib/queue/store.mjs), as a list: the runner (is the umtool // app running it), then each step -- its qid, state, label, what it waits on, // its error, its full log, and Cancel / Retry. Steps are added from a terminal // (`umtool queue add -- `), never here. // // Polls GET /api/queue every 3 s ONLY while a step is queued or running (an // idle page makes no requests), and asks the page to refresh when a step // finishes, so the render records beside it catch up. export const QUEUE_POLL_MS = 3000; const TONE: Record = { queued: "info", running: "meter", done: "on", failed: "blocking", blocked: "open", cancelled: "state", }; type View = { runner: RunnerView; steps: StepView[] }; const clock = (iso: string | null) => (iso ? iso.slice(11, 16) : ""); export default function QueuePanel({ initial, project = null, emptyText = "Nothing queued." }: { initial: View; project?: string | null; emptyText?: string }) { const router = useRouter(); // settings.privacy: what a step's label, command and error DRAW (client-side, // so never the private files' rules); the identity when it is off. const { displayText } = useDisplay(); const [view, setView] = useState(initial); const [said, setSaid] = useState<{ text: string; ok: boolean } | null>(null); const [ready, setReady] = useState(false); const inflight = useRef(false); const current = useRef(view); current.current = view; useEffect(() => setReady(true), []); useEffect(() => setView(initial), [initial]); const live = view.steps.some((s) => s.state === "queued" || s.state === "running"); useEffect(() => { if (!live) return; const t = setInterval(async () => { if (inflight.current || document.visibilityState === "hidden") return; inflight.current = true; try { const r = await fetch(`/api/queue${project ? `?${new URLSearchParams({ project })}` : ""}`, { cache: "no-store" }); if (!r.ok) return; const next = (await r.json()) as View; const moved = current.current.steps.some((s) => (s.state === "running" || s.state === "queued") && next.steps.find((n) => n.qid === s.qid)?.state !== s.state); setView(next); if (moved) router.refresh(); } catch { /* the next tick asks again */ } finally { inflight.current = false; } }, QUEUE_POLL_MS); return () => clearInterval(t); }, [live, project, router]); const act = async (op: "cancel" | "retry", qid: string) => { const r = await fetch(`/api/queue?${new URLSearchParams({ [op]: qid })}`, { method: "POST", cache: "no-store" }); const j = (await r.json().catch(() => ({}))) as { said?: string; error?: string }; setSaid({ text: j.said ?? j.error ?? `HTTP ${r.status}`, ok: r.ok }); const v = await fetch(`/api/queue${project ? `?${new URLSearchParams({ project })}` : ""}`, { cache: "no-store" }).then((x) => (x.ok ? x.json() : null)).catch(() => null); if (v) setView(v as View); router.refresh(); }; const runner = view.runner; return (

{runner.live ? ( <> Run by the umtool app (pid {runner.pid}) one step at a time. {runner.waiting ? {displayText(runner.waiting)}. : null} ) : ( No runner: nothing in the queue runs until the umtool app is up (archilyzer-umtool.service). )}{" "} Add steps from a terminal: umtool queue add -- <command…>

{said && (

{displayText(said.text)}

)} {view.steps.length === 0 ? (

{emptyText}

) : (
    {view.steps.map((s) => (
  • {s.qid} {s.state} {displayText(s.label)} {s.after && after {s.after}} {s.startedAt ? `${clock(s.startedAt)}${s.endedAt ? `→${clock(s.endedAt)}` : ""}` : `queued ${clock(s.createdAt)}`} {s.exitCode != null ? ` · exit ${s.exitCode}` : ""} {s.attempts > 1 ? ` · attempt ${s.attempts}` : ""} {s.startedAt && ( log )} {(s.state === "queued" || s.state === "blocked" || (s.state === "running" && !s.cancelRequested)) && ( )} {(s.state === "failed" || s.state === "cancelled") && ( )}
    {s.error && (

    {displayText(s.error)}

    )}
  • ))}
)}
); }