feat(webui): free-form draggable/resizable dashboard grid (phase 4)

The overview becomes a gridstack-backed free grid — drag, resize, and show/
hide 9 widgets (the 5 top stat tiles split out of the old single block, plus
connections/alerts/host/sessions), persisted to config/ui.json's
layout.overviewBlocks as {id,x,y,w,h,visible}.

- gridstack (zero-dep, vanilla; chosen over react-grid-layout whose
  react-draggable uses the React-19-removed findDOMNode). React renders each
  widget into a gridstack item via a portal (context flows through); gridstack
  owns layout/drag/resize. Per-widget minW/minH prevent content collapse.
- Desktop = editable in place; narrow screens = auto single-column, read-only.
  Fixed cellHeight => no ResizeObserver => the narrow column-clamp never
  persists over the desktop coords.
- Edit mode shows a panel: widget show/hide + nav reorder (the phase-3 motion
  list); the grid below is live-editable. Reset + empty-state preserved.
- Server UiLayoutItem gains optional x/y/w/h (clamped); the stats->5-tiles
  migration + default placement live client-side (lib/dashboard-layout.ts).

Verified across 2 adversarial review rounds (gridstack+React lifecycle traced
against the library source: StrictMode-safe, no mount-persist loop, no
re-init on coord change, no narrow-visit data loss).
This commit is contained in:
DrSmoothl committed 2026-06-13 21:17:23 +08:00
1 parent a5335687ec
commit f07f309c51
10 files changed
+532 -226

No files matched your search

+30 -1
View File
@@ -92,6 +92,16 @@ export interface UiAppearance {
export interface UiLayoutItem {
id: string;
visible: boolean;
/**
* Grid position/size (overview blocks only; nav items omit them). The
* client owns the widget catalogue, per-widget min sizes, and the
* `stats`→tiles migration — the server just preserves + clamps these to
* sane integer bounds so a corrupt file can't store absurd coords.
*/
x?: number;
y?: number;
w?: number;
h?: number;
}
export interface UiLayout {
@@ -188,6 +198,15 @@ function clampNum(value: unknown, min: number, max: number, fallback: number): n
return Math.min(max, Math.max(min, n));
}
function clampInt(value: unknown, min: number, max: number, fallback: number): number {
return Math.trunc(clampNum(value, min, max, fallback));
}
function isFiniteNum(value: unknown): boolean {
if (typeof value === 'number') return Number.isFinite(value);
return typeof value === 'string' && value.trim() !== '' && Number.isFinite(Number(value));
}
function boolOr(value: unknown, fallback: boolean): boolean {
return typeof value === 'boolean' ? value : fallback;
}
@@ -216,7 +235,17 @@ function normalizeLayoutItems(value: unknown, fallback: UiLayoutItem[]): UiLayou
const id = idOr(raw.id, '');
if (!id || seen.has(id)) continue;
seen.add(id);
out.push({ id, visible: boolOr(raw.visible, true) });
const item: UiLayoutItem = { id, visible: boolOr(raw.visible, true) };
// Grid coords are optional (overview blocks carry them; nav items don't).
// Preserve + clamp when any are present; missing ones get safe defaults
// and the client re-applies per-widget min sizes on load.
if (isFiniteNum(raw.x) || isFiniteNum(raw.y) || isFiniteNum(raw.w) || isFiniteNum(raw.h)) {
item.x = clampInt(raw.x, 0, 50, 0);
item.y = clampInt(raw.y, 0, 1000, 0);
item.w = clampInt(raw.w, 1, 12, 1);
item.h = clampInt(raw.h, 1, 100, 1);
}
out.push(item);
}
// An array that parsed to nothing usable is indistinguishable from corrupt;
// fall back so the frontend always has a baseline to reconcile against.