import { readdir } from "node:fs/promises"; import path from "node:path"; import Link from "next/link"; import { notFound } from "next/navigation"; import { Suspense } from "react"; import { readRevisionHead, REPORT_HISTORY_GIT_DIRNAME } from "yt-dlp-transcript-common/publish/reportHistory"; import { hasSlides, slideLint } from "yt-dlp-transcript-common/lib/report/slides"; import type { Crumb } from "@/components/shell/AppHeader"; import DeleteArticle from "@/components/articles/DeleteArticle"; import { RefreshHost, RefreshMenuItem } from "@/components/articles/RefreshDialog"; import ArticleChecks from "@/components/articles/ArticleChecks"; import ArticleOverview from "@/components/articles/ArticleOverview"; import ArticleReader from "@/components/articles/ArticleReader"; import WorkspacePanel from "@/components/articles/WorkspacePanel"; import IterationsView from "@/components/articles/IterationsView"; import PieceFrame from "@/components/piece/PieceFrame"; import PolicyChip, { PolicyChipShell } from "@/components/piece/PolicyChip"; import ScheduleControls from "@/components/series/ScheduleControls"; import { livingScheduleOf } from "@/lib/schedules"; import { seriesHref } from "@/lib/hrefs"; import DeleteProject from "@/components/projects/DeleteProject"; import ReportTabBody, { reportCounts } from "@/components/projects/report/ReportTabBody"; import { badgeVariants } from "@/components/ui/badge"; import { EmptyState } from "@/components/ui/empty-state"; import { Tabs } from "@/components/ui/tabs"; import { articleView } from "@/lib/articles/article"; import { articleWorkspaceListing, openWorkspaceFile } from "@/lib/articles/files"; import { linkedProjects } from "@/lib/articles/links.mjs"; import { readArticleNotes, readReportFile, reportDirFor } from "@/lib/articles/sites"; import { sourceFor } from "@/lib/articles/sources.mjs"; import { readArticleProposals } from "@/lib/articles/verdicts"; import VerdictProposals from "@/components/articles/VerdictProposals"; import { articleEvidenceHref, articleHref, projectHref, workspaceHref } from "@/lib/hrefs"; import { ARTICLES_KEY, corpusNotesFile } from "@/lib/paths"; import { readPieceGraph } from "@/lib/pieces"; import { articleVideo, projectNoteTab, readArticlePieceFrame, type ArticleVideo } from "@/lib/pieces/article"; import { ARTICLE_PIECE_TABS, VIDEO_TAB_IDS, articleTabOf } from "@/lib/pieces/tabs.mjs"; import { projectRef } from "@/lib/projects"; import { readClipDetail } from "@/lib/projects/report.mjs"; import type { NotesRead } from "@/lib/annotations/types"; // ONE ARTICLE PIECE, wherever the article lives: a site's report // (/sites//) or one of the article store's (/articles/, // release 26) -- `site` is the site's id or ARTICLES_KEY. Since P4a-2 // (plans/umtool-redesign.md) the article and the video project it claims are // ONE page: the piece frame (components/piece/PieceFrame.tsx) around one of // nine tabs. // // Overview the Next card, the iterations, the article's sections, the Cut // Article the reader with its notes; `?tab=source` its workspace // files, its draft marked, and what would make its slides // read badly (the old page's three views, unchanged) // Iterations the article store's iterations (IterationsView, release 27/28) // Cut … History the claimed video project's P4a-1 tabs (ReportTabBody): // current.video's project, else the article's Cut // (lib/pieces/article.ts); one outside REPORTS_ROOT is "not // reachable here" // // ?tab= the tab (absent: the Overview) // ?rv=slides|iso the article as slides, or the overview (the // reader's own switch; the place is the hash) // ?status=open|resolved|all the notes filter it opens with // ?note= the note it opens on: an article note on the // Article tab, a video note where its anchor is // ?ws=&rel= a workspace file open on the source view // ?project= another video project the article owns // ?all=1 the cut's every quote // ?heard= (with tab=iterations) Fix a mishearing open, // its heard field filled (release 32 R2) // ?refresh=1 the Refresh plan open (R4: the suggestion // card's action) export type ArticleSearch = { tab?: string; status?: string; note?: string; ws?: string; rel?: string; rv?: string; all?: string; project?: string; refresh?: string; heard?: string; }; type TabHref = (t: string, q?: { all?: boolean; project?: string }) => string; const NUMERIC = /^[1-9][0-9]*$/; /** How many iterations the store holds for an article (a readdir; the tab reads them whole). */ async function iterationCount(site: string, report: string): Promise { if (site !== ARTICLES_KEY) return 0; const dir = path.join(/* turbopackIgnore: true */ reportDirFor(site, report), "iterations"); const names = await readdir(/* turbopackIgnore: true */ dir).catch(() => [] as string[]); return names.filter((n) => NUMERIC.test(n)).length; } export default async function ArticlePage({ site, reportId, sp, home, crumbs, published, }: { // The site's id, or ARTICLES_KEY. site: string; reportId: string; sp: ArticleSearch; // The head's meta line's first link: the site, or the article list. home: { href: string; label: string }; // The trail before the article's own crumbs. crumbs: Crumb[]; published: boolean; }) { const read = await readReportFile(site, reportId); const store = site === ARTICLES_KEY; const iterations = await iterationCount(site, reportId); const hasIterations = iterations > 0; const missing = !read.report && read.problems[0]?.message === "no report.json"; // A living report waiting for its first tick (R5) has no iteration yet, and // is a page all the same: its schedule is. const living = store ? await livingScheduleOf(reportId).catch(() => null) : null; if (missing && !hasIterations && !living) notFound(); const r = read.report; const a = { site, id: reportId }; const [notes, source, frame, graph] = await Promise.all([ readArticleNotes(site, reportId), sourceFor(site, reportId), readArticlePieceFrame(site, reportId, { hasIterations, hasReport: !!r }), readPieceGraph(), ]); // A living report's schedule (R5): its chip in the head. const schedule = living ? { id: living.id, shape: living.shape, chip: living.chip, paused: living.paused, cadence: living.cadence, running: !!living.running } : null; // An edition's series (R5). const seriesId = frame.piece?.series ?? null; const series = seriesId ? graph.byId.get(`s:${seriesId}`) : undefined; const edition = seriesId ? { title: series?.title ?? seriesId, href: seriesHref(seriesId) } : null; const video = frame.piece ? await articleVideo(frame.piece, graph, sp.project ?? null) : null; const videoRef = video?.project ? await projectRef(video.project.id) : null; const detail = videoRef ? await readClipDetail(videoRef.dir) : null; // A note: the article's own opens the reader; a note on the video project // opens where its anchor is drawn. const articleNote = !!sp.note && !!notes.doc?.notes.some((n) => n.id === sp.note); const noteTab = sp.note && !sp.tab && !articleNote && videoRef ? await projectNoteTab(videoRef.dir, sp.note, { piece: "article" }) : null; const tab = articleTabOf(sp, { noteTab }); // `?project=` is carried from tab to tab only when it is not the article's Cut. const project = video?.project && video.project.id !== video.cut ? video.project.id : undefined; const tabHref: TabHref = (t, q = {}) => articleHref(a, { tab: t, all: q.all, project: q.project === undefined ? project : q.project || undefined }); const title = r?.title || frame.piece?.title || reportId; const counts = { ...(detail && videoRef ? await reportCounts(detail, videoRef.dir) : {}), ...(iterations ? { iterations } : {}) }; const openNotes = notes.doc?.notes.filter((n) => n.status === "open").length ?? 0; const gitDir = path.join(/* turbopackIgnore: true */ reportDirFor(site, reportId), REPORT_HISTORY_GIT_DIRNAME); const head = await readRevisionHead(gitDir).catch(() => null); const meta = (
{/* The home link, unless the trail already has it (the store's "articles"). */} {!crumbs.some((c) => c.href === home.href) && ( {home.label} )} {missing ? "in the making" : published ? "published" : "draft"} {r?.published && published {r.published}} {r?.updated && updated {r.updated}} {head && {head.revision} revision{head.revision === 1 ? "" : "s"}}
); return (
({ ...t, href: tabHref(t.id) }))} counts={counts} meta={meta} note={`${openNotes} open notes`} head={ … } > } schedule={schedule} edition={edition} more={ <> {frame.pieceId} {store && hasIterations && r && } {store && } {schedule && } {videoRef && ( <> {videoRef.id} {/* Its video's workspace (L7a): the project's own address -- the one way there from a claimed piece. */} Open the workspace )} } > {store && hasIterations && r && } {tab === "overview" && ( s.title || `Section ${i + 1}`) : null} frame={frame} video={video} videoRef={videoRef} detail={detail} tabHref={tabHref} /> )} {tab === "article" && ( )} {tab === "iterations" && (
{store ? ( ) : ( Iterations are the article store's. )}
)} {VIDEO_TAB_IDS.includes(tab) && (videoRef && detail ? ( ) : (
))}
); } /** The video tabs of an article with no video on show: none yet, not reachable here, or a manifest that does not parse. */ function NoVideo({ video, unparsed, iterationsHref }: { video: ArticleVideo | null; unparsed: string | null; iterationsHref: string | null }) { if (unparsed) { return (

{unparsed}/video.manifest.json could not be parsed.

); } if (video?.unreachable) { return ( {video.unreachable.dir} is outside this umtool's reports root. ); } return ( Make the video from an iteration ) : undefined } /> ); } /** * The Article tab: the old article page's views, moved in unchanged -- the * reader with its notes, the workspace source (`?tab=source`), and the * report's problems when it does not read. Its first line is the views' * switch (article · source · evidence) and the projects linked to it. */ async function ArticleTab({ site, reportId, sp, read, missing, notes, source, tabHref, cut, }: { site: string; reportId: string; sp: ArticleSearch; read: Awaited>; missing: boolean; notes: Awaited>; source: Awaited>; tabHref: TabHref; cut: string | null; }) { const a = { site, id: reportId }; const r = read.report; const view = sp.tab === "source" ? "source" : "article"; const links = await linkedProjects(site, reportId, { workspace: source?.workspace ?? null }); const media = (file: string) => `/api/sites/media?site=${encodeURIComponent(site)}&report=${encodeURIComponent(reportId)}&file=${encodeURIComponent(file)}`; const line = (
{/* The article's views as link tabs: each an address, the current one lit. */} {links.linked.map((p: { id: string }) => ( {p.id} ))} {links.possible.map((p: { id: string }) => ( {p.id} (possible) ))} {read.report && } {source && (
{source.draft && draft {source.draft}} {source.generator && generator {source.generator}}
)}
); if (!r) { return (
{line} {missing ? ( Its iterations } /> ) : ( <>

report.json does not read as a report:

    {read.problems.slice(0, 20).map((p, i) => (
  • {p.message}
  • ))}
)}
); } if (view === "source") { const ws = await articleWorkspaceListing(source?.workspace); const opened = sp.ws && sp.rel ? await openWorkspaceFile(sp.ws, sp.rel) : ws && source?.draft ? await openWorkspaceFile(ws.name, path.relative(ws.dir, source.draft)) : null; const draftRel = ws && source?.draft ? `${ws.name}/${path.relative(ws.dir, source.draft)}` : null; const { view: slideView } = await articleView(r); const lint = hasSlides(slideView) ? slideLint(slideView) : []; const slideProblems = read.problems.filter((p) => /(^|\.)slides?(\.|\[|$)/.test(p.path ?? "")); const base = articleHref(a); return (
{line} {hasSlides(slideView) && (
slides
{lint.length + slideProblems.length === 0 ? (

nothing to fix

) : (
    {slideProblems.map((p, i) => (
  • {p.path} {p.message}
  • ))} {lint.map((l, i) => (
  • {l.key} {" "} {l.message}
  • ))}
)}
)} articleHref(a, { tab: "source", ws: w, rel })} />
); } const { view: readerView, error } = await articleView(r); const initialNotes: NotesRead = { subject: { kind: "article", site: site, report: reportId }, file: corpusNotesFile(site, reportId) ?? "", token: notes.token, doc: notes.doc, source: notes.doc?.source ?? source ?? null, ...(notes.error ? { error: notes.error } : {}), }; const status = sp.status === "resolved" || sp.status === "all" ? sp.status : "open"; // A fact-check's proposed verdicts (release 29 M8), waiting on the operator. const proposals = site === ARTICLES_KEY && r.kind === "factcheck" ? await readArticleProposals(reportId).catch(() => null) : null; const videoSlot = r.video ? { file: r.video.src, src: media(r.video.src), poster: r.video.poster ? media(r.video.poster) : undefined, caption: r.video.caption } : null; return ( {line} ) : ( line ) } video={videoSlot} videoProjects={links.linked.map((p: { id: string; title: string; generatedBy: string | null }) => ({ id: p.id, title: p.title, generatedBy: p.generatedBy }))} /> ); }