// WORK'S FACETS (plans/umtool-redesign.md, P3): which pieces a query keeps, and // the stage a piece is at. Pure, over plain rows (lib/work.ts builds them), so // the filtering is unit-tested and the page only renders. // // Work reads its own keys AND the old lists' keys, so an old link lands on the // same set: // // its own q, stage, kind, site, life, by, sort, recurring (R5: `1` keeps // the series and the living reports) // /browse kind (a project kind id), template, state (a project state), // open (blocking | 1), q, sort (name), deleted // /articles kind=article (the redirect adds it), status, notes (open), // audience, deleted, freed // /sites by=site (the redirect adds it), site, status, notes // // A piece's KINDS are what it is: an article piece is `article` plus the kind // of every project linked to it, a project piece is its project's kind. So the // old `/browse?kind=` still finds the article whose video it is. import { pieceSteps, stageOfPiece } from "../pieces/tabs.mjs"; import { SONG_FLOW } from "../pieces/song-tabs.mjs"; /** The four stages a piece walks, in order (canvas "Work", the stage bar). */ export const STAGES = ["research", "article", "video", "deliver"]; export const LIFECYCLES = ["active", "parked", "done"]; /** * A piece's stage bucket: the step its stepper has current, by THE ONE STAGE * RULE (lib/pieces/tabs.mjs pieceSteps over the graph's `reached`), so Work's * stage facet and the piece page's stepper always agree (P4a-2 follow-up). * A row with no `reached` (an older caller) keeps the old reading: an * article's own stage; a project's `deliver` when it looks done (or ships), * else `video`. * * `kind` is the piece's flow (lib/pieces.ts `flow`): `article`, `project`, or * a song's, whose bucket is the one its current step stands in for. * * @param {{ kind: string, stage: string, looksDone: string[], projectState?: string | null, reached?: { research: boolean, article: boolean, video: boolean, deliver: boolean, spec?: boolean, cuts?: boolean, done?: boolean } }} p */ export function stageOf(p) { if (p.reached) return stageOfPiece({ kind: p.kind, reached: p.reached }); if (p.kind === "article") return STAGES.includes(p.stage) ? p.stage : "article"; if (p.looksDone.length || p.projectState === "shipped") return "deliver"; return "video"; } /** 1-based position on the stage bar. */ export const stepOf = (bucket) => STAGES.indexOf(bucket) + 1; /** * A row's stage bar: how many steps it has, which is current, and its word. A * piece on the four stages is its bucket on four bars; a song (P4a-3) is its * own stepper, Spec → Cuts → Done, on three -- the same steps its page draws, * while its bucket (`stageOf`) is the one Work's stage facet files it under. * * @param {{ kind: string, reached?: object }} p `kind` the piece's flow * @param {string} bucket * @returns {{ step: number, steps: number, label: string | null }} `label` null: the caller's own word */ export function stageBar(p, bucket) { if (p.kind === SONG_FLOW && p.reached) { const steps = pieceSteps({ kind: p.kind, reached: p.reached, lifecycle: "active" }); const at = steps.findIndex((s) => s.state === "current"); return { step: at + 1, steps: steps.length, label: steps[at]?.label.toLowerCase() ?? null }; } return { step: stepOf(bucket), steps: STAGES.length, label: null }; } /** `life`: one of the three, `all`, or (absent) active. */ export function lifeFilter(life) { if (life === "all") return null; return LIFECYCLES.includes(life) ? life : "active"; } /** * Does a row stay under a query? * * @param {{ * id: string, title: string, kinds: string[], bucket: string, site: string | null, * lifecycle: string, templates: string[], states: string[], status: string | null, * audience: string | null, haystack: string, * counts: { blocking: number, open: number, notes: number }, * recurring?: boolean, * }} r * @param {Record} q * @param {{ ignore?: string[] }} [opts] keys to leave out (a facet's own counts) */ export function keepRow(r, q, { ignore = [] } = {}) { const on = (k) => !ignore.includes(k) && q[k]; if (on("stage") && r.bucket !== q.stage) return false; if (on("kind") && !r.kinds.includes(q.kind)) return false; if (on("site") && r.site !== q.site) return false; if (!ignore.includes("life")) { const life = lifeFilter(q.life); if (life && r.lifecycle !== life) return false; } if (on("template") && !r.templates.includes(q.template)) return false; if (on("state") && !r.states.includes(q.state)) return false; if (on("status") && r.status !== q.status) return false; if (on("audience") && r.audience !== q.audience) return false; if (on("recurring") && q.recurring === "1" && !r.recurring) return false; if (on("notes") && q.notes === "open" && r.counts.notes <= 0) return false; if (on("open")) { if (q.open === "blocking" ? r.counts.blocking <= 0 : r.counts.blocking + r.counts.open <= 0) return false; } if (on("q")) { const needle = q.q.trim().toLowerCase(); if (needle && !`${r.title}\n${r.id}\n${r.haystack}`.toLowerCase().includes(needle)) return false; } return true; } /** Newest first, or by title (`sort=name`), id breaking ties. */ export function sortRows(rows, sort) { const out = [...rows]; if (sort === "name") out.sort((a, b) => a.title.localeCompare(b.title) || a.id.localeCompare(b.id)); else out.sort((a, b) => b.newestMs - a.newestMs || a.id.localeCompare(b.id)); return out; }