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

+13 -11
View File
@@ -1,5 +1,6 @@
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, type ReactNode } from 'react';
import { useApi } from '@/lib/api';
import { defaultOverviewGrid, migrateOverviewBlocks } from '@/lib/dashboard-layout';
import type { UiLayout, UiLayoutItem } from '@/types';
// Client-side layout customization (the "C" half). The server stores the
@@ -11,14 +12,6 @@ import type { UiLayout, UiLayoutItem } from '@/types';
// (`reconcileLayoutItems`), so the context stays a dumb store and adding a new
// block / nav item later doesn't need a context change — it just appears.
const DEFAULT_OVERVIEW_BLOCKS: UiLayoutItem[] = [
{ id: 'stats', visible: true },
{ id: 'connections', visible: true },
{ id: 'alerts', visible: true },
{ id: 'host', visible: true },
{ id: 'sessions', visible: true },
];
const DEFAULT_NAV_ITEMS: UiLayoutItem[] = [
{ id: '/', visible: true },
{ id: '/processes', visible: true },
@@ -28,7 +21,9 @@ const DEFAULT_NAV_ITEMS: UiLayoutItem[] = [
];
export const DEFAULT_LAYOUT: UiLayout = {
overviewBlocks: DEFAULT_OVERVIEW_BLOCKS.map((i) => ({ ...i })),
// Overview blocks are the positioned grid widgets (the catalogue owns the
// default placement + the legacy `stats`→tiles migration).
overviewBlocks: defaultOverviewGrid(),
navItems: DEFAULT_NAV_ITEMS.map((i) => ({ ...i })),
};
@@ -87,7 +82,14 @@ export function LayoutProvider({ children }: { children: ReactNode }) {
const config = await api.ui.get();
// Seed from the server only if the user hasn't already edited (the
// overview + editor are interactive immediately, before this resolves).
if (!cancelled && !dirtyRef.current && config?.layout) setLayout(config.layout);
// Overview blocks are migrated to the current grid catalogue (legacy
// `stats`→tiles, default coords for new/coordless widgets).
if (!cancelled && !dirtyRef.current && config?.layout) {
setLayout({
overviewBlocks: migrateOverviewBlocks(config.layout.overviewBlocks),
navItems: config.layout.navItems,
});
}
} catch {
/* keep defaults — layout is non-critical */
}
@@ -125,7 +127,7 @@ export function LayoutProvider({ children }: { children: ReactNode }) {
const resetLayout = useCallback(() => {
persist({
overviewBlocks: DEFAULT_OVERVIEW_BLOCKS.map((i) => ({ ...i })),
overviewBlocks: defaultOverviewGrid(),
navItems: DEFAULT_NAV_ITEMS.map((i) => ({ ...i })),
});
}, [persist]);