"use client"; import { useEffect, useState } from "react"; import { Button } from "@/components/ui/button"; import { Input, Select } from "@/components/ui/field"; // IMPORT THIS CAPTURE (release 37 S4): a page citation's Wayback copy imported // into a Wayback pages channel (picked, or created for the page's host) as a // page record — the editor's import-page, one job at archive.org's pace — and // then the citation rewritten as a post citation of that record, the page kept // as its fallback (/api/articles/import-capture). A site report is built from // its draft: the replacement citation is shown for the draft instead. type Channel = { slug: string; name: string }; type Rec = { id: string; title?: string; text: string }; type Status = { status: string; done: boolean; ok?: boolean; error?: string; records: Rec[] }; const NEW = "__new__"; async function call(init?: RequestInit, query = ""): Promise { const r = await fetch(`/api/articles/import-capture${query}`, { cache: "no-store", ...init }); const j = await r.json().catch(() => ({})); if (!r.ok) throw new Error([j.error ?? r.statusText, ...((j.problems as string[] | undefined) ?? [])].join(" — ")); return j as T; } export default function ImportCapture({ site, report, cite, writable, host, }: { site: string; report: string; cite: string; writable: boolean; host: string; }) { const [open, setOpen] = useState(false); const [channels, setChannels] = useState(null); const [pick, setPick] = useState(""); const [name, setName] = useState(host ? `${host} pages` : ""); const [job, setJob] = useState<{ slug: string; jobId: string } | null>(null); const [status, setStatus] = useState(null); const [error, setError] = useState(null); const [busy, setBusy] = useState(false); const [used, setUsed] = useState<{ written: boolean; n?: number; citation: unknown } | null>(null); useEffect(() => { if (!open || channels) return; call<{ channels: Channel[] }>() .then((j) => { setChannels(j.channels); setPick(j.channels[0]?.slug ?? NEW); }) .catch((e) => setError((e as Error).message)); }, [open, channels]); // Follow the import job until it ends. useEffect(() => { if (!job || status?.done) return; const t = setInterval(() => { call(undefined, `?${new URLSearchParams({ job: job.jobId, slug: job.slug })}`) .then(setStatus) .catch((e) => setError((e as Error).message)); }, 1500); return () => clearInterval(t); }, [job, status?.done]); const start = async () => { setBusy(true); setError(null); try { const body = pick === NEW ? { newChannel: { name } } : { slug: pick }; const j = await call<{ slug: string; jobId: string }>({ method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ action: "start", site, report, cite, ...body }), }); setStatus(null); setJob(j); } catch (e) { setError((e as Error).message); } finally { setBusy(false); } }; const use = async (id: string) => { if (!job) return; setBusy(true); setError(null); try { setUsed( await call({ method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ action: "use", site, report, cite, slug: job.slug, id }), }), ); } catch (e) { setError((e as Error).message); } finally { setBusy(false); } }; if (!open) { return ( ); } return (
Import this capture
{!job && ( <> {pick === NEW && ( setName(e.target.value)} placeholder="name" /> )} )} {job && !status?.done && (
importing into {job.slug}… {status?.status ?? "queued"}
)} {status?.done && !status.ok &&
{status.error ?? `the import ${status.status}`}
} {status?.done && status.ok && !used && (
{status.records.length} record{status.records.length === 1 ? "" : "s"} in {job?.slug}: cite one
    {status.records.map((r) => (
  • {r.id} {r.title && — {r.title}} {r.text && {r.text}}
  • ))}
)} {used?.written && (
{cite} now cites the page record; written to i{used.n} (the page is kept as its fallback).
)} {used && !used.written && (
{writable ? "" : "This report is built from its draft: put this citation in the draft as "} {cite}:
            {JSON.stringify(used.citation, null, 2)}
          
)} {error &&
{error}
}
); }