import path from "node:path"; import { readNotes } from "../annotations/store.mjs"; import { ARTICLES_KEY } from "../articles/key.mjs"; import { iterationProjects } from "../articles/links.mjs"; import { articleHref, projectHref } from "../hrefs"; import { lifecycleOf, readLifecycle } from "../lifecycle"; import { articlePieceId, readPieceGraph, type Piece, type PieceGraph, type PieceProject } from "../pieces"; import { boardOfArticle } from "./board.mjs"; import { readCases } from "./cases"; import type { PieceFrame } from "./frame"; import { PIECE_ADDRESS_TAB_IDS, PIECE_FOLDED, pieceSteps, stageOfPiece, tabOfNoteAnchor } from "./tabs.mjs"; // --------------------------------------------------------------------------- // THE ARTICLE-ROOTED PIECE (plans/umtool-redesign.md, P4a-2): an article and // the video project(s) it claims are one piece, at the article's address. // // Which project the video tabs show (the plan's linking order): the project // of the iteration `current.video` names; else the article's Cut as // lib/articles/links.mjs picks it (its first linked: iterations newest first, // then a manifest that names it, then a unique slug). Only a project this // piece OWNS counts -- one two articles link belongs to the first that claims // it (lib/pieces.ts). A current video whose projectDir is outside REPORTS_ROOT // is "not reachable here": the video tabs say so rather than show another. // // A project an article claims renders as that article's piece: its own address // (`/projects/`) redirects here with the same `?tab=` (claimedProjectRedirect), // and the benches and kind views that stay at project addresses link back here // (pieceHrefOfProject). `?project=` picks another of the piece's projects // (an article whose iterations made two); it is written only when the project // is not the article's Cut, so the common case keeps the bare address. // // SERVER ONLY. // --------------------------------------------------------------------------- export type ArticleVideo = { /** The project the video tabs show, or null (none, or not reachable here). */ project: PieceProject | null; /** The current video's iteration when its projectDir is outside REPORTS_ROOT, or the only ones there are. */ unreachable: { n: number; dir: string } | null; /** Every project this piece owns, the one on show among them. */ owned: PieceProject[]; /** The project the article's Cut opens with no `?project=`. */ cut: string | null; /** The iteration that made each project, when one did. */ iterationOf: Map; }; type IterationProject = { n: number; dir: string; current: boolean; reachable: boolean; projectId: string | null }; /** * The article piece's video projects and the one on show (`want`, when the * piece owns it, else the article's Cut). */ export async function articleVideo(piece: Piece, graph: PieceGraph, want?: string | null): Promise { const owned = piece.projects.filter((p) => graph.pieceOfProject.get(p.id) === piece.id); const its: IterationProject[] = piece.site === ARTICLES_KEY && piece.report ? ((await iterationProjects().catch(() => new Map())).get(piece.report) ?? []) : []; const iterationOf = new Map(); for (const x of [...its].reverse()) if (x.projectId && !iterationOf.has(x.projectId)) iterationOf.set(x.projectId, x.n); const current = its.find((x) => x.current) ?? null; const currentGone = current && !current.reachable ? { n: current.n, dir: current.dir } : null; const currentOwned = current?.projectId ? owned.find((p) => p.id === current.projectId) : undefined; const cut = currentGone ? null : (currentOwned ?? owned.find((p) => p.cut) ?? owned[0] ?? null)?.id ?? null; const chosen = want && owned.some((p) => p.id === want) ? want : cut; const project = owned.find((p) => p.id === chosen) ?? null; const unreachable = project ? null : (currentGone ?? (owned.length === 0 ? (piece.unreachable[0] ?? null) : null)); return { project, unreachable, owned, cut, iterationOf }; } /** The tab a `?note=` on a PROJECT opens: where the note's anchor is drawn (its notes.json), or null. */ export async function projectNoteTab(dir: string, note: string | undefined, { piece = "project" }: { piece?: "project" | "article" } = {}): Promise { if (!note) return null; const { doc } = await readNotes(path.join(/* turbopackIgnore: true */ dir, "notes.json")).catch(() => ({ doc: null })); const n = doc?.notes?.find((x: { id: string }) => x.id === note) as { anchor?: { kind?: string } } | undefined; return n?.anchor?.kind ? tabOfNoteAnchor(n.anchor.kind, { piece }) : null; } type TabQuery = { tab?: string; all?: boolean; note?: string | null }; /** * Where a project's tab lives: inside its article's piece when an article * claims it, else on its own page. The benches and kind views link back with * this. */ export async function pieceHrefOfProject(projectId: string, q: TabQuery = {}): Promise { const graph = await readPieceGraph().catch(() => null); const piece = graph ? graph.byId.get(graph.pieceOfProject.get(projectId) ?? "") : undefined; // On its own page a folded tab (L7b: Build and Media) is Deliver; an article piece keeps them. if (!graph || !piece || piece.kind !== "article" || !piece.site || !piece.report) { return projectHref(projectId, { ...q, tab: q.tab ? ((PIECE_FOLDED as Record)[q.tab] ?? q.tab) : q.tab }); } const v = await articleVideo(piece, graph, projectId); return articleHref( { site: piece.site, id: piece.report }, { tab: q.tab, all: q.all, project: v.cut === projectId ? undefined : projectId, note: q.note }, ); } /** * The article piece a claimed project's own address redirects to, with the * same tab (`?tab=`, else the tab its `?note=` is drawn on, else `?all=1`'s * Cut), or null when no article claims it. */ export async function claimedProjectRedirect( project: { id: string; dir: string }, search: Record, ): Promise { const graph = await readPieceGraph().catch(() => null); const piece = graph ? graph.byId.get(graph.pieceOfProject.get(project.id) ?? "") : undefined; if (!piece || piece.kind !== "article") return null; const one = (k: string) => (typeof search[k] === "string" ? (search[k] as string) : undefined); const note = one("note"); const all = one("all") === "1"; const asked = one("tab"); const tab = asked && PIECE_ADDRESS_TAB_IDS.includes(asked) ? asked : ((note ? await projectNoteTab(project.dir, note, { piece: "article" }) : null) ?? (all ? "cut" : "overview")); return pieceHrefOfProject(project.id, { tab, all, note }); } export type ArticlePieceFrame = PieceFrame & { /** Where the piece lives in the graph's terms (the piece's site and report). */ site: string; report: string; }; /** * An article piece's frame: the piece, its lifecycle, the board that seeded * it, and the stepper by the one stage rule (lib/pieces/tabs.mjs pieceSteps). */ export async function readArticlePieceFrame( site: string, report: string, { hasIterations, hasReport }: { hasIterations: boolean; hasReport: boolean }, ): Promise { const [graph, life, cases] = await Promise.all([readPieceGraph(), readLifecycle(), readCases()]); const pieceId = articlePieceId(site, report); const piece = graph.byId.get(pieceId) ?? null; const lifecycle = lifecycleOf(life, pieceId); const board = boardOfArticle(articleHref({ site, id: report }), cases); // The one stage rule (lib/pieces/tabs.mjs pieceSteps), on the graph's facts -- // the same Work's stage facet reads -- with what this page read fresh: its // iterations, its report, the board (the graph is memoised). const reached = { ...(piece?.reached ?? { video: false, deliver: false }), ...(hasIterations || board ? { research: true } : {}), article: hasReport || !!piece?.reached?.article, }; const steps = pieceSteps({ kind: "article", reached, lifecycle }) as PieceFrame["steps"]; return { piece, pieceId, article: null, board, lifecycle, lifecycleError: life.error ?? null, steps, stage: stageOfPiece({ kind: "article", reached }), site, report, }; }