Skip to content
Myr

today.ts

This file

Path

apps/cli/src/tui/today.ts

205 lines · 5.7 KiB · TypeScript

Last changed by

Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f

History of this file

Take it

Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f

lines 1–2why, one step back
1// Nothing here touches the terminal, so the CLI loads OpenTUI only when `--tui` opens it.
2// A choice other than the decision's default waits for `y`: the default is the safe one.

Merge branch 'build/710-today-tui'oscabrielbefore adoption8f57dc3

lines 3–23why, one step back
3import type { Decide } from "@repo/protocol/contracts";
4import { DecisionSubject } from "@repo/protocol/domain/decision";5import type { Choice, Decision, Today } from "@repo/protocol/domain/decision";6import type { DecisionId, ProjectName } from "@repo/protocol/ids";7import { Array as Arr, DateTime, Option, Order } from "effect";89export type DecideCall = Omit<typeof Decide.payloadSchema.Type, "note">;1011export interface TodayViewState {12 readonly today: Today;13 readonly cursor: number;14 readonly confirming: Option.Option<DecideCall>;15 readonly notice: readonly string[];16}1718export interface Step {19 readonly state: TodayViewState;20 readonly call: Option.Option<DecideCall>;21 readonly quit: boolean;22}23

Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f

line 24why, one step back
24/** Ids are minted per project, so the project comes first. */

Merge branch 'build/710-today-tui'oscabrielbefore adoption8f57dc3

lines 25–36why, one step back
25export const placeOf = (project: ProjectName, id: DecisionId) =>
26 `${project}/${id}`;2728const keyOf = ({ id, project }: Decision) => placeOf(project, id);2930const expiryOrder: Order.Order<Decision> = Order.combineAll([31 Order.mapInput(DateTime.Order, ({ expiresAt }: Decision) => expiresAt),32 Order.mapInput(DateTime.Order, ({ openedAt }: Decision) => openedAt),33 Order.mapInput(Order.String, ({ project }: Decision) => project),34 Order.mapInput(Order.String, ({ id }: Decision) => id),35]);36

Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f

line 37why, one step back
37/** Ids are minted per project: where an id is named `n` times, its `n` soonest-expiring decisions are meant. */

Merge branch 'build/710-today-tui'oscabrielbefore adoption8f57dc3

lines 38–194why, one step back
38const expiringSoon = (today: Today): ReadonlySet<string> => {
39 const named = Arr.groupBy(today.expiringSoon, (id) => id);40 const byExpiry = Arr.sort(today.decisions, expiryOrder);4142 return new Set(43 Object.entries(named).flatMap(([id, times]) =>44 byExpiry45 .filter((decision) => decision.id === id)46 .slice(0, times.length)47 .map(keyOf)48 )49 );50};5152export const soonIn = (today: Today) => {53 const soon = expiringSoon(today);5455 return (decision: Decision) => soon.has(keyOf(decision));56};5758export const inOrder = (today: Today): readonly Decision[] => {59 const isSoon = soonIn(today);6061 return Arr.sort(62 today.decisions,63 Order.combine(64 Order.mapInput(Order.Boolean, (decision: Decision) => !isSoon(decision)),65 expiryOrder66 )67 );68};6970export interface ChoiceKey {71 readonly choice: Choice;72 readonly key: string;73}7475const yesOrNo: readonly ChoiceKey[] = [76 { choice: "accept", key: "a" },77 { choice: "decline", key: "d" },78];7980export const choicesFor = (decision: Decision): readonly ChoiceKey[] =>81 DecisionSubject.match(decision.subject, {82 Finish: () => yesOrNo,83 OpenProposal: () => yesOrNo,84 PostureChange: () => yesOrNo,85 ProxyWidened: () => yesOrNo,86 RaiseRatchet: () => yesOrNo,87 SecretInTree: () => yesOrNo,88 StrayPush: (): readonly ChoiceKey[] => [89 { choice: "adopt", key: "a" },90 { choice: "revert", key: "r" },91 ],92 Vouch: () => yesOrNo,93 Waiver: () => yesOrNo,94 });9596export const start = (today: Today): TodayViewState => ({97 confirming: Option.none(),98 cursor: 0,99 notice: [],100 today,101});102103export const current = (state: TodayViewState): Option.Option<Decision> =>104 Arr.get(inOrder(state.today), state.cursor);105106const clamp = (cursor: number, today: Today) =>107 Math.max(0, Math.min(cursor, today.decisions.length - 1));108109const stay = (state: TodayViewState): Step => ({110 call: Option.none(),111 quit: false,112 state,113});114115const send = (state: TodayViewState, call: DecideCall): Step => ({116 call: Option.some(call),117 quit: false,118 state: { ...state, confirming: Option.none() },119});120121const moveBy = (state: TodayViewState, by: number): Step =>122 stay({123 ...state,124 confirming: Option.none(),125 cursor: clamp(state.cursor + by, state.today),126 });127128const choose = (state: TodayViewState, key: string): Step =>129 Option.match(current(state), {130 onNone: () => stay(state),131 onSome: (decision) =>132 Option.match(133 Arr.findFirst(choicesFor(decision), (each) => each.key === key),134 {135 onNone: () => stay(state),136 onSome: ({ choice }) => {137 if (decision.decided !== undefined) {138 return stay(state);139 }140141 const call = {142 choice,143 decision: decision.id,144 project: decision.project,145 };146147 return choice === decision.default148 ? send(state, call)149 : stay({ ...state, confirming: Option.some(call) });150 },151 }152 ),153 });154155const UP = new Set(["up", "k"]);156157const DOWN = new Set(["down", "j"]);158159const QUIT = new Set(["q", "ctrl+c", "escape"]);160161const CONFIRM = new Set(["y", "return", "enter"]);162163const CANCEL = new Set(["n", "escape"]);164165const free = (state: TodayViewState, key: string): Step => {166 if (UP.has(key)) {167 return moveBy(state, -1);168 }169170 if (DOWN.has(key)) {171 return moveBy(state, 1);172 }173174 if (QUIT.has(key)) {175 return { call: Option.none(), quit: true, state };176 }177178 return choose(state, key);179};180181export const reduce = (state: TodayViewState, key: string): Step =>182 Option.match(state.confirming, {183 onNone: () => free(state, key),184 onSome: (waiting) => {185 if (CONFIRM.has(key)) {186 return send(state, waiting);187 }188189 const cancelled = { ...state, confirming: Option.none() };190191 return CANCEL.has(key) ? stay(cancelled) : free(cancelled, key);192 },193 });194

Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f

line 195why, one step back
195/** The decision taken has left the list, so the same cursor lands on the next one. */

Merge branch 'build/710-today-tui'oscabrielbefore adoption8f57dc3

lines 196–205why, one step back
196export const refreshed = (
197 state: TodayViewState,198 today: Today,199 notice: readonly string[]200): TodayViewState => ({201 confirming: Option.none(),202 cursor: clamp(state.cursor, today),203 notice,204 today,205});