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

@@ -0,0 +1,99 @@
import type { UiLayoutItem } from '@/types';
// The overview dashboard is a free 12-column gridstack grid. This module is
// the pure (React-free) catalogue + migration so it can be shared by the
// LayoutContext (defaults / reset / migrate-on-load), the DashboardGrid
// (render + min sizes), and the layout editor (labels) without import cycles.
export const GRID_COLS = 12;
/** px per grid row. Tiles are 1 row; cards a few rows. */
export const GRID_CELL_HEIGHT = 80;
export const GRID_MARGIN = 8;
export interface GridWidgetSpec {
id: string;
label: string;
/** Minimum width/height in grid units — prevents the content from collapsing. */
minW: number;
minH: number;
/** Default placement when the operator hasn't positioned it yet. */
def: { x: number; y: number; w: number; h: number };
}
/** The five top stat tiles, split out of the legacy single `stats` block. */
export const STAT_TILE_IDS = [
'stat:status', 'stat:accounts', 'stat:processes', 'stat:host', 'stat:uptime',
] as const;
export const GRID_WIDGETS: GridWidgetSpec[] = [
{ id: 'stat:status', label: '服务状态', minW: 2, minH: 1, def: { x: 0, y: 0, w: 2, h: 1 } },
{ id: 'stat:accounts', label: '在线账号', minW: 2, minH: 1, def: { x: 2, y: 0, w: 2, h: 1 } },
{ id: 'stat:processes', label: '进程注入', minW: 2, minH: 1, def: { x: 4, y: 0, w: 2, h: 1 } },
{ id: 'stat:host', label: '主机名', minW: 2, minH: 1, def: { x: 6, y: 0, w: 2, h: 1 } },
{ id: 'stat:uptime', label: '系统运行', minW: 2, minH: 1, def: { x: 8, y: 0, w: 2, h: 1 } },
{ id: 'connections', label: 'OneBot 连接', minW: 3, minH: 3, def: { x: 0, y: 1, w: 6, h: 4 } },
{ id: 'alerts', label: '最近告警', minW: 3, minH: 3, def: { x: 6, y: 1, w: 6, h: 4 } },
{ id: 'host', label: '主机资源', minW: 4, minH: 3, def: { x: 0, y: 5, w: 12, h: 4 } },
{ id: 'sessions', label: '在线会话', minW: 3, minH: 3, def: { x: 0, y: 9, w: 12, h: 5 } },
];
const WIDGET_BY_ID = new Map(GRID_WIDGETS.map((w) => [w.id, w]));
export function widgetLabel(id: string): string {
return WIDGET_BY_ID.get(id)?.label ?? id;
}
function isObj(v: unknown): v is Record<string, unknown> {
return typeof v === 'object' && v !== null && !Array.isArray(v);
}
function validCoords(item: UiLayoutItem | undefined): boolean {
return !!item
&& typeof item.x === 'number' && typeof item.y === 'number'
&& typeof item.w === 'number' && typeof item.h === 'number';
}
/** A fully-positioned overview block (coords always present). */
export function defaultOverviewGrid(): UiLayoutItem[] {
return GRID_WIDGETS.map((w) => ({ id: w.id, visible: true, ...w.def }));
}
/**
* Bring any stored overview layout up to the current grid catalogue:
* - expand the legacy single `stats` block into the 5 individual tiles
* (inheriting its visibility),
* - keep stored coords/visibility for known widgets, assign the default
* placement to any widget missing coords,
* - append catalogue widgets the stored layout predates (visible),
* - drop unknown ids.
* Output items always carry x/y/w/h.
*/
export function migrateOverviewBlocks(stored: UiLayoutItem[] | undefined): UiLayoutItem[] {
const byId = new Map<string, UiLayoutItem>();
for (const raw of Array.isArray(stored) ? stored : []) {
if (isObj(raw) && typeof raw.id === 'string') byId.set(raw.id, raw as UiLayoutItem);
}
// Legacy: a single `stats` block → seed the 5 tiles' visibility from it,
// but only when the tiles aren't already individually present.
const legacyStats = byId.get('stats');
return GRID_WIDGETS.map((spec) => {
const item = byId.get(spec.id);
const isStatTile = (STAT_TILE_IDS as readonly string[]).includes(spec.id);
const visible = item
? item.visible !== false
: isStatTile && legacyStats
? legacyStats.visible !== false
: true;
const coords = validCoords(item)
? { x: item!.x!, y: item!.y!, w: item!.w!, h: item!.h! }
: { ...spec.def };
return { id: spec.id, visible, ...coords };
});
}
export function minSizeOf(id: string): { minW: number; minH: number } {
const spec = WIDGET_BY_ID.get(id);
return { minW: spec?.minW ?? 1, minH: spec?.minH ?? 1 };
}