Skip to content
Myr

ask-progress.ts

This file

Path

apps/web/src/features/shared/ask-progress.ts

213 lines · 5.5 KiB · TypeScript

Last changed by

Merge project/drop-proposals into mainoscabrielbefore adoptionb4ed08b

History of this file

Take 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 back
1import 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 back
21 * A new claim, a release or a lapse starts the ask over, so an earlier
22 * 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 back
23 */
24 readonly latest?: Outcome;

Merge build/1500-design-draws into mainoscabrielbefore adoption9bc46b7

line 25why, one step back
25 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 back
26 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}42

Finish the Control room: every page passes axe and a keyboard walk in both schemesoscabrielbefore adoption3fd50a5

line 43why, one step back
43export 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 back
44
45export 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 back
84 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–101why, one step back
86 },
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 },

Merge project/discussions into mainoscabrielbefore adoption29b0353

line 102why, one step back
102 DiscussionPosted: constVoid,

Draw every list and subject from one row, one status line, one family fan and one set of fact blocksoscabrielbefore adoption886b726

lines 103–116why, one step back
103 LedgerAppended: constVoid,
104 Pushed: ({ ask, at, head }) => {105 change(ask, (progress) => ({ ...progress, lastPush: { at, head } }));106 },107 Released: ({ ask }) => {108 change(ask, startedOver);109 },110 Warned: ({ ask }) => {111 change(ask, identity);112 },113 });114};115116/**

Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72

lines 117–118why, one step back
117 * Applied in their number's order, each number once, so a replay heard twice or
118 * 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 119–146why, one step back
119 */
120export const askStates = (121 events: readonly ProjectEvent[]122): ReadonlyMap<AskId, AskProgress> => {123 const states = new Map<AskId, AskProgress>();124 const seen = new Set<number>();125126 for (const event of events.toSorted((a, b) => a.seq - b.seq)) {127 if (!seen.has(event.seq)) {128 seen.add(event.seq);129 apply(states, event);130 }131 }132133 return states;134};135136export const whoseMove = (latest?: Outcome, claimant?: Handle): Move => {137 if (latest === undefined) {138 return claimant === undefined ? "none" : "answerer";139 }140141 return Outcome.match(latest, {142 AwaitingDecision: (): Move => "decider",143 AwaitingFinish: (): Move => "decider",144 Checking: (): Move => "platform",145 Landed: (): Move => "none",146 Refused: (): Move => "answerer",

Finish the Control room: every page passes axe and a keyboard walk in both schemesoscabrielbefore adoption3fd50a5

line 147why, one step back
147 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 148–176why, one step back
148 });
149};150151export const markOf = (progress: AskProgress | undefined): Mark => {152 if (progress === undefined) {153 return "open";154 }155156 if (progress.landed !== undefined) {157 return "landed";158 }159160 const { claimant, latest } = progress;161162 if (latest !== undefined) {163 return Outcome.match(latest, {164 AwaitingDecision: (): Mark => "waiting",165 AwaitingFinish: (): Mark => "waiting",166 Checking: (): Mark => "checking",167 Landed: (): Mark => "landed",168 Refused: (): Mark => "refused",169 WaitingOnParent: (): Mark => "waiting",170 });171 }172173 return claimant === undefined ? "open" : "claimed";174};175176/**

Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72

lines 177–178why, one step back
177 * A listing's `blockedBy` names only the children still open, so it cannot
178 * count them.

Draw every list and subject from one row, one status line, one family fan and one set of fact blocksoscabrielbefore adoption886b726

lines 179–196why, one step back
179 */
180export const familyMarks = (181 parent: Ask,182 states: ReadonlyMap<AskId, AskProgress>183): Family => {184 const children = parent.blockedBy.map((ask) => ({185 ask,186 mark: markOf(states.get(ask)),187 }));188189 return {190 children,191 landed: children.filter(({ mark }) => mark === "landed").length,192 total: children.length,193 };194};195196/**

Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72

lines 197–198why, one step back
197 * A replay that already held a landing never stamps it, and a landing stamps
198 * 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 199–213why, one step back
199 */
200export const stampsFrom = (201 previous: ReadonlyMap<AskId, AskProgress> | undefined,202 next: ReadonlyMap<AskId, AskProgress>,203 mountSeq: number204): ReadonlySet<AskId> =>205 new Set(206 [...next].flatMap(([ask, { landed }]) =>207 landed !== undefined &&208 landed.seq > mountSeq &&209 previous?.get(ask)?.landed === undefined210 ? [ask]211 : []212 )213 );