import { readdir, readFile } from "node:fs/promises"; import path from "node:path"; import { fmtAgo } from "@/lib/format"; import { Markdown } from "@/lib/markdown"; import { diffManifests, formatChange } from "@/lib/report/manifest-diff.mjs"; import { listSnapshots, readSnapshot } from "@/lib/report/snapshots.mjs"; import SnapshotButton from "../SnapshotButton"; import { Pill, type ReportTabProps } from "./shared"; // The History tab: the manifest's revisions with what changed since each, // its provenance as written, and the notes beside it (prose fields and the // sibling .md files). Moved from ReportProject unchanged (P4a-1). export default async function HistoryTab({ project, detail }: ReportTabProps) { const { manifest: m } = detail; const p = (m.provenance ?? {}) as Record; const snapshots = await listSnapshots(project.dir); const diffs = await Promise.all( snapshots.map(async (sn) => { try { return diffManifests(await readSnapshot(project.dir, sn.rel), m); } catch { return null; } }), ); // Sibling notes: every .md that is neither the report nor the README. const names = await readdir(project.dir).catch(() => [] as string[]); const noteFiles = names.filter((n) => /\.md$/i.test(n) && !/^readme\.md$/i.test(n) && n !== "sweep-report.md").sort(); const noteTexts = await Promise.all(noteFiles.map((n) => readFile(path.join(project.dir, n), "utf8").catch(() => ""))); // Provenance splits by SHAPE: a scalar or a URL stays in the table; a long // string, or one with a newline, is prose the author wrote and reads as a // paragraph under Notes. No field is moved or rewritten. const isProse = (v: unknown) => typeof v === "string" && (v.length > 120 || v.includes("\n")); const scalars = Object.entries(p).filter(([, v]) => !isProse(v) && (typeof v !== "object" || v === null)); const prose = Object.entries(p).filter(([, v]) => isProse(v)) as [string, string][]; const structured = Object.entries(p).filter(([, v]) => typeof v === "object" && v !== null); return ( <> {/* --- revisions ------------------------------------------------- */}

revisions — {snapshots.length}

{snapshots.length === 0 ? (

no snapshots. One copies video.manifest.json into{" "} revisions/; a build that stamps a deliverable aside takes one of the manifest that made it.

) : ( )}
{/* --- provenance, as written; prose under notes ------------------- */}

provenance

{scalars.map(([k, v]) => (
{k}
{typeof v === "string" && /^https?:\/\//.test(v) ? ( {v} ) : ( String(v) )}
))} {structured.map(([k, v]) => (
{k}
{JSON.stringify(v).slice(0, 200)}
))}
{(prose.length > 0 || noteFiles.length > 0) && (

notes

{prose.map(([k, v]) => (
{k}

{v}

))} {noteFiles.map((n, i) => (
{n}
))}
)} ); }