"use client"; import Link from "next/link"; import Form from "next/form"; import { useRouter } from "next/navigation"; import { useState, useTransition } from "react"; import { StreamActionLog } from "yt-dlp-transcript-common/components/StreamActionLog"; import { cdxTsDate } from "yt-dlp-transcript-common/lib/waybackPages/scanFile"; import { IMPORT_CONFIRM_ABOVE, type CaptureFilter, type CaptureRowView, } from "yt-dlp-transcript-common/lib/waybackPages/scanView"; import type { CaptureImportResult } from "yt-dlp-transcript-common/controller/waybackImport"; import { importCapturesAction, setWaybackAutoAction, waybackScanAction } from "../actions"; import { cancelJobAction } from "../../../../jobs/actions"; // THE CAPTURES TAB'S PANEL (release 37 S2). The server hands it one page of // already-filtered rows; the filter is the URL's (a GET form), so a 50,000-row // scan never reaches the browser. Selection is per page; Import all in range // is the filter, sent back and re-applied on the server. export type CapturesStatus = { scanned: boolean; versions: number; pages: number; imported: number; newRows: number; lastScanAt?: string; lastScanJobId?: string; pass: number; complete: boolean; targets: { url: string; match: string; complete: boolean; rows: number; resumable: boolean; excluded?: string }[]; auto: boolean; liveScanJobId?: string; }; type Props = { slug: string; status: CapturesStatus; rows: CaptureRowView[]; matching: number; matchingToImport: number; page: number; pages: number; filter: CaptureFilter; years: string[]; prefixes: string[]; }; const n = (x: number) => x.toLocaleString("en-US"); const plural = (x: number, one: string, many = `${one}s`) => `${n(x)} ${x === 1 ? one : many}`; const when = (iso: string) => `${iso.slice(0, 16).replace("T", " ")} UTC`; function hrefWith(slug: string, filter: CaptureFilter, page?: number): string { const q = new URLSearchParams(); if (filter.year) q.set("year", filter.year); if (filter.prefix) q.set("prefix", filter.prefix); if (filter.q) q.set("q", filter.q); if (filter.show && filter.show !== "all") q.set("show", filter.show); if (page && page > 1) q.set("page", String(page)); const s = q.toString(); return `/channels/${slug}/captures${s ? `?${s}` : ""}`; } function importLine(r: CaptureImportResult): string { if (!r.ok) return r.error ?? "The import was refused."; const parts = [`Queued ${plural(r.queued.length, "import job")}`]; const imported = r.skipped.filter((s) => s.why.startsWith("imported")).length; if (imported) parts.push(`${n(imported)} already imported`); const gone = r.skipped.length - imported; if (gone) parts.push(`${n(gone)} no longer in the scan`); if (r.left) parts.push(`${n(r.left)} left: import the range again for the rest`); return `${parts.join("; ")}.`; } export function CapturesPanel({ slug, status, rows, matching, matchingToImport, page, pages, filter, years, prefixes }: Props) { const router = useRouter(); const [selected, setSelected] = useState>(new Set()); const [confirming, setConfirming] = useState(false); const [result, setResult] = useState(null); const [pending, startTransition] = useTransition(); // Shown at once; the server's answer arrives with the refresh. const [auto, setAuto] = useState(status.auto); const selectable = rows.filter((r) => !r.imported); const allOnPage = selectable.length > 0 && selectable.every((r) => selected.has(r.key)); const toggle = (key: string) => setSelected((s) => { const next = new Set(s); if (next.has(key)) next.delete(key); else next.add(key); return next; }); const run = (choice: { keys: string[] } | { filter: CaptureFilter }) => startTransition(async () => { const r = await importCapturesAction(slug, choice); setResult(r); setConfirming(false); if (r.ok) setSelected(new Set()); router.refresh(); }); const scanLabel = !status.scanned || status.complete ? "Scan now" : "Continue scan"; const statusLine = !status.scanned ? "Not scanned yet" : [ `${plural(status.versions, "version")} of ${plural(status.pages, "page")}`, `${n(status.imported)} imported`, ...(status.newRows ? [`${n(status.newRows)} new since the last scan`] : []), ...(status.lastScanAt ? [`last scan ${when(status.lastScanAt)}`] : []), status.complete ? "complete" : `continues where it stopped (${status.targets.filter((t) => !t.complete).length} of ${status.targets.length} targets left)`, ].join(" · "); return (

{statusLine}

    {status.targets.map((t) => (
  • {t.url} ({t.match}) — {t.excluded ? {t.excluded} : t.complete ? "complete" : t.resumable ? `${n(t.rows)} rows read, continues` : "not scanned this pass"}
  • ))}
{status.liveScanJobId ? (

A scan is queued or running:{" "} {status.liveScanJobId}

) : ( waybackScanAction(slug)} cancelAction={cancelJobAction} buttonLabel={scanLabel} runningLabel="Scanning…" label="Wayback scan" onSettled={() => router.refresh()} /> )} {status.lastScanJobId ? (

Last scan:{" "} {status.lastScanJobId}

) : null}
{status.scanned ? (
Clear
{plural(matching, "row")} match{matching === 1 ? "es" : ""} {matching !== matchingToImport ? ` (${n(matchingToImport)} not imported)` : ""} {confirming ? ( Import {plural(matchingToImport, "capture")}, one job each, at archive.org's pace? ) : ( )}
{result ? (

{importLine(result)}{" "} {result.ok && result.queued.length > 0 ? ( Jobs ) : null}

) : null}
{rows.map((r) => ( ))}
setSelected((s) => { const next = new Set(s); for (const r of selectable) { if (allOnPage) next.delete(r.key); else next.add(r.key); } return next; }) } /> Path First seen Last seen Version Imported New
toggle(r.key)} /> {r.path || "/"} {cdxTsDate(r.firstTs)} {cdxTsDate(r.lastTs)} {r.version} of {r.versions} {r.imported ? ( r.imported.waybackUrl ? ( ✓ {r.imported.id} ) : ( ✓ {r.imported.id} ) ) : ( — )} {r.isNew ? new : null}
{pages > 1 ? ( ) : null}
) : null}
); }