Skip to content
Myr

decisions.ts

This file

Path

apps/web/src/features/inbox/decisions.ts

96 lines · 3.0 KiB · TypeScript

Last changed by

Merge redesign into mainoscabrielbefore adoptioncaedd21

History of this file

Take it

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 1–96why, one step back
1import { Today } from "@repo/protocol/domain/decision";
2import type { Decision } from "@repo/protocol/domain/decision";3import type { DecisionId, ProjectName } from "@repo/protocol/ids";4import { Unauthenticated } from "@repo/protocol/principal";5import { Refusal } from "@repo/protocol/refusal";6import { Array as Arr, Cause, DateTime, Option, Order, Schema } from "effect";78import { decisionKey } from "../decision/subject.ts";910/** What a read of the reader's decisions came back as: the decision page reads it, and the inbox's live decisions. */11export const TodayRead = Schema.TaggedUnion({12 Read: { today: Today },13 Refused: { refusal: Refusal },14 SignedOut: {},15 Unreachable: {},16});1718export type TodayRead = typeof TodayRead.Type;1920const isRefusal = Schema.is(Refusal);2122const isUnauthenticated = Schema.is(Unauthenticated);2324export const isSignedOut = (cause: Cause.Cause<unknown>) =>25 Option.exists(Cause.findErrorOption(cause), isUnauthenticated);2627export const readOfCause = (cause: Cause.Cause<unknown>): TodayRead => {28 const error = Cause.findErrorOption(cause);2930 if (Option.isSome(error) && isRefusal(error.value)) {31 return TodayRead.cases.Refused.make({ refusal: error.value });32 }3334 return isSignedOut(cause)35 ? TodayRead.cases.SignedOut.make({})36 : TodayRead.cases.Unreachable.make({});37};3839export const expiryOrder: Order.Order<Decision> = Order.combineAll([40 Order.mapInput(DateTime.Order, ({ expiresAt }: Decision) => expiresAt),41 Order.mapInput(DateTime.Order, ({ openedAt }: Decision) => openedAt),42 Order.mapInput(Order.String, ({ project }: Decision) => project),43 Order.mapInput(Order.String, ({ id }: Decision) => id),44]);4546/**47 * Ids are minted per project, so a Today across projects may hold two decisions48 * with one id; where an id is named `n` times, the `n` of its decisions that49 * expire first are the ones meant.50 */51export const expiringSoon = (today: Today): ReadonlySet<string> => {52 const named = Arr.groupBy(today.expiringSoon, (id) => id);53 const byExpiry = Arr.sort(today.decisions, expiryOrder);5455 return new Set(56 Object.entries(named).flatMap(([id, times]) =>57 byExpiry58 .filter((decision) => decision.id === id)59 .slice(0, times.length)60 .map(decisionKey)61 )62 );63};6465export const orderDecisions = (today: Today): readonly Decision[] => {66 const soon = expiringSoon(today);6768 return Arr.sort(69 today.decisions,70 Order.combine(71 Order.mapInput(72 Order.Boolean,73 (decision: Decision) => !soon.has(decisionKey(decision))74 ),75 expiryOrder76 )77 );78};7980export const findDecision = (81 today: Today,82 project: ProjectName,83 id: DecisionId84): Option.Option<{ readonly decision: Decision; readonly soon: boolean }> => {85 const soon = expiringSoon(today);8687 return Arr.findFirst(88 today.decisions,89 (decision) => decision.project === project && decision.id === id90 ).pipe(91 Option.map((decision) => ({92 decision,93 soon: soon.has(decisionKey(decision)),94 }))95 );96};