/** * Copyright (c) 2025 Elara AI Pty Ltd * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details. */ /** * Shared record editing for Plan, Calendar and the builders over the app's own * records: one history, checked saves and windowed reads. Each event kind is a * session of the history, and so is each record a builder writes beside its * kinds a row at a time — the Roster's weeks and targets (#1369) — so a gesture * across them is one step, and Save sends each record's changes as a request * of its own. A kind whose events a reader reads by its cells (#1424) keeps * what each Save lands of them, as the Save left them, for that reader to draw * over cells read before the commit until it has read them again (`landed`, * `shown`): a Save's confirmation reads its events by their keys, never the * cells. */ import { type RefObject } from "react"; import { DictType, StringType, type ValueTypeOf, type option } from "@elaraai/east"; import type { ScheduleKindType, ScheduleItemType, ScheduleDraftsType, ScheduleGestureType, ScheduleReadType } from "@elaraai/e3-ui/internal"; import { type EditHistory, type EditHistoryJoined, type EditSession, type Origin } from "@elaraai/east-ui-components"; import type { FieldSpecValue, RecordSliceBindType } from "@elaraai/east-ui/internal"; /** The common event projection decoded from Schedule. */ type Item = ValueTypeOf; /** An event's projected facts and its full record row. */ export type ScheduleRead = Omit, "item"> & { readonly item: I; }; /** * The common kind, with a renderer's richer projection when it has one: one event by its key's text, the drafts in * place, its row as a reader of the kind's cells holds it given (#1039) — `none` for a kind given no cells, which reads * the window; `some(none)` for an event they do not hold, read by its key ({@link heldRowOf}). */ export type ScheduleEditingKind = Omit, "event"> & { readonly event: (id: string, drafts: ScheduleKindDraftsValue, from: Date, to: Date, held: option>) => option>; }; /** * The rows a builder's reads hold of one kind, by the event's key's text: each its row's bytes — read one at a time by a * gesture or an inspector, or every one in turn by a check over them all. */ export interface ScheduleKindRows extends Iterable { /** * Whether they hold an event. * * @param id - The event's key's text */ has(id: string): boolean; /** * An event's row. * * @param id - The event's key's text * @returns Its row's bytes; `undefined` for an event they do not hold */ get(id: string): { readonly row: Uint8Array; } | undefined; } /** * The rows each kind a reader gives cells holds (#1039, #1599) — a kind read by its series index's cells, or its * window's own rows — by the kind's slot, then by the event's key's text: where a gesture, the inspector and a banner * find one of its events, with no read of its own. */ export type ScheduleHeldRows = ReadonlyMap; /** * What the cells' reader holds of one event of a kind (#1039), as the kind's `event` takes it: `none` for a kind it gives * no cells, which reads the window; `some(none)` for an event they do not hold, read by its key; else the event's row. * * @param held - The rows each kind given cells holds, by its slot * @param kind - The event's kind's slot * @param id - The event's key's text * @returns What its `event` is given */ export declare function heldRowOf(held: ScheduleHeldRows | undefined, kind: string, id: string): option>; /** * Where an issue of a kind's session is, as a builder names it — its event's * title, read with the drafts in place; for an event the drafts removed, its * title as its record held it when last read ({@link ScheduleEditing.heldEvent}): * a conflict's card names the event a person removed, never its key — or its * key's text while the event is read, or where its read fails; empty for the * source as a whole. * * @param editing - The shared editing * @param kind - The kind's slot * @param entry - The issue's entry: its event's key's text * @returns The event's name */ export declare function scheduleIssueName(editing: Pick, kind: string, entry: string): string; /** One event entry's identity in the shared session. */ export interface ScheduleEntryRef { readonly id: string; } /** The time range the renderer reads. */ export interface ScheduleRange { readonly from: Date; readonly to: Date; } /** Every kind's drafts, by kind then entry id. */ export type ScheduleEventDraftsValue = ValueTypeOf>; /** One kind's drafts. */ export type ScheduleKindDraftsValue = ValueTypeOf; /** A write through a kind's field names. */ export type ScheduleGestureValue = ValueTypeOf; /** * One event a gesture changes, and how: through its kind's `write` (a move, * a resize, a drop, a schedule or an unschedule, a field edited), its new row * whole (an author's `update`, a duplicate — `created`, an event under a new * key), or deleted. A row of a record beside the kinds is given whole, * rewritten from the row as the gesture finds it (#1388: the Schematic's * gestures, each through its kind's own `write`; Publish's status, over the * period's row as its Save left it), or deleted. */ export type ScheduleChange = { readonly kind: string; readonly id: string; readonly gesture: ScheduleGestureValue; } | { readonly kind: string; readonly id: string; readonly row: Uint8Array; readonly created?: true | undefined; } | { readonly kind: string; readonly id: string; /** * The row as the gesture leaves it, from the row as it finds it — drafted, else as the record holds it: the * row the session's original is, so a rewrite never rests on a copy older than it. `undefined` refuses * it, and the gesture records nothing. */ readonly rewrite: (row: Uint8Array) => Uint8Array | undefined; /** * The row the gesture writes where it finds none, made only then: a period its record holds no row of, which * Publish writes afresh from the app's `values`. Left out, or making none — `values` that fail as they run — * a gesture that finds no row records nothing. */ readonly absent?: (() => Uint8Array | undefined) | undefined; } | { readonly kind: string; readonly id: string; readonly remove: true; }; /** * A record a builder writes beside its event kinds, a whole row at a time * (#1369): the Roster's weeks (`Schedule.periods`) and targets * (`Roster.targets`), and each of the Schematic's kinds and their details * (#1388). Its session joins the history as a kind's does; a change names it * by its key, and gives its new row whole, rewrites it, or removes it. */ export interface ScheduleEditingRecord { /** What a change names it by (`periods`, `targets`, a Schematic kind's slot): distinct from every other's. */ readonly key: string; /** What its banners call it. */ readonly name: string; /** Its session over the record: its entry and key types, and its patch door. */ readonly editing: ValueTypeOf["editing"]; /** The record read by key, for one read a window at a time; `none` for one read whole. */ readonly entries: ValueTypeOf["entries"]; /** The app's check over its drafted rows, which holds Save, naming the row; none unless given. */ readonly ready?: ValueTypeOf["ready"] | undefined; /** * Its row's form, as its builder's inspector draws it (#1585): a changed row's number outside its field's bounds * holds Save as the app's check does, naming the row and the field. None unless given. */ readonly fields?: readonly FieldSpecValue[] | undefined; /** * Whether the builder draws what a confirmed Save landed of it over the rows it read before the commit, until they * are read again (`EditSession.landed`): a record read by key whose rows the builder reads apart from the * confirmation — the Schematic's kinds read by area, their windows. `false` by default. */ readonly drawsLanded?: boolean | undefined; } /** * A row as a builder's own reads hold it, by its key's text (#1369): `undefined` * for one they do not hold, which is read by its key; `none` while they are * read; `some(none)` for one they hold the record has none of; else its bytes. * Every read of a record is pinned to one revision, so a builder's window * holds its rows at the revision a Save committed once it lands: the Save * confirms what the window holds from it, and reads by key only the rest. */ export type ScheduleRowRead = (id: string) => option> | undefined; /** * Each kind's or record's rows as its builder draws them now, by its slot or key, then by the row's key's text: its * bytes, or `undefined` for one the builder draws none of ({@link ScheduleEditingArgs.standing}). */ export type ScheduleStandingRows = Readonly Uint8Array | undefined>>; /** An event a confirmed Save landed, as it left it ({@link ScheduleLandedRows}). */ export interface ScheduleLandedRow { /** The event as the Save left it, as the seams take a draft: its row, or `missing` for one the Save deleted. */ readonly draft: Uint8Array; /** Its row as the Save left it, as a reader of the kind's cells holds a row; `undefined` for one the Save deleted. */ readonly row: Uint8Array | undefined; } /** * What confirmed Saves landed of a kind whose events its builder reads apart from a Save's confirmation — a kind a reader * reads by its cells (#1424) — as its session keeps them (`EditSession.landed`), each event encoded once; and the * revision each of their Saves was drafted at, which tells a read of the kind from before them from one that holds them * (east-ui-components' `readBeforeSaves`). */ export interface ScheduleLandedRows { /** Each event a Save landed, by its key's text. */ readonly rows: ReadonlyMap; /** The revisions the Saves' requests were drafted at; `undefined` among them for an event held from none (`LandedEntry.from`). */ readonly from: ReadonlySet; } /** What confirmed Saves landed of each kind that draws them, by the kind's slot. */ export type ScheduleLanded = ReadonlyMap; /** * The new events the last carry-over of each kind's drafts gave another key (#1583), by the kind's slot, then by the key * the event had (`EditSession.renumbered`): the key made for each was taken by another Save's own new event. */ export type ScheduleRenumbered = ReadonlyMap>; /** One kind's session, or one record's, as the history holds it, for its banners. */ export interface ScheduleKindSession { /** The kind's slot, or the record's key. */ readonly kind: string; /** Its key in the history. */ readonly key: string; /** Its session. */ readonly session: EditSession; /** Its row's form, as its builder's inspector draws it: what its issues name a field by (#1585). */ readonly fields: readonly FieldSpecValue[]; } /** What {@link useScheduleEditing} hands the canvas. */ export interface ScheduleEditing { /** The one history across the kinds, the records and the joined sessions — `data`'s. */ readonly history: EditHistory; /** Each kind's session, in the kinds' order, then each record's. */ readonly sessions: readonly ScheduleKindSession[]; /** Every kind's drafts and every record's, by kind or record then by entry id — the same object while their bytes hold. */ readonly drafts: ScheduleEventDraftsValue; /** Moves each time the drafts do. */ readonly draftsVersion: number; /** * What confirmed Saves landed of each kind given {@link ScheduleEditingArgs.drawsLanded}, by its slot: what a reader * of its cells draws over cells read before the commit, until they are read again ({@link ScheduleEditing.shown}). * The same object while nothing landed moves, a kind's the same while its own holds; one empty object while nothing * is held. */ readonly landed: ScheduleLanded; /** * The new events the last carry-over of each kind's drafts gave another key ({@link ScheduleEditingArgs.rekeys}): * what a builder moves on to the new key with them — its selection. The same object until a carry-over gives an * event another key; one empty object while none has. */ readonly renumbered: ScheduleRenumbered; /** The history's version: it moves with every change of it or of its sessions. */ readonly version: number; /** * Lets go of what Saves landed of a kind's events, once its builder's reads hold them as the record does now * ({@link ScheduleEditing.landed}). * * @param kind - The kind's slot * @param ids - The events' keys' texts */ shown(kind: string, ids: Iterable): void; /** * A kind's drafts, or a record's, by entry id: what its seams read. * * @param kind - The kind's slot, or the record's key * @returns Its drafts; none for one with none */ draftsOf(kind: string): ScheduleKindDraftsValue; /** * Whether a kind or a record takes a gesture now: its record read, its session writable. * * @param kind - The kind's slot, or the record's key */ available(kind: string): boolean; /** * Whether a kind's session — or a record's — has yet to judge its drafts where its record reads now (#1599): it is * carrying them over to a record that moved, or — from the very render the record moves in, before the session is * told — it stands, no request of its own out, at another revision than the one its record reads at. What it says * of a drafted row meanwhile, that its record holds it ({@link ScheduleEditing.held}), is of the record as it was: a * builder keeps such a row as it last drew it (`drafted-rows.ts`' `DraftedStanding`). Tracked where it is called: * it reads the record's revision. * * @param kind - The kind's slot, or the record's key * @returns Whether it has; never for one read whole, whose session judges its drafts as its record is read */ carrying(kind: string): boolean; /** * Whether an event — or a row of a record a builder writes — holds a * conflict not yet chosen: what wears the warn ring on a canvas. * * @param kind - The kind's slot, or the record's key * @param id - The event's key's text */ conflicted(kind: string, id: string): boolean; /** * An event's row as its kind's record held it when last read — what its * drafts are tinted against — or a row of a record a builder writes. * * @param kind - The kind's slot, or the record's key * @param id - The event's key's text * @returns The row, decoded; `undefined` for an event the record does not hold — and, for a kind read a window at * a time (#1199), for one its drafts do not hold, which stands as the record holds it */ held(kind: string, id: string): unknown; /** * An event as its kind's record held it when last read ({@link ScheduleEditing.held}), read as its kind reads a row * — its title, its start and its end — with none of its drafts in place and no read of the record: what names an * event its drafts removed. * * @param kind - The kind's slot * @param id - The event's key's text * @returns The event; `undefined` for one {@link ScheduleEditing.held} holds no row of, or a record beside the kinds * @throws {Error} When the kind's read of the row fails */ heldEvent(kind: string, id: string): ScheduleRead | undefined; /** * One event as its kind reads it now, its drafts in place (#1197, #1199): * what a gesture, the inspector and a banner read of it. Where the * builder's own reads hold its row (`reads`: a page of the Calendar's * Backlog, #1424), it is read from that row; else a kind read a window at * a time looks for it among the rows its windows hold over the range the * canvas reads, then reads it by its key — tracked, inside a tracked * evaluation, so the read lands there. * * @param kind - The kind's slot * @param id - The event's key's text * @returns The event as Plan draws it, and its row; `undefined` for no such event, or one whose read is in flight * @throws {Error} When the kind's read fails */ read(kind: string, id: string): ScheduleRead | undefined; /** * A new event's key, as its text — its kind's session's to make * (`EditSession.mint`): a text key from an event's key or a template's, * else made up — the next in a row (`J-1001-2`) for a kind whose record * its session holds whole, and with a suffix drawn at random * (`J-1001-5f3a9c2e`) for one read a window at a time, whose earlier * copies stand out of view; an Integer key one past the largest; past * every key the record is known to hold, the drafts hold, the builder * shows a row under and `minted` holds. Two sessions that hold a record * whole make the same key from the same one: where another Save takes it * first, a kind given {@link ScheduleEditingArgs.rekeys} has the event * made another, and any other raises the two as a conflict. * * @param kind - The kind's slot, or the record's key * @param from - The key's text it is made from; empty for a row made from none * @param minted - The keys a gesture has made already * @returns The key's text; `undefined` for a kind keyed by neither String nor Integer, and while an Integer kind * read a window at a time has its largest key read */ mint(kind: string, from: string, minted: ReadonlySet): string | undefined; /** * Record one gesture as one step of the history: each event it changes, * in its kind's session, and each row of a record, in the record's — a * record's row given whole, rewritten from the row as it stands, or * removed. * * @param changes - Each event or row it changes, and how; one changed twice composes, in order * @param origin - The gesture * @param label - Its label in the history * @returns Whether anything changed; nothing is, when a rewrite refuses, or finds no row and gives none in its place */ record(changes: readonly ScheduleChange[], origin: Origin, label: string): boolean; } /** What {@link useScheduleEditing} reads. */ export interface ScheduleEditingArgs { /** The event kinds, as the payload carries them. */ kinds: readonly ScheduleEditingKind[]; /** The records the builder writes beside its kinds, a whole row at a time (#1369): a session each, after the kinds'. */ records?: readonly ScheduleEditingRecord[] | undefined; /** * Each kind's or record's rows as the builder's own reads hold them, by its slot or key (#1369): what a gesture * finds an entry as before it, and what a Save confirms its entries by, before any is read by its key. */ reads?: Readonly> | undefined; /** * Where the rows the builder draws now are kept, by kind or record ({@link ScheduleStandingRows}): what its person * sees. A gesture's original is found there first, after the session's own version and what its last Save left * (`EditSession.startOf`) — so a gesture begins from the row shown, and is taken at once while a record's rows are * read again after a Save, or after another session's write, never refused for a read on its way (a Save never * stops the editing). Never a Save's confirmation, which reads the revision it committed. A row drawn may have been * read at the revision before: where another write changed the same part of it since, the source refuses the Save * that sends it, and the session carries its drafts over at its own revision (`EditSession.nameConflicts`), the * part both changed a conflict to choose. Read as a gesture is recorded, so rows moving make no new session. */ standing?: RefObject | undefined; /** The canvas's storage key: the sessions and their history are kept under it. */ storageKey: string; /** Sessions made elsewhere that join the history, ahead of the kinds': `data`'s. */ joined: readonly EditHistoryJoined[]; /** The range the canvas reads its event kinds' rows over (#1199): a kind read a window at a time holds every event it draws there. */ range?: ScheduleRange | undefined; /** * The record Slices over the edited kinds' primary records, for confirmed Save feedback: each kind's is the one * over its record (`sourceId`) — the Calendar's, its slice's part named after the kind (#1343). */ recordSlices?: readonly ValueTypeOf[] | undefined; /** * Where the rows each kind read by its cells holds are kept (#1039), by the kind's slot, then by the event's key's * text: where a gesture, the inspector and a banner find one of its events, with no read of its own; one they do not * hold is read by its key, never over the window. Read as each read runs, so new cells make no new kinds and draw * nothing (#1594). */ held?: RefObject | undefined; /** * The kinds whose events the builder reads apart from a Save's confirmation, by their slots: those a reader may read * by their cells (#1424), whose cells stand at the revision they were read at until they are read again. What each * Save lands of them is kept (`EditSession.landed`) and handed on ({@link ScheduleEditing.landed}) until the builder * lets it go. None by default: a kind read over the window keeps its drafted read until the read again lands. */ drawsLanded?: ReadonlySet | undefined; /** * The kinds whose new events take another key where another Save took the one made for them (#1583), by their * slots: two people who each make an event from one template, over a kind whose record their sessions hold whole, * are given the same key ({@link ScheduleEditing.mint}), and the event made here goes on under another its session * makes — both kept, nothing to choose. Only for a kind none of whose events anything else the builder drafts names by its key: under * a new key, such a name would be left on the other session's event (keys.ts' `onceKeys`). None by default: the * two are a conflict to choose between. Its builder moves on what it holds by the old key * ({@link ScheduleEditing.renumbered}). */ rekeys?: ReadonlySet | undefined; /** * What a conflict at a kind's quantity, or at its split across resources, is called — the builder's own word for * a quantity: the two change together, so the conflicts popover shows them as one card under it. */ quantityLabel?: string | undefined; /** * The fields of a kind's form a changed row's numbers are held to the bounds of (#1585), by the kind's slot, from the * row as its draft holds it — every bounded field of its form unless given: an Allocation's reservation holds no * quantity until an arrival is booked into it, so none of its quantity's bounds are its own. */ boundsOf?: Readonly readonly FieldSpecValue[]>> | undefined; /** * What the builder's author begins its Autosave at — its payload's `autosave`, which every builder hands on, so none * leaves its author's word unread. Its viewer's own choice, once made, stands over it (east-ui-components' * `useEditHistory`). */ autosave: boolean; } export declare const EMPTY_SCHEDULE_DRAFTS: ScheduleKindDraftsValue; /** * The event kinds' sessions and the records' under one history with the * joined ones — see the module docs. * * @param args - The kinds, the records beside them, the canvas's storage key, the sessions that join, and the range the canvas reads * @returns The history, the kinds' and the records' sessions and drafts, how an event is read, and how a gesture is recorded */ export declare function useScheduleEditing({ kinds, records, reads, standing, storageKey, joined, range, recordSlices, held: cellRows, drawsLanded, rekeys, quantityLabel, boundsOf, autosave, }: ScheduleEditingArgs): ScheduleEditing; export {}; //# sourceMappingURL=editing.d.ts.map