subject.ts
This file
Path
apps/web/src/features/decision/subject.ts
161 lines · 5.7 KiB · TypeScript
Last changed by
Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
History of this fileTake it
Merge build/1224-decider-pagesoscabrielbefore adoptiond22b7a2
lines 1–7why, one step back1import { DecisionSubject } from "@repo/protocol/domain/decision";2import type { Choice, Decision } from "@repo/protocol/domain/decision";3import { DateTime, Match } from "effect";45const yesOrNo: readonly Choice[] = ["accept", "decline"];67/**Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
lines 8–9why, one step back8 * The platform reads `accept` on a stray push as `adopt` too, but the page9 * offers each subject its own words only.Merge build/1224-decider-pagesoscabrielbefore adoptiond22b7a2
lines 10–161why, one step back10 */11export const allowedChoices = DecisionSubject.match({12 Finish: () => yesOrNo,13 OpenProposal: () => yesOrNo,14 PostureChange: () => yesOrNo,15 ProxyWidened: () => yesOrNo,16 RaiseRatchet: () => yesOrNo,17 SecretInTree: () => yesOrNo,18 StrayPush: (): readonly Choice[] => ["adopt", "revert"],19 Vouch: () => yesOrNo,20 Waiver: () => yesOrNo,21});2223const short = (sha: string) => sha.slice(0, 7);2425export const refWords = (ref: string) => ref.replace(/^refs\/heads\//u, "");2627export const describeSubject = DecisionSubject.match({28 Finish: ({ ask, head }) =>29 `Finish the passing answer to ${ask}, at ${short(head)}`,30 OpenProposal: ({ proposal }) => `Open the ask proposed in ${proposal}`,31 PostureChange: ({ from, to }) =>32 `Change the project's posture from ${from} to ${to}`,33 ProxyWidened: ({ hosts, item }) =>34 `Let ${item} be sent to ${hosts.length === 0 ? "more hosts" : hosts.join(", ")} through the platform's proxy`,35 RaiseRatchet: ({ after, ask, before, ratchet, territory }) =>36 `Raise the ${ratchet} ratchet in ${territory} from ${before} to ${after}, for the answer to ${ask}`,37 SecretInTree: ({ ask, item }) =>38 `A stored value of ${item} turned up in the answer to ${ask}, which is held aside`,39 StrayPush: ({ after, before, ref }) =>40 `${refWords(ref)} moved without a landing, from ${short(before)} to ${short(after)}`,41 Vouch: ({ handle }) => `Vouch for ${handle}, so they may claim asks`,42 Waiver: ({ ask, at, marker, reason }) =>43 `Allow ${marker} at ${at.path}:${at.line} in the answer to ${ask}: ${reason}`,44});4546export const subjectKind = DecisionSubject.match({47 Finish: () => "finish",48 OpenProposal: () => "proposal",49 PostureChange: () => "posture change",50 ProxyWidened: () => "proxy",51 RaiseRatchet: () => "ratchet",52 SecretInTree: () => "stored value",53 StrayPush: () => "stray push",54 Vouch: () => "vouch",55 Waiver: () => "waiver",56});5758interface Lines {59 readonly yes: string;60 readonly no: string;61}6263const RECORDED =64 "Your choice is kept on the decision; the platform takes no other step on it.";6566const linesOf = (subject: DecisionSubject): Lines =>67 Match.value(subject).pipe(68 Match.tagsExhaustive({69 Finish: (): Lines => ({70 no: "Nothing lands; the answer keeps waiting for a finish.",71 yes: "Lands the answer as it is, with you as Finished-by.",72 }),73 OpenProposal: (): Lines => ({74 no: "The proposal closes; nothing lands.",75 yes: "Opens the ask: its file and check land on main, with you as Decided-by.",76 }),77 PostureChange: ({ from }): Lines => ({78 no: `The answer that changes it is refused; the posture stays ${from}.`,79 yes: "The answer that changes it goes on through the gate, with you as Decided-by.",80 }),81 ProxyWidened: (): Lines => ({ no: RECORDED, yes: RECORDED }),82 RaiseRatchet: ({ after, before }): Lines => ({83 no: `The answer is refused; the ratchet stays at ${before}.`,84 yes: `The answer goes on through the gate with the ratchet at ${after}, with you as Decided-by.`,85 }),86 SecretInTree: (): Lines => ({ no: RECORDED, yes: RECORDED }),87 StrayPush: ({ after, before, ref }): Lines =>88 refWords(ref) === "main"89 ? {90 no: `A landing puts back the tree at ${short(before)}, with you as Decided-by.`,91 yes: `main stays at ${short(after)}, kept as a landing with you as Decided-by.`,92 }93 : {94 no: `Only main is reverted: ${refWords(ref)} stays where it is.`,95 yes: `${refWords(ref)} stays where it is; only a push to main becomes a landing.`,96 },97 Vouch: ({ handle }): Lines => ({98 no: `Nothing lands; ${handle} stays unvouched.`,99 yes: `Lands ${handle} in the vouch file, with you as Decided-by.`,100 }),101 Waiver: ({ marker }): Lines => ({102 no: "The answer is refused at that line.",103 yes: `The answer goes on through the gate with ${marker} allowed there, with you as Decided-by.`,104 }),105 })106 );107108const saysYes = {109 accept: true,110 adopt: true,111 decline: false,112 revert: false,113} as const satisfies Record<Choice, boolean>;114115export const consequenceOf = (116 subject: DecisionSubject,117 choice: Choice118): string => {119 const lines = linesOf(subject);120121 return saysYes[choice] ? lines.yes : lines.no;122};123124export const choiceWords = {125 accept: "Accept",126 adopt: "Adopt",127 decline: "Decline",128 revert: "Revert",129} as const satisfies Record<Choice, string>;130131export const takenWords = {132 accept: "Accepted",133 adopt: "Adopted",134 decline: "Declined",135 revert: "Reverted",136} as const satisfies Record<Choice, string>;137138export const tookEffect = (decision: Decision, choice: Choice): boolean =>139 saysYes[choice]140 ? consequenceOf(decision.subject, choice) !== RECORDED141 : DecisionSubject.guards.StrayPush(decision.subject) &&142 refWords(decision.subject.ref) === "main";143144export const byWhen = (at: DateTime.Utc) =>145 `${DateTime.formatUtc(at, {146 day: "numeric",147 hour: "2-digit",148 hourCycle: "h23",149 locale: "en-GB",150 minute: "2-digit",151 month: "short",152 weekday: "short",153 })} UTC`;154155export const describeDefault = (decision: Decision) =>156 `If nobody decides by ${byWhen(decision.expiresAt)}: ${decision.default}`;157158export const decisionHref = (decision: Decision) =>159 `/${decision.project}/decisions/${decision.id}`;160161export const decisionKey = ({ id, project }: Decision) => `${project}/${id}`;