import { readInbox, type Inbox } from "./inbox"; import { readLifecycle } from "./lifecycle"; import { KINDS, listProjects } from "./projects"; import { listPieces, type PieceWithLifecycle } from "./pieces"; import { stageBar, stageOf } from "./work/facets.mjs"; // --------------------------------------------------------------------------- // WORK, AS DATA: every piece (lib/pieces.ts) as one row for the Work list -- // its stage on the four-step bar, what kind of thing it is, its lifecycle and // how many calls wait on it -- plus the facts the facets filter on // (lib/work/facets.mjs). GET /api/pieces gives the same counts. // // SERVER ONLY. // --------------------------------------------------------------------------- export type PieceCounts = { /** Every decision on the piece, hidden ones included. */ total: number; /** On a parked or done piece. */ hidden: number; /** Visible decisions per inbox group. */ byGroup: Record; /** Visible calls (the five call groups) -- Work's "Open" column. */ calls: number; /** By severity over every decision, hidden or not (the old /browse `open` filter). */ blocking: number; open: number; /** Open notes (the old /articles and /sites `notes=open`). */ notes: number; }; export type WorkRow = PieceWithLifecycle & { /** research | article | video | deliver. */ bucket: string; /** The current step on the row's stage bar, 1-based, of `steps` (four, or a song's three). */ step: number; steps: number; /** What the stage bar says: an article's stage, a project's own state. */ stageLabel: string; /** `article` and/or project kind ids. */ kinds: string[]; kindLabel: string; templates: string[]; states: string[]; projectState: string | null; haystack: string; counts: PieceCounts; /** A recurring report (R5): a series, or a living report's article. */ recurring: boolean; /** Its cadence chip ("weekly · next Mon 06:00"), when recurring. */ chip: string | null; }; const CALL_GROUPS = new Set(["your-call", "review", "blocking", "notes", "open"]); /** Decision counts per piece, from one inbox read. */ export function countsByPiece(inbox: Inbox): Map { const out = new Map(); for (const d of inbox.items) { if (!d.piece) continue; let c = out.get(d.piece); if (!c) out.set(d.piece, (c = { total: 0, hidden: 0, byGroup: {}, calls: 0, blocking: 0, open: 0, notes: 0 })); c.total += 1; if (d.severity === "blocking") c.blocking += 1; else if (d.severity === "open") c.open += 1; if (d.kind === "open-note") c.notes += 1; if (d.hidden) c.hidden += 1; else if (d.group) { c.byGroup[d.group] = (c.byGroup[d.group] ?? 0) + 1; if (CALL_GROUPS.has(d.group)) c.calls += 1; } } return out; } const EMPTY: PieceCounts = { total: 0, hidden: 0, byGroup: {}, calls: 0, blocking: 0, open: 0, notes: 0 }; export async function readWork(): Promise<{ rows: WorkRow[]; lifecycleError?: string }> { const life = await readLifecycle(); const [pieces, inbox, projects] = await Promise.all([listPieces(life), readInbox(), listProjects()]); const counts = countsByPiece(inbox); const byId = new Map(projects.map((p) => [p.id, p])); const label = new Map(KINDS.map((k) => [k.id, k.label])); // One row per series: an edition's article is under its series (R5). const rows = pieces.filter((p) => !p.series).map((p): WorkRow => { const own = p.project ? byId.get(p.project) : undefined; const linked = p.projects.map((x) => byId.get(x.id)).filter((x) => x !== undefined); const mine = own ? [own] : linked; const projectKinds = [...new Set(mine.map((x) => x.kind))]; // A watch (R6) is stored like a series but is its own kind: no editions, // an article only when its triage escalates. const recurringKind = p.schedule?.shape === "watch" ? "watch" : "series"; const kinds = p.kind === "article" ? ["article", ...projectKinds] : p.kind === "series" ? [recurringKind] : projectKinds; const kindLabel = p.kind === "series" ? recurringKind : p.kind === "article" ? projectKinds.length ? `article + ${projectKinds.map((k) => label.get(k) ?? k).join(", ")}` : "article" : (projectKinds.map((k) => label.get(k) ?? k).join(", ") || "project"); const projectState = own?.state ?? null; // The piece's flow, so a song's bucket is its own stepper's (Spec → Cuts → Done). const bucket = stageOf({ kind: p.flow, stage: p.stage, looksDone: p.looksDone, projectState, reached: p.reached }); const bar = stageBar({ kind: p.flow, reached: p.reached }, bucket); return { ...p, bucket, step: bar.step, steps: bar.steps, stageLabel: bar.label ?? (p.kind === "article" ? bucket : (projectState ?? p.stage)), kinds, kindLabel, templates: [...new Set(mine.map((x) => x.template))], states: [...new Set(mine.map((x) => x.state))], projectState, haystack: [p.site, p.report, ...mine.map((x) => x.haystack)].filter(Boolean).join("\n"), counts: counts.get(p.id) ?? EMPTY, recurring: !!p.schedule, chip: p.schedule?.chip ?? null, }; }); return { rows, ...(life.error ? { lifecycleError: life.error } : {}) }; }