"use client"; import { useEffect, useState } from "react"; import { buttonVariants } from "@/components/ui/button"; import { Dialog } from "@/components/ui/dialog"; import { controlClass } from "@/components/ui/field"; import { researchCaseHandoffApi } from "@/lib/research/hrefs"; import type { ArticlePlan, HandoffPlan, VideoPlan } from "@/lib/research/case-types"; // --------------------------------------------------------------------------- // A HAND-OFF from the board, plan then confirm (lib/research/handoff.ts): the // plan is read when the dialog opens and nothing is written until the one // button that names what it does is pressed. An article spends money (the // model and its cap are on the plan); a video writes a project. // --------------------------------------------------------------------------- async function post(url: string, body: unknown): Promise { const r = await fetch(url, { 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; } function Row({ k, children }: { k: string; children: React.ReactNode }) { return ( <>
{k}
{children}
); } function ArticleBody({ p, dates, setDates }: { p: ArticlePlan; dates?: { from: string; to: string }; setDates?: (d: { from: string; to: string }) => void }) { return (
{p.article} {p.articleExists ? "(exists: a new iteration)" : "(new, in the making)"} {p.question} {p.channels.join(", ") || "—"} {dates && setDates ? ( ) : ( (p.dateFrom || p.dateTo) && ( {p.dateFrom ?? "…"} – {p.dateTo ?? "…"} ) )} {p.reading && ( the top {p.reading.recordings} recordings by rarity × hits, plus one per year-quarter a rare term hits (at most {p.reading.spreadMax}) {p.reading.minutes !== undefined ? `; about ${p.reading.minutes} min of model time` : ""} )} {p.seeded !== false ? ( the board as cited markdown: {p.seed.quotes} {p.seed.quotes === 1 ? "quote" : "quotes"}, {p.seed.chars.toLocaleString("en-US")} characters ) : ( {p.source || "the local corpus"} )} {p.model ? {p.model} : p.policyError ? "the editor cannot be asked" : "—"} {p.extractor ? ( <> {" "} · extractor {p.extractor} ) : null} {p.capUsd !== null ? ( <> {" "} · spends up to ${p.capUsd.toFixed(2)} ) : null}
); } function VideoBody({ p }: { p: VideoPlan }) { return (
{p.slug} {p.title} {p.clips.length} — each the cue span of a pinned quote
    {p.clips.map((c) => (
  1. {c.id} {c.channel}/{c.video} {c.start}–{c.end} “{c.quote}”
  2. ))}
{p.siteOrigin ? {p.siteOrigin} : "not set"} {p.writes}
); } export default function HandoffDialog({ caseId, to, onClose, onDone, }: { caseId: string; to: "article" | "video" | "sweep" | null; onClose: () => void; onDone: (href: string, what: string) => void; }) { const [plan, setPlan] = useState(null); const [error, setError] = useState(null); const [working, setWorking] = useState(false); // A scope sweep's dates (D2): the scope's, changeable here for this run. const [dates, setDates] = useState<{ from: string; to: string }>({ from: "", to: "" }); useEffect(() => { setPlan(null); setError(null); if (!to) return; let live = true; post<{ plan: HandoffPlan }>(researchCaseHandoffApi(caseId), { to }) .then((r) => { if (!live) return; setPlan(r.plan); if (r.plan.to === "article") setDates({ from: r.plan.dateFrom ?? "", to: r.plan.dateTo ?? "" }); }) .catch((e: Error) => live && setError(e.message)); return () => { live = false; }; }, [caseId, to]); const confirm = async () => { if (!plan) return; setWorking(true); setError(null); try { const r = await post<{ href: string }>(researchCaseHandoffApi(caseId), { to, confirm: true, ...(plan.to === "article" ? { article: plan.article } : { slug: plan.slug }), ...(to === "sweep" ? { dateFrom: dates.from, dateTo: dates.to } : {}), }); onDone(r.href, plan.to === "article" ? `The sweep for ${plan.article} is queued${to === "sweep" ? ": its run page follows it" : ""}` : `Project ${plan.slug} written`); } catch (e) { setError((e as Error).message); } finally { setWorking(false); } }; const title = to === "article" ? "Draft an article from this board" : to === "sweep" ? "Run a sweep on this scope" : "Start a video from this board"; const blocked = !plan || plan.refusals.length > 0 || (plan.to === "article" && !!plan.policyError); return ( {!plan && !error && (

Reading the plan…

)} {plan && (plan.to === "article" ? : )} {plan && plan.refusals.length > 0 && (
    {plan.refusals.map((r) => (
  • {r}
  • ))}
)} {plan && plan.to === "article" && plan.policyError &&

The editor: {plan.policyError}

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

{error}

)}
); }