/** * Copyright (c) 2025 Elara AI Pty Ltd * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details. */ /** * THE Plan state machine (`Plan Spec.md` §6.1) — every piece of interaction * state in one pure reducer: no React, no DOM, no East values. * {@link planReducer} answers one event, and the store around it carries the * declared-collapse bookkeeping that lets a host data commit RECONCILE the UI * state instead of resetting it (#610). Side effects are returned as data * (`PlanEffect[]`) and run by the canvas controller INSIDE the action that * produced them, right after the transition (#815) — no reducer ever performs * them, and no batch waits for a later drain. * * The slice is the single source of truth for window / resolution / filters — * the machine holds only ephemeral UI state and emits `slice.*` effects for * the component to write through the bind handle. Brush pixel geometry lives * in the shared `slice/brush-strip.tsx` gesture machine; the reducer tracks * only whether a brush drag is in flight (the esc-ladder rung) and receives * the committed window as instants. * * Non-negotiable transition rules (unit-tested as a table in * `plan-state.test.ts`): * * - **Esc ladder**, strict precedence, exactly one rung per press: * brush-cancel → focus-return (links / expand) → deselect. (Drag-cancel * belongs to the shared drag layer, which owns the drag lifecycle — the * machine's own drag staging was deleted as dead code, #569.) * - **Windows are instants** (#631): a committed brush window and the * `slice.setRange` effect carry `PlanInstantValue`s on the axis's arm; * the component writes them as the slice arm the axis speaks. * - **One row focus per canvas** (R1 links / R2 expand): invoking a second * focus control returns the first; invoking the active row's own control * returns it. A links focus's other rows' band opens and folds within it. * - **Grain changes rows, never the axis**: `grain.set` clears `selected`, * keeps `expanded` / the window. * - **One selection** (#1197, the Calendar's B15): a row — at the bucket a * click on its plot named — or events on the canvas. An event's click * selects it and its row, Shift / ⌘ / Ctrl adding or removing it; selecting * a row takes the events away; the esc ladder's deselect rung clears all of * it at once. Events selected from outside the canvas — the overlaps chip's * pair, a peer the inspector's banner names (#1198) — replace the selection * whole, on their row. * * The hover CURSOR (hairline + ruler chip) is NOT machine state: it is * display-only chrome written straight to the DOM by the canvas's cursor * controller (#609) — routing a pointermove through a reducer re-rendered * every mounted row once per event. * * @packageDocumentation */ import { type PlanInstantValue } from "./instant.js"; /** A row's stable key (never an index — the flat row array reorders). */ export type RowKey = string; /** The §5 grains. */ export type PlanGrain = "group" | "resource"; /** All ephemeral UI state — one object, one reducer. */ export interface PlanUiState { /** The active grain (initial from the IR; explicit fold-all actions use resource). */ grain: PlanGrain; /** Collapsed rows — any row with children (#822; keys present = COLLAPSED — * rows carry the initial set). */ collapsed: ReadonlySet; /** The selected row, if any (`--brand-tint`, the one selection colour). */ selected: RowKey | null; /** The bucket a click on the selected row's plot named — its start instant — or `null`: what an inspector reads the row's measures at (#1197). */ selectedAt: PlanInstantValue | null; /** The selected events, by their elements' keys (#1197, the Calendar's B15): a click selects one, Shift, ⌘ or Ctrl adds or removes one. */ elements: readonly string[]; /** Chart rows toggled from spark to expanded. */ chartsExpanded: ReadonlySet; /** Whether a horizon-brush drag is in flight (esc-ladder rung). */ brush: { active: true; } | null; /** The row-scoped focus (R1 links / R2 expand) — at most one per canvas. A links focus's `others` says whether the * band its other rows fold into is open (#1680's review); absent, it is folded. */ focus: { kind: "links" | "expand"; key: RowKey; others?: boolean; } | null; } /** Every interaction the surface can report. */ export type PlanEvent = { t: "grain.set"; grain: PlanGrain; } | { t: "group.toggle"; key: RowKey; } /** One atomic fold of every parent, including nested parents. */ | { t: "groups.set"; keys: readonly RowKey[]; collapsed: boolean; } /** A row selected — `at`, the bucket a click on its plot named (#1197). */ | { t: "row.select"; key: RowKey; at?: PlanInstantValue | undefined; } /** An event's element selected on its row (#1197) — `additive` adds or removes it (Shift, ⌘ or Ctrl). */ | { t: "element.select"; key: string; row: RowKey; additive: boolean; } /** * Events selected from outside the canvas (#1198) — the overlaps chip's * pair, a peer the inspector's banner names — by their elements' keys: the * selection replaced by them, on the row that draws them, or on none when * none does (the viewer hid the series). */ | { t: "elements.select"; keys: readonly string[]; row: RowKey | null; } | { t: "chart.toggle"; key: RowKey; } | { t: "brush.down"; } | { t: "brush.commit"; min: PlanInstantValue; max: PlanInstantValue; } | { t: "brush.clear"; } | { t: "brush.up"; } | { t: "focus.links"; key: RowKey; } | { t: "focus.expand"; key: RowKey; } | { t: "focus.clear"; } /** A links focus's other rows' band opened, or folded again (#1680's review). */ | { t: "focus.others"; } /** A zoom to a resolution — about the middle given, in the axis's numbers: a coarser table cell's period, where a * double click on it zooms to edit it (#1438) — else about the window's own middle. */ | { t: "resolution.set"; resolution: string; about?: number | undefined; } /** A window pan of N whole periods — the narrow layout's two-finger * horizontal drag (§10); the `[` / `]` keys are the one-period case. */ | { t: "pan"; buckets: number; } | { t: "key"; key: "esc" | "n" | "[" | "]" | "g"; }; /** Side effects, returned as data — never performed in the reducer. */ export type PlanEffect = { t: "slice.setRange"; min: PlanInstantValue; max: PlanInstantValue; } | { t: "slice.clearRange"; } | { t: "slice.setResolution"; resolution: string; about?: number | undefined; } | { t: "emit.select"; key: RowKey; } | { t: "emit.groupToggle"; key: RowKey; expanded: boolean; } | { t: "emit.grainChange"; grain: PlanGrain; } | { t: "scroll.toNow"; } | { t: "pan"; buckets: number; }; /** The §5 grains, in the order the `g` key cycles them and the toolbar's * grain segment lists them (#632) — one list, so the two cannot drift. */ export declare const PLAN_GRAINS: readonly PlanGrain[]; /** The initial UI state for a decoded root. */ export declare function initialPlanState(grain: PlanGrain, collapsedKeys: Iterable): PlanUiState; /** * The pure transition function: `(state, event, ctx) → { state, effects }`. * * @param s - The current UI state * @param e - The interaction event * @returns The next state plus the effects the component must run */ export declare function planReducer(s: PlanUiState, e: PlanEvent): { state: PlanUiState; effects: PlanEffect[]; }; /** The UI store: UI state + collapse seeding + the user's toggles. */ export interface PlanStore { /** The ephemeral UI state. */ ui: PlanUiState; /** * Declared-collapse keys already applied once. A declared key seeds * `collapsed` the FIRST time its row appears and never again, so a group * the user has since opened stays open across value changes and paged * window landings alike. A key that vanishes is pruned; if it later * returns it is a NEW row and re-seeds. */ seeded: ReadonlySet; /** * The collapse of every row the USER toggled (`true` = collapsed) — what * a remount restores (#813), and what a bound `ui` state's `collapsed` and * `expanded` lists hold (#824). The declaration only seeds rows the user * never touched; a toggle outranks it from then on, for rows resident or * not. Kept apart from `collapsed` because a restored toggle must outlive * the rows being absent (a paged window not yet landed). */ overrides: ReadonlyMap; /** The declared initial grain, as of the last reconcile — a CHANGED * declaration is adopted (it re-derives the rows, like `grain.set`); * an unchanged one leaves the user's grain alone. */ declaredGrain: PlanGrain; } /** One store transition: the next store, and the effects its action must run. */ export interface PlanStoreStep { /** The next store — the same object when nothing changed. */ store: PlanStore; /** The effects to run, in order, after the transition. */ effects: readonly PlanEffect[]; } /** Everything the store reducer handles. */ export type PlanAction = /** An interaction event — the machine transition plus its effects. */ { t: "event"; e: PlanEvent; } /** * The host's data changed (a new decoded value): drop per-row state whose * rows vanished, seed never-seen declared collapse, keep everything else. */ | { t: "reconcile"; /** Every row key the new value holds. */ alive: ReadonlySet; /** * Whether `alive` is EVERY row the canvas has (#813). An inline * collection's keys are all here; a paged source's resident rows are * not all its rows — a key missing from them may simply not have * landed — so an incomplete set prunes nothing. */ complete: boolean; /** The new value's declared-collapsed row keys. */ declaredCollapsed: ReadonlySet; /** The new value's declared initial grain. */ declaredGrain: PlanGrain; } /** * Rows arrived WITHOUT a data change (a paged window landed): seed * never-seen declared collapse and drop nothing — eviction must not * erase state the user still owns. */ | { t: "seed"; declaredCollapsed: ReadonlySet; } /** * The host wrote the canvas's bound `ui` state (#824): its selection, the * rows it folds or opens against their declaration, and its expanded * charts replace the canvas's own. A row in neither fold list follows its * declaration again. No callback fires — the host made the change, and * echoing it back to the host would loop. */ | { t: "external"; /** The selected row, if any. */ selected: RowKey | null; /** Each overridden row's collapse (`true` = folded, `false` = opened). */ collapse: ReadonlyMap; /** The expanded chart rows. */ charts: ReadonlySet; }; /** What a remount restores (#813) — the user's own toggles, nothing else. */ export interface PlanRestored { /** The collapse of each row the user toggled (`true` = collapsed). */ collapse: Iterable; /** The chart rows the user expanded. */ charts: Iterable; } /** * The initial store for a decoded root (declared collapse counts as seeded). * * @param grain - The declared grain * @param collapsedKeys - The declared-collapsed row keys * @param restored - The user's persisted toggles, when a remount restores them * (#813): a toggled row keeps its toggle, and only the rows the user never * touched take the declaration * @returns The store */ export declare function initialPlanStore(grain: PlanGrain, collapsedKeys: Iterable, restored?: PlanRestored): PlanStore; /** * The store transition — pure, like everything here. The effects come back * with the next store and the caller runs them; nothing waits in the store. * * @param store - The current store * @param a - The action * @returns The next store — `store` itself when nothing changed — and the * effects the action must run */ export declare function planStoreReducer(store: PlanStore, a: PlanAction): PlanStoreStep; //# sourceMappingURL=plan-state.d.ts.map