subject.ts
This file
Path
apps/web/src/features/answer/subject.ts
143 lines · 4.4 KiB · TypeScript
Last changed by
Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
History of this fileTake it
Merge build/1223-ask-and-answeroscabrielbefore adoptiondf02458
lines 1–36why, one step back1import { Client } from "@repo/protocol/contracts";2import type { Verdict } from "@repo/protocol/domain/answer";3import type { Ask } from "@repo/protocol/domain/ask";4import { ProjectEvent } from "@repo/protocol/domain/event";5import type { LandingView } from "@repo/protocol/domain/landing";6import { Outcome } from "@repo/protocol/domain/outcome";7import type { AskId, CommitSha, ProjectName } from "@repo/protocol/ids";8import { Effect, Option, Stream } from "effect";910import { lastSeq } from "../shared/ask-progress.ts";11import type { AskProgress, Round } from "../shared/ask-progress.ts";12import type { StageHolds } from "../shared/outcome.tsx";1314const askOf = (event: ProjectEvent): readonly AskId[] =>15 ProjectEvent.match(event, {16 AskClosed: ({ ask }) => [ask],17 AskOpened: ({ ask }) => [ask.ask],18 Claimed: ({ ask }) => [ask],19 Decided: () => [],20 Judged: ({ ask }) => [ask],21 Landed: () => [],22 Lapsed: ({ ask }) => [ask],23 LedgerAppended: () => [],24 ProposalDrafted: () => [],25 ProposalOpened: () => [],26 Pushed: ({ ask }) => [ask],27 Released: ({ ask }) => [ask],28 Warned: ({ ask }) => [ask],29 });3031export const eventsAbout = (32 events: readonly ProjectEvent[],33 asks: ReadonlySet<AskId>34): readonly ProjectEvent[] =>35 events.filter((event) => askOf(event).some((about) => asks.has(about)));36Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
line 37why, one step back37/** The platform names a split's delete child after its parent. */Merge build/1223-ask-and-answeroscabrielbefore adoptiondf02458
lines 38–114why, one step back38export const deleteChildOf = (parent: Ask): AskId | undefined =>39 parent.blockedBy.find((child) => child === `${parent.id}-delete`);4041export const familyHead = (ask: Ask, parent: Ask | undefined) =>42 parent ?? (deleteChildOf(ask) === undefined ? undefined : ask);4344export const asksAround = (45 ask: Ask,46 parent: Ask | undefined47): ReadonlySet<AskId> =>48 new Set([49 ask.id,50 ...ask.blockedBy,51 ...(parent === undefined ? [] : [parent.id, ...parent.blockedBy]),52 ]);5354export const readEvents = (project: ProjectName) =>55 Effect.gen(function* () {56 const client = yield* Client;5758 const events: readonly ProjectEvent[] = yield* Stream.runCollect(59 client.watch({ project, since: 0 })60 ).pipe(Effect.orElseSucceed((): readonly ProjectEvent[] => []));6162 return events;63 });6465export const readParent = (ask: Ask) =>66 Effect.gen(function* () {67 if (ask.parent === undefined) {68 return Option.none<Ask>();69 }7071 const client = yield* Client;7273 return yield* client74 .showAsk({ ask: ask.parent, project: ask.project })75 .pipe(76 Effect.map(({ ask: parent }) => Option.some(parent)),77 Effect.orElseSucceed(() => Option.none<Ask>())78 );79 });8081export const keptEvents = (82 events: readonly ProjectEvent[],83 ask: Ask,84 parent: Ask | undefined85) => ({86 events: eventsAbout(events, asksAround(ask, parent)),87 lastSeq: lastSeq(events),88});8990const waitedOnPerson = Outcome.isAnyOf(["AwaitingDecision", "AwaitingFinish"]);9192export const holdsOf = (93 verdict: Verdict,94 landing: LandingView | undefined,95 rounds: readonly Round[]96): StageHolds => ({97 decided:98 (landing?.credits.some(({ role }) => role === "Decided-by") ?? false) ||99 rounds.some(100 ({ head, outcome }) => head === verdict.head && waitedOnPerson(outcome)101 ),102 heldBack: verdict.heldBack.length > 0,103});104105export const roundOf = (106 rounds: readonly Round[],107 head: CommitSha108): { readonly number: number; readonly of: number } | undefined => {109 const index = rounds.findLastIndex((round) => round.head === head);110111 return index === -1 ? undefined : { number: index + 1, of: rounds.length };112};113114/**Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
lines 115–117why, one step back115 * A round's own event says what the gate said when it judged the push (often116 * still checking), so the verdict on that head, and the landing that closed the117 * ask, speak for it once they came.Merge build/1223-ask-and-answeroscabrielbefore adoptiondf02458
lines 118–143why, one step back118 */119export const roundsAsTheyStand = (120 progress: AskProgress | undefined,121 verdict: Verdict | undefined122): readonly Round[] => {123 const rounds = progress?.rounds ?? [];124 const landed = progress?.landed;125 const last = rounds.length - 1;126127 const latestAt =128 verdict === undefined129 ? -1130 : rounds.findLastIndex(({ head }) => head === verdict.head);131132 return rounds.map((round, index) => {133 if (index === latestAt && verdict !== undefined) {134 return { ...round, outcome: verdict.outcome };135 }136137 return index === last &&138 landed !== undefined &&139 !Outcome.isAnyOf(["Refused"])(round.outcome)140 ? { ...round, outcome: Outcome.cases.Landed.make({ sha: landed.sha }) }141 : round;142 });143};