import { stat } from "node:fs/promises"; import path from "node:path"; import type { ProjectRef } from "@/lib/project-types"; import { decisionsRead } from "@/lib/projects"; import { listTakes } from "@/lib/report/takes.mjs"; import { readManifest } from "@/lib/projects/report.mjs"; import { homeHref, projectHref, takesHref, versionReviewHref } from "@/lib/hrefs"; import { editorUrl } from "@/lib/articles/generation"; import { QUOTE_CHECK_FILE } from "@/lib/report/quoteCheck.mjs"; import { projectCallsOf } from "./projectCalls"; import { xJarStatus } from "./xJar"; import { readinessPills } from "./readiness.mjs"; // --------------------------------------------------------------------------- // THE READINESS BAR's read (umtool redesign L7b; lib/inbox/readiness.mjs buckets): the rows the Inbox reads for // this one piece -- the reducer's decisions (decisionsForProject, memoised on the project's files), its calls // (projectCallsOf, the same memo) and X's jar (xJarDecisions: a minute's memo, never waited on past 0.4 s) -- // and four stats. Never throws: a piece that cannot be read is a bar of —. SERVER ONLY. // --------------------------------------------------------------------------- export type ReadinessPill = { id: string; label: string; state: "bad" | "count" | "ok" | "none"; count: number; title: string; href: string | null; select?: string | null }; export type Readiness = { project: string; pills: ReadinessPill[] }; const exists = (f: string) => stat(/* turbopackIgnore: true */ f).then(() => true, () => false); export async function readinessOf(ref: ProjectRef): Promise { try { const [read, calls, jar, manifest, quoteCheck, linksChecked, takes] = await Promise.all([ decisionsRead(ref).catch(() => ({ value: [], failed: true })), projectCallsOf(ref), xJarStatus(), readManifest(ref.dir).catch(() => null) as Promise | null>, exists(path.join(ref.dir, QUOTE_CHECK_FILE)), exists(path.join(ref.dir, "out", "links.json")), listTakes(ref.dir).then((r) => r.takes, () => []), ]); const decisions = read.value; const xjar = jar.row ? [jar.row] : []; const timeline = (Array.isArray(manifest?.timeline) ? manifest.timeline : []) as Record[]; const clips = timeline.filter((e) => e?.type === "clip").length; const xPosts = timeline.filter((e) => e?.type === "post" && (e.platform === "x" || /^https?:\/\/(x|twitter)\.com\//.test(String(e.url ?? "")))).length; const rows = [...decisions, ...calls, ...xjar]; // Current only when the checks read and say so (no unbuilt or stale-build row). const built = !read.failed && !rows.some((r) => r.kind === "unbuilt" || r.kind === "stale-build"); // A render on record: a take or version with a preview on disk (the take panel's own test, previewSize). const rendered = (takes as { previewSize: number | null }[]).some((t) => t.previewSize != null); const pills = readinessPills({ rows: rows as never, facts: { quoteCheck, linksChecked, clips, xPosts, built, rendered, decisionsKnown: !read.failed, xKnown: jar.known }, hrefs: { decisions: projectHref(ref.id, { tab: "overview" }), home: homeHref(), deliver: projectHref(ref.id, { tab: "deliver" }), takes: takesHref(ref.id), cut: projectHref(ref.id, { tab: "cut" }), xSettings: `${editorUrl()}/settings`, review: (take: string) => versionReviewHref(ref.id, take), }, }) as ReadinessPill[]; return { project: ref.id, pills }; } catch { return { project: ref.id, pills: [] }; } }