"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { Button } from "@/components/ui/button"; import { controlClass } from "@/components/ui/field"; import { cn } from "@/lib/utils"; // CONNECT AN ARCHIVE (demo epic D1), from the Source chip: type an origin, Read // it (its corpus.json: title, channels, videos — nothing saved), Connect. The // editor's `archives` action does both (POST /api/research/archives forwards), // so a connect here is the one /sites and `pnpm ops archives` make. Connected, // the page reloads its sources and the scope moves to the new one. type Probe = { origin: string; title: string; totals: { channels: number; videos: number }; channels: { slug: string; name: string; videoCount: number | null }[] }; const nf = new Intl.NumberFormat("en-US"); export default function ConnectArchive({ onConnected, onCancel }: { onConnected: (handle: string) => void; onCancel: () => void }) { const router = useRouter(); const [origin, setOrigin] = useState(""); const [probe, setProbe] = useState(null); const [error, setError] = useState(""); const [busy, setBusy] = useState(false); async function ask(dryRun: boolean) { setBusy(true); setError(""); try { const r = await fetch("/api/research/archives", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ origin: probe?.origin ?? origin, dryRun }), }); const b = await r.json(); if (!r.ok) throw new Error(b.error ?? `HTTP ${r.status}`); if (dryRun) setProbe(b.probe); else { router.refresh(); onConnected(`remote:${b.archive.origin}`); } } catch (e) { setError((e as Error).message); } finally { setBusy(false); } } return (
{ e.preventDefault(); void ask(!probe); }} onKeyDown={(e) => { if (e.key === "Escape") { e.preventDefault(); onCancel(); } }} >
{ setOrigin(e.target.value); setProbe(null); }} className={cn(controlClass, "w-72 py-[3px] text-[12px]")} />
{probe && (

{probe.title} · {nf.format(probe.totals.channels)} channels · {nf.format(probe.totals.videos)} videos {" "} — {probe.channels.slice(0, 8).map((c) => c.name).join(", ")} {probe.channels.length > 8 ? `, and ${probe.channels.length - 8} more` : ""}

)} {error && (

{error}

)}
); }