import { readInbox, type GroupSummary } from "../inbox"; import { summarizeGroups } from "../inbox/groups.mjs"; import { whatOf } from "../inbox/what.mjs"; import { lifecycleOf, readLifecycle, type LifecycleState } from "../lifecycle"; import { projectPieceId, readPieceGraph, type Piece } from "../pieces"; import { pieceSteps, stepOfPiece } from "./tabs.mjs"; import { kindPieceFlow } from "../projects/kinds.mjs"; import type { Reached } from "../pieces"; import { boardOfProject } from "./board.mjs"; import { readCases } from "./cases"; import type { Decision } from "../decisions"; // --------------------------------------------------------------------------- // A PIECE PAGE's frame, as data (plans/umtool-redesign.md, P4a-1): which piece // a project page is, where it is on the four stages, its lifecycle, and its // Next call -- the top of the Inbox, narrowed to this piece. // // A project is the page; the PIECE is what the Inbox and Work list. A project // no article claims is its own piece (`p:`); one an article claims (its // iteration's projectDir, its manifest's `article`, a unique slug) is part of // that article's piece, so the stage, the lifecycle and the Next call are the // article's. P4a-2 renders that project AS the article's piece; until then the // project page says whose it is and links it. // // SERVER ONLY. The graph is lib/pieces' (memoised 15 s); the lifecycle is read // fresh, as everywhere. // --------------------------------------------------------------------------- export type StepState = "done" | "current" | "todo" | "na"; export type PieceFrame = { /** The piece this page is part of, or null when the graph does not list it (a project the walk refused). */ piece: Piece | null; pieceId: string; /** The piece is an article's, and this project is one of its videos. */ article: { title: string; site: string; report: string; href: string; via: string; cut: boolean } | null; lifecycle: LifecycleState; lifecycleError: string | null; steps: { label: string; state: StepState; title?: string }[]; /** The research board (R2 case) this project was scaffolded from, when a case's video hand-off names it. */ board: { id: string; title: string; at: string | null } | null; /** The step the stepper has current (research … deliver; a song's spec, cuts or done). */ stage: string; }; export { readCases } from "./cases"; /** The research board a project was scaffolded from: the case whose video hand-off names it (lib/pieces/board.mjs). */ async function boardOf(projectId: string): Promise { return boardOfProject(projectId, await readCases()); } /** * @param project the project the page is * @param opts.reached facts the page read fresh (a song page reads the song * anyway), over the graph's memoised ones -- a promote shows at once */ export async function readPieceFrame( project: { id: string; kind?: string; state?: string | null }, opts: { reached?: Partial } = {}, ): Promise { const [graph, life, board] = await Promise.all([readPieceGraph(), readLifecycle(), boardOf(project.id)]); const pieceId = graph.pieceOfProject.get(project.id) ?? projectPieceId(project.id); const piece = graph.byId.get(pieceId) ?? null; const lifecycle = lifecycleOf(life, pieceId); const own = piece?.projects.find((p) => p.id === project.id) ?? null; const article = piece && piece.kind === "article" && piece.site && piece.report ? { title: piece.title, site: piece.site, report: piece.report, href: piece.href, via: own?.via ?? "slug", cut: own?.cut ?? false } : null; // The one stage rule (lib/pieces/tabs.mjs pieceSteps): the graph's facts, so // the stepper here and Work's stage facet read the same answer. // The stepper is the piece's flow: an article's, a report video's, or the one // its project's kind declares (a song's), also for a project the graph has // not listed yet (one scaffolded within the memo's 15 s). const kind = piece?.flow ?? (project.kind ? kindPieceFlow(project.kind) : null) ?? "project"; // The board is read fresh (the graph is memoised): a board's hand-off shows at once. const reached = { ...(piece?.reached ?? { video: true }), ...opts.reached, ...(board ? { research: true } : {}) }; const steps = pieceSteps({ kind, reached, lifecycle }) as PieceFrame["steps"]; const stage = stepOfPiece({ kind, reached }); return { piece, pieceId, article, board, lifecycle, lifecycleError: life.error ?? null, steps, stage }; } export type NextCall = { kind: string; /** What it is, in a few words ("claims never ruled", "Narration draft · 5 fields"). */ what: string; why: string; count: number; severity: string; group: string; action: { label: string; href: string }; /** The action is the lifecycle's (Mark done / Reactivate), a POST rather than a link. */ lifecycleTo: LifecycleState | null; /** More calls on this piece after this one. */ more: number; }; /** * The piece's Next call: its top Inbox card (lib/inbox, ranked and grouped the * Inbox's way), or -- for a parked or done piece, whose calls are hidden -- * the way back. Null when nothing is waiting. */ export async function readNextCall(pieceId: string, lifecycle: LifecycleState): Promise { const inbox = await readInbox(); const mine = inbox.items.filter((d: Decision) => d.piece === pieceId); if (lifecycle !== "active") { const waiting = mine.filter((d) => d.hiddenBy === lifecycle).length; return { kind: "lifecycle", what: lifecycle === "parked" ? "Parked" : "Marked done", why: waiting ? `${waiting} call${waiting === 1 ? "" : "s"} hidden while it is ${lifecycle}` : "nothing hidden", count: waiting, severity: "info", group: "hidden", action: { label: "Reactivate", href: "" }, lifecycleTo: "active", more: 0, }; } const groups = summarizeGroups(mine.filter((d) => !d.hidden)) as GroupSummary[]; // Quiet is facts, not calls: a Next card never offers one. const cards = groups.filter((g) => g.id !== "quiet").flatMap((g) => g.cards.map((c) => ({ g, c }))); const top = cards[0]; if (!top) return null; const { g, c } = top; const what = c.what ?? whatOf(c.kind); return { kind: c.kind, what, why: c.what && c.why.startsWith(c.what) ? c.why.slice(c.what.length).replace(/^ · /, "") : c.why, count: c.count, severity: c.severity, group: g.id, action: c.action, lifecycleTo: c.kind === "looks-done" ? "done" : null, more: cards.length - 1, }; }