areas.ts
This file
Path
apps/web/src/features/ledger/areas.ts
153 lines · 4.5 KiB · TypeScript
Last changed by
Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
History of this fileTake it
Merge build/1222-project-recordsoscabrielbefore adoption2e6b5a5
lines 1–153why, one step back1import { LedgerEntry } from "@repo/protocol/domain/visibility";2import type { Ledger } from "@repo/protocol/domain/visibility";3import type { AreaName, SuiteRoot } from "@repo/protocol/ids";4import type { DateTime } from "effect";5import { Option } from "effect";67export interface Admission {8 readonly at: DateTime.Utc;9 readonly knownBadCaught: number;10 readonly mainRuns: number;11}1213export interface AreaSummary {14 readonly admission: Option.Option<Admission>;15 readonly area: AreaName;16 readonly authors: Option.Option<number>;17 readonly changedAt: DateTime.Utc;18 readonly checks: number;19 readonly root: Option.Option<SuiteRoot>;20}2122interface AreaFact {23 readonly admitted: Option.Option<Admission & { readonly suite: SuiteRoot }>;24 readonly area: AreaName;25 readonly at: DateTime.Utc;26 readonly authors: Option.Option<number>;27 readonly checks: Option.Option<number>;28 readonly root: Option.Option<SuiteRoot>;29}3031const noFact: Omit<AreaFact, "area" | "at"> = {32 admitted: Option.none(),33 authors: Option.none(),34 checks: Option.none(),35 root: Option.none(),36};3738const factsOf = (entry: LedgerEntry): readonly AreaFact[] =>39 LedgerEntry.match(entry, {40 Admitted: ({ area, at, knownBadCaught, mainRuns, suite }) => [41 {42 ...noFact,43 admitted: Option.some({ at, knownBadCaught, mainRuns, suite }),44 area,45 at,46 root: Option.some(suite),47 },48 ],49 CommitmentPublished: ({ area, at, authors, checks, suite }) => [50 {51 ...noFact,52 area,53 at,54 authors: Option.some(authors),55 checks: Option.some(checks),56 root: Option.some(suite),57 },58 ],59 CountChanged: ({ area, at, count }) => [60 { ...noFact, area, at, checks: Option.some(count) },61 ],62 LayerDeclared: () => [],63 PostureChanged: () => [],64 Released: () => [],65 ValueSet: () => [],66 });6768const recorded = (ledger: Ledger) =>69 ledger.entries.toSorted((one, other) => one.seq - other.seq);7071interface Folded {72 readonly admissions: ReadonlyMap<SuiteRoot, Admission>;73 readonly area: AreaName;74 readonly authors: Option.Option<number>;75 readonly changedAt: DateTime.Utc;76 readonly checks: Option.Option<number>;77 readonly root: Option.Option<SuiteRoot>;78}7980const foldFact = (before: Folded | undefined, fact: AreaFact): Folded => ({81 admissions: Option.match(fact.admitted, {82 onNone: () => before?.admissions ?? new Map(),83 onSome: ({ suite, ...admission }) =>84 new Map([...(before?.admissions ?? []), [suite, admission]]),85 }),86 area: fact.area,87 authors: Option.orElse(fact.authors, () => before?.authors ?? Option.none()),88 changedAt: fact.at,89 checks: Option.orElse(fact.checks, () => before?.checks ?? Option.none()),90 root: Option.orElse(fact.root, () => before?.root ?? Option.none()),91});9293export const areaSummaries = (ledger: Ledger): readonly AreaSummary[] => {94 const areas = new Map<AreaName, Folded>();9596 for (const fact of recorded(ledger).flatMap(factsOf)) {97 areas.set(fact.area, foldFact(areas.get(fact.area), fact));98 }99100 return [...areas.values()]101 .toSorted((one, other) => one.area.localeCompare(other.area))102 .map(({ admissions, area, authors, changedAt, checks, root }) => ({103 admission: Option.flatMap(root, (suite) =>104 Option.fromUndefinedOr(admissions.get(suite))105 ),106 area,107 authors,108 changedAt,109 checks: Option.getOrElse(checks, () => 0),110 root,111 }));112};113114export const plural = (count: number, one: string, many: string) =>115 `${count} ${count === 1 ? one : many}`;116117export const areaHeadline = ({ area, authors, checks }: AreaSummary) =>118 `${area}: ${plural(checks, "sealed check", "sealed checks")}${Option.match(119 authors,120 {121 onNone: () => "",122 onSome: (count) => ` by ${plural(count, "person", "people")}`,123 }124 )}`;125126export interface LedgerHead {127 readonly lead: string;128 readonly rest: string;129}130131export const ledgerHead = (ledger: Ledger): LedgerHead => {132 const areas = areaSummaries(ledger);133134 if (areas.length > 0) {135 const [posture = ledger.postureLabel] =136 ledger.posture === "openWithSealedEvidence"137 ? ledger.postureLabel.split(" · ")138 : [ledger.postureLabel];139140 return {141 lead: posture,142 rest: `. ${areas.map(areaHeadline).join("; ")}.`,143 };144 }145146 return {147 lead: ledger.postureLabel,148 rest:149 ledger.layers.length === 0150 ? ". Nothing is held back."151 : `. ${plural(ledger.layers.length, "layer", "layers")} declared; no sealed checks counted yet.`,152 };153};