"use client"; import { Fragment, useState } from "react"; import { Button } from "@/components/ui/button"; import { Table, TD, TH } from "@/components/ui/table"; import { cn } from "@/lib/utils"; import { caseStageApi } from "@/lib/research/case-stage-hrefs"; import type { LeadContext, LeadRow, LeadsView } from "@/lib/research/case-leads"; import type { LeadDisposition } from "yt-dlp-transcript-common/lib/research/caseTypes"; // STAGE 8, LEADS (release 38 lane C): the accounts and URLs a posts or forum // channel links most, with context on demand, a role note and a disposition // each. never-archive withholds the lead for good (its name becomes one of // the case's private names, as a hash); archive creates its channel through // the editor, on no site, and starts its first fetch. const DISPOSITION_LABEL: Record = { archive: "Archive", watch: "Watch", "never-archive": "Never archive", }; async function call(url: string, init?: { method?: string; body?: unknown }): Promise { const res = await fetch(url, { method: init?.method ?? "GET", headers: init?.body !== undefined ? { "content-type": "application/json" } : undefined, body: init?.body !== undefined ? JSON.stringify(init.body) : undefined, cache: "no-store", }); const body = await res.json().catch(() => ({})); if (!res.ok) throw new Error(body?.error ?? `HTTP ${res.status}`); return body as T; } export default function LeadsPanel({ caseId, initial }: { caseId: string; initial: LeadsView }) { const [view, setView] = useState(initial); const [error, setError] = useState(null); const [note, setNote] = useState(null); const [busy, setBusy] = useState(null); const [roles, setRoles] = useState>({}); const [context, setContext] = useState>({}); const load = async (channel: string) => { setBusy("scan"); setError(null); try { setView(await call(`${caseStageApi(caseId, "leads")}?channel=${encodeURIComponent(channel)}`)); setContext({}); } catch (e) { setError((e as Error).message); } finally { setBusy(null); } }; // `disposition` undefined: the role alone changes; "" makes the lead undecided again. const decide = async (lead: LeadRow, disposition: LeadDisposition | "" | undefined, role?: string) => { setBusy(lead.key); setError(null); setNote(null); try { const r = await call<{ view: LeadsView; created?: { slug: string; jobIds: string[] } }>(caseStageApi(caseId, "leads"), { method: "POST", body: { key: lead.key, ...(disposition !== undefined ? { disposition: disposition || null } : {}), ...(role !== undefined ? { role } : {}), url: lead.url, count: lead.count, channel: view.channel, }, }); setView(r.view); if (r.created) setNote(`Created ${r.created.slug} on no site${r.created.jobIds.length ? "; its first fetch is queued" : ""}.`); else if (disposition === "never-archive") setNote("Withheld: the name is one of the case's private names now, kept as a hash."); } catch (e) { setError((e as Error).message); } finally { setBusy(null); } }; const older = async (lead: LeadRow) => { setBusy(lead.key); setError(null); try { const r = await call<{ jobId: string; slug: string }>(caseStageApi(caseId, "leads/older"), { method: "POST", body: { key: lead.key } }); setNote(`The older walk of ${r.slug} is queued.`); } catch (e) { setError((e as Error).message); } finally { setBusy(null); } }; const toggleContext = async (lead: LeadRow) => { if (context[lead.key]) { const { [lead.key]: _gone, ...rest } = context; setContext(rest); return; } setContext({ ...context, [lead.key]: "loading" }); try { const c = await call( `${caseStageApi(caseId, "leads/context")}?channel=${encodeURIComponent(view.channel ?? "")}&key=${encodeURIComponent(lead.key)}`, ); setContext((m) => ({ ...m, [lead.key]: c })); } catch (e) { setError((e as Error).message); } }; const listed = view.leads.filter((l) => !l.withheld); const withheld = view.leads.filter((l) => l.withheld); return (

{view.posts} posts read · {listed.length} leads {view.withheld > 0 && <> · {view.withheld} withheld (the case's private names)}

{note &&

{note}

} {error &&

{error}

} {!view.channel &&

No posts or forum channel is in the case's scope: pick one.

} {view.channel && ( {listed.map((l) => ( {context[l.key] && ( )} ))} {withheld.map((l, i) => ( ))}
Lead Platform Posts Role Disposition Channel
{l.key} {l.platform} {l.count} setRoles({ ...roles, [l.key]: e.target.value })} onBlur={() => roles[l.key] !== undefined && roles[l.key] !== (l.role ?? "") && decide(l, undefined, roles[l.key])} className="w-56 rounded-[3px] border border-seam bg-ground px-1.5 py-0.5 text-[12.5px] text-text" /> {l.channel ? ( {l.channel} ) : ( — )}
{context[l.key] === "loading" ? ( Reading the posts… ) : ( )}
[name withheld] {l.platform} {l.count} {l.role || "kept as a hash"} {DISPOSITION_LABEL["never-archive"]} never withheld lead {i + 1}
)}
); }