// Which research board a project was scaffolded from -- or an article seeded from (R2's "Start a video from // this board"). The hand-off is recorded on the CASE, not the project: the // confirm writes `{ to: "video", href: /projects/, at, what }` into the // case's `handoffs` (lib/research/handoff.ts confirmVideo). So the board of a // project is the case whose video hand-off names it -- the newest, should two. // Pure: the page reads the cases (lib/pieces/frame.ts) and asks this. /** The path of an href, decoded segment by segment, without its query or hash. */ function pathOf(href) { const bare = String(href ?? "").split(/[?#]/)[0]; try { return bare.split("/").map(decodeURIComponent).join("/"); } catch { return bare; } } /** * The newest case whose `to` hand-off's href has the path `want`. * * @param {"video" | "article"} to * @param {string} want a decoded path * @param {{ id: string, title: string, handoffs?: { to: string, href: string, at?: string }[] }[]} cases * @returns {{ id: string, title: string, at: string | null } | null} */ function boardOf(to, want, cases) { let best = null; for (const c of cases) { for (const h of c.handoffs ?? []) { if (h.to !== to || pathOf(h.href) !== want) continue; const at = h.at ?? null; if (!best || (at ?? "") > (best.at ?? "")) best = { id: c.id, title: c.title, at }; } } return best; } /** * @param {string} projectId * @param {{ id: string, title: string, handoffs?: { to: string, href: string, at?: string }[] }[]} cases * @returns {{ id: string, title: string, at: string | null } | null} */ export function boardOfProject(projectId, cases) { return boardOf("video", `/projects/${projectId}`, cases); } /** * The board an article was seeded from (R2's "Draft an article from this * board"): the case whose ARTICLE hand-off names the article's page -- * `{ to: "article", href: /articles/ }` (lib/research/handoff.ts * confirmArticle; the store's address, the only one a hand-off makes). * * @param {string} articlePath the article's page path (`/articles/`, `/sites//`) * @param {{ id: string, title: string, handoffs?: { to: string, href: string, at?: string }[] }[]} cases */ export function boardOfArticle(articlePath, cases) { return boardOf("article", pathOf(articlePath), cases); }