ask-progress.ts
This file
Path
apps/web/src/features/shared/ask-progress.ts
214 lines · 5.5 KiB · TypeScript
Last changed by
Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
History of this fileTake it
Draw every list and subject from one row, one status line, one family fan and one set of fact blocksoscabrielbefore adoption886b726
lines 1–20why, one step back1import type { Ask } from "@repo/protocol/domain/ask";2import { ProjectEvent } from "@repo/protocol/domain/event";3import { Outcome } from "@repo/protocol/domain/outcome";4import type { AskId, CommitSha, Handle, LandedSha } from "@repo/protocol/ids";5import type { DateTime } from "effect";6import { constVoid, identity } from "effect/Function";78export interface Round {9 readonly at: DateTime.Utc;10 readonly head: CommitSha;11 readonly outcome: Outcome;12 readonly seq: number;13}1415export interface AskProgress {16 readonly claimant?: Handle;17 readonly landed?: { readonly sha: LandedSha; readonly seq: number };18 readonly lastPush?: { readonly at: DateTime.Utc; readonly head: CommitSha };19 readonly lastSeq: number;20 /**Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
lines 21–22why, one step back21 * A new claim, a release or a lapse starts the ask over, so an earlier22 * answerer's refusal no longer speaks for it.Draw every list and subject from one row, one status line, one family fan and one set of fact blocksoscabrielbefore adoption886b726
lines 23–24why, one step back23 */24 readonly latest?: Outcome;Merge build/1500-design-draws into mainoscabrielbefore adoption9bc46b7
line 25why, one step back25 readonly opened?: DateTime.Utc;Draw every list and subject from one row, one status line, one family fan and one set of fact blocksoscabrielbefore adoption886b726
lines 26–42why, one step back26 readonly rounds: readonly Round[];27}2829export type Mark =30 | "checking"31 | "claimed"32 | "landed"33 | "open"34 | "refused"35 | "waiting";3637export interface Family {38 readonly children: readonly { readonly ask: AskId; readonly mark: Mark }[];39 readonly landed: number;40 readonly total: number;41}42Finish the Control room: every page passes axe and a keyboard walk in both schemesoscabrielbefore adoption3fd50a5
line 43why, one step back43export type Move = "answerer" | "decider" | "none" | "parent" | "platform";Draw every list and subject from one row, one status line, one family fan and one set of fact blocksoscabrielbefore adoption886b726
lines 44–83why, one step back4445export const lastSeq = (events: readonly ProjectEvent[]): number => {46 let highest = 0;4748 for (const { seq } of events) {49 highest = Math.max(highest, seq);50 }5152 return highest;53};5455const fresh: AskProgress = { lastSeq: 0, rounds: [] };5657const unclaimed = ({ claimant: _held, ...rest }: AskProgress): AskProgress =>58 rest;5960const startedOver = ({61 claimant: _held,62 latest: _said,63 ...rest64}: AskProgress): AskProgress => rest;6566const apply = (states: Map<AskId, AskProgress>, event: ProjectEvent): void => {67 const change = (68 ask: AskId,69 step: (progress: AskProgress) => AskProgress70 ): void => {71 states.set(ask, {72 ...step(states.get(ask) ?? fresh),73 lastSeq: event.seq,74 });75 };7677 ProjectEvent.match(event, {78 AskClosed: ({ ask, landing, seq }) => {79 change(ask, (progress) => ({80 ...unclaimed(progress),81 landed: { seq, sha: landing },82 }));83 },Merge build/1500-design-draws into mainoscabrielbefore adoption9bc46b7
lines 84–85why, one step back84 AskOpened: ({ ask, at }) => {85 change(ask.ask, (progress) => ({ ...progress, opened: at }));Draw every list and subject from one row, one status line, one family fan and one set of fact blocksoscabrielbefore adoption886b726
lines 86–117why, one step back86 },87 Claimed: ({ ask, claimant }) => {88 change(ask, (progress) => ({ ...startedOver(progress), claimant }));89 },90 Decided: constVoid,91 Judged: ({ ask, at, head, outcome, seq }) => {92 change(ask, (progress) => ({93 ...progress,94 latest: outcome,95 rounds: [...progress.rounds, { at, head, outcome, seq }],96 }));97 },98 Landed: constVoid,99 Lapsed: ({ ask }) => {100 change(ask, startedOver);101 },102 LedgerAppended: constVoid,103 ProposalDrafted: constVoid,104 ProposalOpened: constVoid,105 Pushed: ({ ask, at, head }) => {106 change(ask, (progress) => ({ ...progress, lastPush: { at, head } }));107 },108 Released: ({ ask }) => {109 change(ask, startedOver);110 },111 Warned: ({ ask }) => {112 change(ask, identity);113 },114 });115};116117/**Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
lines 118–119why, one step back118 * Applied in their number's order, each number once, so a replay heard twice or119 * out of order folds to the same map.Draw every list and subject from one row, one status line, one family fan and one set of fact blocksoscabrielbefore adoption886b726
lines 120–147why, one step back120 */121export const askStates = (122 events: readonly ProjectEvent[]123): ReadonlyMap<AskId, AskProgress> => {124 const states = new Map<AskId, AskProgress>();125 const seen = new Set<number>();126127 for (const event of events.toSorted((a, b) => a.seq - b.seq)) {128 if (!seen.has(event.seq)) {129 seen.add(event.seq);130 apply(states, event);131 }132 }133134 return states;135};136137export const whoseMove = (latest?: Outcome, claimant?: Handle): Move => {138 if (latest === undefined) {139 return claimant === undefined ? "none" : "answerer";140 }141142 return Outcome.match(latest, {143 AwaitingDecision: (): Move => "decider",144 AwaitingFinish: (): Move => "decider",145 Checking: (): Move => "platform",146 Landed: (): Move => "none",147 Refused: (): Move => "answerer",Finish the Control room: every page passes axe and a keyboard walk in both schemesoscabrielbefore adoption3fd50a5
line 148why, one step back148 WaitingOnParent: (): Move => "parent",Draw every list and subject from one row, one status line, one family fan and one set of fact blocksoscabrielbefore adoption886b726
lines 149–177why, one step back149 });150};151152export const markOf = (progress: AskProgress | undefined): Mark => {153 if (progress === undefined) {154 return "open";155 }156157 if (progress.landed !== undefined) {158 return "landed";159 }160161 const { claimant, latest } = progress;162163 if (latest !== undefined) {164 return Outcome.match(latest, {165 AwaitingDecision: (): Mark => "waiting",166 AwaitingFinish: (): Mark => "waiting",167 Checking: (): Mark => "checking",168 Landed: (): Mark => "landed",169 Refused: (): Mark => "refused",170 WaitingOnParent: (): Mark => "waiting",171 });172 }173174 return claimant === undefined ? "open" : "claimed";175};176177/**Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
lines 178–179why, one step back178 * A listing's `blockedBy` names only the children still open, so it cannot179 * count them.Draw every list and subject from one row, one status line, one family fan and one set of fact blocksoscabrielbefore adoption886b726
lines 180–197why, one step back180 */181export const familyMarks = (182 parent: Ask,183 states: ReadonlyMap<AskId, AskProgress>184): Family => {185 const children = parent.blockedBy.map((ask) => ({186 ask,187 mark: markOf(states.get(ask)),188 }));189190 return {191 children,192 landed: children.filter(({ mark }) => mark === "landed").length,193 total: children.length,194 };195};196197/**Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
lines 198–199why, one step back198 * A replay that already held a landing never stamps it, and a landing stamps199 * once, whether the stream or the poll brought it.Draw every list and subject from one row, one status line, one family fan and one set of fact blocksoscabrielbefore adoption886b726
lines 200–214why, one step back200 */201export const stampsFrom = (202 previous: ReadonlyMap<AskId, AskProgress> | undefined,203 next: ReadonlyMap<AskId, AskProgress>,204 mountSeq: number205): ReadonlySet<AskId> =>206 new Set(207 [...next].flatMap(([ask, { landed }]) =>208 landed !== undefined &&209 landed.seq > mountSeq &&210 previous?.get(ask)?.landed === undefined211 ? [ask]212 : []213 )214 );