"use client"; import { useRouter } from "next/navigation"; import { useEffect, useState } from "react"; import { buttonVariants } from "@/components/ui/button"; import { Dialog } from "@/components/ui/dialog"; import { useHydrated } from "@/components/ui/use-hydrated"; import type { RefreshPlan } from "yt-dlp-transcript-common/lib/report/draft/refresh/types"; // --------------------------------------------------------------------------- // REFRESH, plan then confirm (plans/umtool-redesign.md, R4). The plan is the // editor's dry run (`report-draft {"task": "refresh", "dryRun": true}`, through // /api/articles/refresh): the iteration refreshed and what it is searched // again with, each channel's fetches (or why one is skipped), the site built // locally, the model and the cap. Nothing is written until **Refresh** -- // then it is one editor job (the Running list), and the Overview shows what // it found. // // The ⋯ menu's item (RefreshMenuItem) and the Next card's action (`?refresh=1`) // open the one dialog the page holds (RefreshHost): a menu item's own dialog // would close with the menu. // --------------------------------------------------------------------------- const OPEN_EVENT = "umtool:refresh"; const usd = (n: number) => `$${n.toFixed(2)}`; const day = (iso: string | null | undefined) => (iso ? iso.slice(0, 10) : ""); function Row({ k, children }: { k: string; children: React.ReactNode }) { return ( <>
{k}
{children}
); } function PlanBody({ p }: { p: RefreshPlan }) { const fetched = p.channels.filter((c) => !c.skip); return (
i{p.parent.n} ({p.parent.kind}, made {day(p.parent.at)}) {p.parent.question ? {p.parent.question} : null} {p.parent.queries.map((q) => `“${q}”`).join(", ")} against {p.parent.knownCount.toLocaleString("en-US")} recording{p.parent.knownCount === 1 ? "" : "s"} matched then · {p.parent.citedCount} cited quote{p.parent.citedCount === 1 ? "" : "s"} read again {!p.fetch ? ( "none: the corpus is read as it stands" ) : ( )} {p.build.site ? ( <> {p.build.site}, locally, if anything changed — never deployed ) : ( (p.build.reason ?? "none") )} {p.build.builtAt ? built {day(p.build.builtAt)} : null} {p.model} {p.extractor ? ( <> {" "} · extractor {p.extractor} ) : null} {p.capUsd !== null ? ( <> {" "} · spends up to {usd(p.capUsd)} ) : ( " · the local model, no cap" )} only when something changed: a sweep of the new recordings, then a revision · about {usd(p.estimate.perRecordingUsd)} a recording {p.estimate.from === "parent" ? ` (i${p.parent.n}'s rate)` : ""} + {usd(p.estimate.revisionUsd)} {p.daily ? ` · today ${usd(p.daily.usd)} of ${usd(p.daily.cap)}` : ""}
); } async function post(body: unknown): Promise { const r = await fetch("/api/articles/refresh", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }); const j = (await r.json().catch(() => null)) as (T & { error?: string }) | null; if (!r.ok) throw new Error(j?.error ?? `HTTP ${r.status}`); return j as T; } export function RefreshDialog({ article, open, onClose, onDone }: { article: string; open: boolean; onClose: () => void; onDone: (what: string) => void }) { const [plan, setPlan] = useState(null); const [error, setError] = useState(null); const [working, setWorking] = useState(false); useEffect(() => { setPlan(null); setError(null); if (!open) return; let live = true; post<{ plan: RefreshPlan }>({ article }) .then((r) => live && setPlan(r.plan)) .catch((e: Error) => live && setError(e.message)); return () => { live = false; }; }, [article, open]); const confirm = async () => { if (!plan) return; setWorking(true); setError(null); try { const r = await post<{ jobId: string; iteration: number }>({ article, iteration: plan.parent.n, confirm: true }); onDone(`Refresh i${r.iteration} is running (job ${r.jobId})`); } catch (e) { setError((e as Error).message); } finally { setWorking(false); } }; const blocked = !plan || plan.refusals.length > 0; return ( {!plan && !error && (

Reading the plan…

)} {plan && } {plan && plan.refusals.length > 0 && (
    {plan.refusals.map((r) => (
  • {r}
  • ))}
)} {plan && plan.warnings.length > 0 && (
    {plan.warnings.map((w) => (
  • {w}
  • ))}
)} {error && (

{error}

)}
); } /** The ⋯ menu's item: opens the page's one Refresh dialog. */ export function RefreshMenuItem({ article }: { article: string }) { const hydrated = useHydrated(); return ( ); } /** The Iterations tab's Refresh: the same dialog, from the tab's own line. */ export function RefreshButton({ article }: { article: string }) { const hydrated = useHydrated(); return ( ); } /** The page's Refresh dialog: opened by the menu item, or on arrival with `?refresh=1` (the Next card's action). */ export function RefreshHost({ article, initialOpen = false, href }: { article: string; initialOpen?: boolean; href: string }) { const router = useRouter(); const [open, setOpen] = useState(initialOpen); const [said, setSaid] = useState(null); // A client navigation to `?refresh=1` on the page already showing (the Next // card's action) keeps this mounted: open on the new address too. useEffect(() => { if (initialOpen) setOpen(true); }, [initialOpen]); useEffect(() => { const onOpen = (e: Event) => { if ((e as CustomEvent<{ article?: string }>).detail?.article === article) { setSaid(null); setOpen(true); } }; window.addEventListener(OPEN_EVENT, onOpen); return () => window.removeEventListener(OPEN_EVENT, onOpen); }, [article]); const close = () => { setOpen(false); // The address no longer opens it. if (initialOpen) router.replace(href, { scroll: false }); }; return ( <> { setSaid(what); setOpen(false); router.replace(href, { scroll: false }); router.refresh(); }} /> {said && (

{said}

)} ); }