"use server"; // THE CAPTURES TAB'S ACTIONS (release 37 S2), and `pnpm ops wayback-scan` / // `wayback-import` over HTTP: one code path for the button and the agent. // // waybackScanAction a `wayback-scan` job on archive.org's queue // (controller/waybackScan.ts) — Scan now / Continue scan. // importCapturesAction one `import-page` job per chosen row // (controller/waybackImport.ts) — Import selected (keys) // and Import all in range (the table's filter). // // Refusals are the controllers' sentences. A scan or an import that wrote // anything revalidates the tab. import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { startWaybackScanJob } from "yt-dlp-transcript-common/controller/waybackScan"; import { queueCaptureImports, type CaptureImportResult } from "yt-dlp-transcript-common/controller/waybackImport"; import type { CaptureFilter } from "yt-dlp-transcript-common/lib/waybackPages/scanView"; import type { StreamActionResult } from "yt-dlp-transcript-common/jobs/streamCommand"; import { safeRevalidate } from "../../../lib/safeRevalidate"; import { patchChannelConfig, readChannelConfig } from "yt-dlp-transcript-common/controller/channels"; const revalidate = (slug: string) => safeRevalidate([`/channels/${slug}`, `/channels/${slug}/captures`]); export async function waybackScanAction( slug: string, opts: { dryRun?: boolean; maxRows?: number; full?: boolean } = {}, ): Promise { return startWaybackScanJob( { slug, ...(opts.dryRun ? { dryRun: true } : {}), ...(opts.maxRows !== undefined ? { maxRows: opts.maxRows } : {}), ...(opts.full ? { full: true } : {}), }, { paths: getPaths(), onDone: revalidate }, ); } // The button's form: a job id, or the refusal. export async function startWaybackScanFromTab(slug: string, full: boolean): Promise<{ ok: true; jobId: string } | { ok: false; error: string }> { const r = await waybackScanAction(slug, full ? { full: true } : {}); if (!r.ok) return { ok: false, error: r.error }; // Nobody reads the stream: the log is on disk, the tab polls nothing. void r.stream.cancel().catch(() => {}); return { ok: true, jobId: r.jobId }; } export async function importCapturesAction( slug: string, choice: { keys: string[] } | { filter: CaptureFilter }, opts: { dryRun?: boolean; requestedBy?: string } = {}, ): Promise { const r = await queueCaptureImports({ paths: getPaths(), slug, ...("keys" in choice ? { keys: choice.keys } : { filter: choice.filter }), ...(opts.dryRun ? { dryRun: true } : {}), requestedBy: opts.requestedBy ?? "captures", }); if (r.queued.length > 0 && !r.dryRun) revalidate(slug); return r; } // The Captures tab's toggle: `wayback.auto` on or off, through the one channel // config writer (the rest of `wayback` kept as it is). export async function setWaybackAutoAction(slug: string, auto: boolean): Promise<{ ok: boolean; error?: string }> { const paths = getPaths(); const config = await readChannelConfig(paths, slug); if (!config?.wayback) return { ok: false, error: `${slug} is not a Wayback pages channel.` }; const { auto: _was, ...rest } = config.wayback; await patchChannelConfig(paths, slug, { wayback: { ...rest, ...(auto ? { auto: true } : {}) } }); revalidate(slug); return { ok: true }; }