read.ts
This file
Path
apps/web/src/features/decision/read.ts
231 lines · 7.3 KiB · TypeScript
Last changed by
Merge redesign into mainoscabrielbefore adoptioncaedd21
History of this fileTake it
Merge build/1224-decider-pagesoscabrielbefore adoptiond22b7a2
line 1why, one step back1/**Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
lines 2–3why, one step back2 * Kept apart from the page's view: a route's loader runs before the view loads,3 * so the route reaches this without carrying the page.Merge build/1224-decider-pagesoscabrielbefore adoptiond22b7a2
lines 4–18why, one step back4 */5import { Client } from "@repo/protocol/contracts";6import { Verdict } from "@repo/protocol/domain/answer";7import { Ask } from "@repo/protocol/domain/ask";8import { Decision, DecisionSubject } from "@repo/protocol/domain/decision";9import type { ProjectEvent } from "@repo/protocol/domain/event";10import { Outcome } from "@repo/protocol/domain/outcome";11import { Proposal } from "@repo/protocol/domain/proposal";12import { CommitSha, DecisionId, ProjectName } from "@repo/protocol/ids";13import type { AskId } from "@repo/protocol/ids";14import { Posture } from "@repo/protocol/posture";15import { Refusal } from "@repo/protocol/refusal";16import { notFound } from "@tanstack/react-router";17import { DateTime, Effect, Exit, Option, Schema, Stream } from "effect";18Merge redesign into mainoscabrielbefore adoptioncaedd21
line 19why, one step back19import { findDecision, readOfCause, TodayRead } from "../inbox/decisions.ts";Merge build/1224-decider-pagesoscabrielbefore adoptiond22b7a2
lines 20–231why, one step back20import { askStates } from "../shared/ask-progress.ts";2122export const Placed = Schema.Struct({23 line: Schema.Int,24 path: Schema.String,25 refusal: Refusal,26 round: Schema.Int,27});2829export const DecisionRead = Schema.TaggedUnion({30 Found: {31 ask: Schema.optionalKey(Ask),32 decision: Decision,33 newerHead: Schema.optionalKey(CommitSha),34 placed: Schema.Array(Placed),35 posture: Schema.optionalKey(36 Schema.Struct({ label: Schema.String, posture: Posture })37 ),38 proposal: Schema.optionalKey(Proposal),39 soon: Schema.Boolean,40 verdict: Schema.optionalKey(Verdict),41 },42 NotOnToday: {},43 Refused: { refusal: Refusal },44 SignedOut: {},45 Unreachable: {},46});4748export type DecisionRead = typeof DecisionRead.Type;4950export type FoundDecision = Extract<DecisionRead, { readonly _tag: "Found" }>;5152export const DecisionPageData = Schema.Struct({53 decision: DecisionId,54 now: Schema.DateTimeUtcFromString,55 project: ProjectName,56 read: DecisionRead,57});5859export type DecisionPageData = typeof DecisionPageData.Type;6061const refusalsOf = (outcome: Outcome): readonly Refusal[] =>62 Outcome.guards.Refused(outcome) ? outcome.refusals : [];6364const placedIn = (events: readonly ProjectEvent[], ask: AskId) =>65 (askStates(events).get(ask)?.rounds ?? []).flatMap(({ outcome }, index) =>66 refusalsOf(outcome).flatMap((refusal) =>67 refusal.at === undefined68 ? []69 : [70 {71 line: refusal.at.line,72 path: refusal.at.path,73 refusal,74 round: index + 1,75 },76 ]77 )78 );7980export const newerThan = (81 events: readonly ProjectEvent[],82 ask: AskId,83 head: CommitSha84): CommitSha | undefined => {85 const last = askStates(events).get(ask)?.lastPush?.head;8687 return last === undefined || last === head ? undefined : last;88};8990const askOf = DecisionSubject.match({91 Finish: ({ ask }) => Option.some(ask),92 OpenProposal: () => Option.none(),93 PostureChange: () => Option.none(),94 ProxyWidened: () => Option.none(),95 RaiseRatchet: ({ ask }) => Option.some(ask),96 SecretInTree: ({ ask }) => Option.some(ask),97 StrayPush: () => Option.none(),98 Vouch: () => Option.none(),99 Waiver: ({ ask }) => Option.some(ask),100});101102const concerning = (decision: Decision, soon: boolean) =>103 Effect.gen(function* () {104 const client = yield* Client;105 const { project, subject } = decision;106107 const ask = yield* Option.match(askOf(subject), {108 onNone: () => Effect.succeedNone,109 onSome: (asked) =>110 client.showAsk({ ask: asked, project }).pipe(111 Effect.map(({ ask: shown }) => Option.some(shown)),112 Effect.orElseSucceed(() => Option.none())113 ),114 });115116 const proposal = DecisionSubject.guards.OpenProposal(subject)117 ? yield* client118 .showProposal({ project, proposal: subject.proposal })119 .pipe(120 Effect.map(({ proposal: shown }) => Option.some(shown)),121 Effect.orElseSucceed(() => Option.none())122 )123 : Option.none();124125 const posture = DecisionSubject.guards.PostureChange(subject)126 ? yield* client.ledger({ project }).pipe(127 Effect.map((ledger) =>128 Option.some({ label: ledger.postureLabel, posture: ledger.posture })129 ),130 Effect.orElseSucceed(() => Option.none())131 )132 : Option.none();133134 const finish = DecisionSubject.guards.Finish(subject)135 ? Option.some(subject)136 : Option.none();137138 const verdict = yield* Option.match(finish, {139 onNone: () => Effect.succeedNone,140 onSome: ({ ask: asked, head }) =>141 client.verdict({ ask: asked, head, project }).pipe(142 Effect.map(({ verdict: judged }) => Option.fromUndefinedOr(judged)),143 Effect.orElseSucceed(() => Option.none())144 ),145 });146147 const events = Option.isSome(finish)148 ? yield* Stream.runCollect(client.watch({ project, since: 0 })).pipe(149 Effect.map((all): readonly ProjectEvent[] => all),150 Effect.orElseSucceed((): readonly ProjectEvent[] => [])151 )152 : [];153154 return DecisionRead.cases.Found.make({155 decision,156 placed: Option.match(finish, {157 onNone: () => [],158 onSome: ({ ask: asked }) => placedIn(events, asked),159 }),160 soon,161 ...Option.match(ask, {162 onNone: () => ({}),163 onSome: (shown) => ({ ask: shown }),164 }),165 ...Option.match(proposal, {166 onNone: () => ({}),167 onSome: (shown) => ({ proposal: shown }),168 }),169 ...Option.match(posture, {170 onNone: () => ({}),171 onSome: (shown) => ({ posture: shown }),172 }),173 ...Option.match(verdict, {174 onNone: () => ({}),175 onSome: (shown) => ({ verdict: shown }),176 }),177 ...Option.match(finish, {178 onNone: () => ({}),179 onSome: ({ ask: asked, head }) => {180 const newer = newerThan(events, asked, head);181182 return newer === undefined ? {} : { newerHead: newer };183 },184 }),185 });186 });187188export const readDecision = (params: {189 readonly project: string;190 readonly decision: string;191}) =>192 Effect.gen(function* () {193 const project = yield* Schema.decodeEffect(ProjectName)(194 params.project195 ).pipe(Effect.mapError(() => notFound()));196197 const id = yield* Schema.decodeEffect(DecisionId)(params.decision).pipe(198 Effect.mapError(() => notFound())199 );200201 const client = yield* Client;202 const now = yield* DateTime.now;203 const today = yield* Effect.exit(client.today({ project }));204205 const read = yield* Exit.match(today, {206 onFailure: (cause) =>207 Effect.succeed(208 TodayRead.match(readOfCause(cause), {209 Read: (): DecisionRead => DecisionRead.cases.Unreachable.make({}),210 Refused: ({ refusal }): DecisionRead =>211 DecisionRead.cases.Refused.make({ refusal }),212 SignedOut: (): DecisionRead =>213 DecisionRead.cases.SignedOut.make({}),214 Unreachable: (): DecisionRead =>215 DecisionRead.cases.Unreachable.make({}),216 })217 ),218 onSuccess: (listed) =>219 Option.match(findDecision(listed, project, id), {220 onNone: () => Effect.succeed(DecisionRead.cases.NotOnToday.make({})),221 onSome: ({ decision, soon }) => concerning(decision, soon),222 }),223 });224225 return yield* Schema.encodeEffect(DecisionPageData)({226 decision: id,227 now,228 project,229 read,230 });231 });