// THE CAPTURES TABLE (release 37 S2): a Wayback pages channel's scan rows as // the channel page's Captures tab shows them, and the filter the tab, Import // all in range and the download lane's `wayback.auto` pick share. Pure, // client-safe. import { importedRecordOf, type ImportedIndex, type ImportedRecordRef } from "./imported"; import { displayPathOf, hostsOf, isNewRow, type WaybackScanFile, type WaybackScanRow } from "./scanFile"; // The replay URL of a capture (waybackClient.ts's waybackReplayUrl, spelled // here so this module stays client-safe: that one pulls in node:fs). function replayUrl(timestamp: string, original: string): string { return `https://web.archive.org/web/${timestamp}/${original}`; } export type CaptureRowView = { // ` `: what a selection names. key: string; originalUrl: string; path: string; digest: string; firstTs: string; lastTs: string; count: number; // This content's place among the URL's versions (1 = the earliest), of // `versions`. version: number; versions: number; isNew: boolean; imported: ImportedRecordRef | null; // The capture an import of this row takes: the content's first listing. replayUrl: string; }; export type CaptureShow = "all" | "new" | "not-imported" | "imported"; export const CAPTURE_SHOW_VALUES: readonly CaptureShow[] = ["all", "new", "not-imported", "imported"]; export type CaptureFilter = { // YYYY of first seen. year?: string; // A path prefix as the summary names it ("blog.example/post/"): host first, // no scheme, no www. prefix?: string; // Free text, over the original URL (case-insensitive). q?: string; show?: CaptureShow; }; export function captureRowKey(r: Pick): string { return `${r.digest} ${r.originalUrl}`; } // Every row, in table order (by URL, then first seen), with its version // ordinal, new mark and imported record. export function captureRows(file: WaybackScanFile, index: ImportedIndex): CaptureRowView[] { const withHost = hostsOf(file.rows).size > 1; const byUrl = new Map(); for (const r of file.rows) { const list = byUrl.get(r.originalUrl) ?? []; list.push(r); byUrl.set(r.originalUrl, list); } const out: CaptureRowView[] = []; for (const url of [...byUrl.keys()].sort()) { const list = byUrl.get(url)!.sort((a, b) => a.firstTs.localeCompare(b.firstTs)); list.forEach((r, i) => { out.push({ key: captureRowKey(r), originalUrl: r.originalUrl, path: displayPathOf(r.originalUrl, withHost), digest: r.digest, firstTs: r.firstTs, lastTs: r.lastTs, count: r.count, version: i + 1, versions: list.length, isNew: isNewRow(file, r), imported: importedRecordOf(r, index), replayUrl: replayUrl(r.firstTs, r.originalUrl), }); }); } return out; } export function filterCaptureRows(rows: ReadonlyArray, f: CaptureFilter): CaptureRowView[] { const q = f.q?.trim().toLowerCase(); return rows.filter((r) => { if (f.year && r.firstTs.slice(0, 4) !== f.year) return false; if (f.prefix && !displayPathOf(r.originalUrl, true).startsWith(f.prefix)) return false; if (q && !r.originalUrl.toLowerCase().includes(q)) return false; switch (f.show ?? "all") { case "new": return r.isNew; case "not-imported": return !r.imported; case "imported": return !!r.imported; default: return true; } }); } // A filter from a URL's search params (the tab's own links). export function captureFilterFrom(sp: Record): CaptureFilter { const one = (k: string) => { const v = sp[k]; const s = Array.isArray(v) ? v[0] : v; return typeof s === "string" && s.trim() ? s.trim() : undefined; }; const year = one("year"); const show = one("show"); return { ...(year && /^\d{4}$/.test(year) ? { year } : {}), ...(one("prefix") ? { prefix: one("prefix") } : {}), ...(one("q") ? { q: one("q") } : {}), ...(show && (CAPTURE_SHOW_VALUES as readonly string[]).includes(show) ? { show: show as CaptureShow } : {}), }; } // Rows per page of the table. export const CAPTURES_PAGE_SIZE = 100; // Import all in range asks for a confirmation above this many. export const IMPORT_CONFIRM_ABOVE = 50;