Files
doona/src/shell/widgets/storedLayout.ts
T
Zakk 2cd941d455 feat(widgets): configurable activity dashboard and floating panel
Activity becomes a dashboard of ordered sections whose default layout
reproduces main's Activity page card for card. Each card keeps a preset
size that changes only its own span, and sections pack like masonry: a
tall card sits beside a stack of shorter ones and every column ends level
with its neighbour, in the live page and the editor alike. Extension cards
add node latency (drawn with the Latency tab's dumbbell), source health,
connections by outbound and network, DNS answers and policy group nodes,
each up to three instances with its own display.

Edit mode renders the same cells in a sortable grid with drag, keyboard
and touch reordering, move-only drops, a gap-line drop indicator, a size
and display inspector, a widget gallery with equal preview frames, reset
to the default and a confirmation before discarding a changed draft. The
canvas reads cached data without live subscriptions, and a runtime error
shows above the cards when the status card is removed.

The floating widget panel sits at the bottom inline-end corner, resizes by
pointer or keyboard within stored limits, collapses to its header with the
current rates, and can be pinned so it stays open across pages. The panel
and the editor load on demand to keep the startup shell small.
2026-10-02 16:11:18 +10:00

64 lines
2.0 KiB
TypeScript

import {useSyncExternalStore} from 'react';
export function storedLayout<Layout extends {version: number}>(
key: string,
parse: (value: unknown) => Layout,
defaults: () => Layout,
migrate?: (layout: Layout, absent: boolean, persist: (layout: Layout) => boolean) => Layout
) {
let cached: Layout | undefined;
let lastStored: string | null | undefined;
const listeners = new Set<() => void>();
function read(): Layout {
if (!cached) {
try {
lastStored = localStorage.getItem(key);
cached = parse(JSON.parse(lastStored ?? 'null'));
if (migrate) cached = migrate(cached, lastStored === null, persist);
} catch {
cached = defaults();
}
}
return cached;
}
function save(update: Layout | ((previous: Layout) => Layout)) {
let previous = read();
if (typeof update === 'function') {
try {
const stored = localStorage.getItem(key);
if (lastStored !== undefined && stored !== lastStored) previous = parse(JSON.parse(stored ?? 'null'));
lastStored = stored;
} catch {
/* Keep the session layout when storage is blocked. */
}
}
const layout = typeof update === 'function' ? update(previous) : update;
cached = layout;
persist(layout);
listeners.forEach(notify => notify());
}
function persist(layout: Layout) {
try {
const serialized = JSON.stringify(layout);
localStorage.setItem(key, serialized);
lastStored = serialized;
return true;
} catch {
return false;
}
}
function subscribe(notify: () => void) {
listeners.add(notify);
const sync = (event: StorageEvent) => {
if (event.key !== key && event.key !== null) return;
cached = undefined;
notify();
};
addEventListener('storage', sync);
return () => {
listeners.delete(notify);
removeEventListener('storage', sync);
};
}
return {read, save, useLayout: () => useSyncExternalStore(subscribe, read)};
}