Skip to content

State hooks

Small state hooks, useSelection and useSinceLastVisit, with newId and versionedStore.

API

Functions

FunctionSignatureDoes
useSelection(keys: readonly string[]): SelectionThe ticked rows of a list for a bulk bar, kept to the rows still shown: a row that leaves the list (decided, filtered out) leaves the selection with it.
useSinceLastVisit(key: string, now: number, fallbackMs?: number): stringWhen this browser last opened the page, as an ISO time: fallbackMs (24 h) before now on the server and while hydrating, then the time this browser stored on its last visit. The visit is stored once, so the next one reports from now. A per-viewer convenience: with storage refused it keeps reporting the fallback window.
newId(): stringAn id a server accepts as the client's own (a UUID where the platform has one).
versionedStore<T extends object>({ key, v, parse, }: { key: string; v: number; parse: (value: Record<string, unknown>) => T | null; }): VersionedStore<T>A versioned record in this browser: stored as JSON with v beside its own fields, so a record written by an older format is dropped instead of misread. parse checks the fields and answers the record or null. Storage can be refused; the record is then kept for this page only.
parseVersioned<T>(raw: string | null, v: number, parse: (value: Record<string, unknown>) => T | null): T | nullA stored string as version v of a record, or null.
selectionOf(selected: ReadonlySet<string>, keys: readonly string[]): { ticked: string[]; all: boolean; some: boolean; }Which of keys are selected, kept to the keys still shown, and whether that is none, some or all.
sinceOf(stored: string | null, now: number, fallbackMs: number): stringThe start of the "since you last looked" window: the stored time, else fallbackMs before now.