feat(webui): customizable dashboard + nav layout (phase 3)

The "C" half — reorder + show/hide the overview cards and the sidebar nav,
persisted to config/ui.json's layout section.

- LayoutContext loads the layout (authed GET /api/ui) and persists edits via
  the section-merge POST /api/ui {layout} (debounced). reconcileLayoutItems
  keeps stored order/visibility, appends new catalogue entries (forward-compat),
  and forces pinned items visible.
- Overview is refactored into a block registry (stats/connections/alerts/host/
  sessions). A 「编辑布局」 mode (motion Reorder — zero new deps) edits both the
  cards and the nav in place, with per-item show/hide and a reset; an all-hidden
  dashboard shows an empty state with 恢复默认.
- Anti-self-lock: PINNED_NAV = ['/', '/settings'] can be reordered but never
  hidden — the overview hosts the layout editor and settings hosts
  account/appearance, so the user can never strand themselves.
- api.ui.save now takes Partial<UiConfig> (the section-merge contract).

A late initial GET can't clobber an in-flight edit (dirtyRef); a pending edit
flushes on unmount. Verified across 2 adversarial review rounds.
This commit is contained in:
DrSmoothl committed 2026-06-13 01:15:00 +08:00
1 parent ac1ffc46d3
commit 69ed8fc722
6 files changed
+613 -225

No files matched your search

@@ -6,6 +6,7 @@ import { Separator } from '@/components/ui/separator';
import { cn } from '@/lib/utils';
import { APP_NAME, APP_VERSION } from '@/types';
import { useAppState } from '@/contexts/AppStateContext';
import { reconcileLayoutItems, useLayout } from '@/contexts/LayoutContext';
import type { AppPath } from '@/router';
export interface NavItem {
@@ -23,6 +24,12 @@ export const NAV_ITEMS: NavItem[] = [
{ to: '/settings', label: '系统设置', icon: SlidersHorizontal, description: '主题与账号' },
];
// Anti-self-lock: these nav items can be reordered but never hidden.
// '/' — hosts the 「编辑布局」 entry point; hiding it would strand the
// user with no way back to un-hide anything.
// '/settings'— account + appearance.
export const PINNED_NAV: AppPath[] = ['/', '/settings'];
interface SidebarProps {
collapsed?: boolean;
onItemClick?: () => void;
@@ -31,6 +38,14 @@ interface SidebarProps {
export function Sidebar({ collapsed = false, onItemClick }: SidebarProps) {
const pathname = useRouterState({ select: (s) => s.location.pathname });
const { updateInfo } = useAppState();
const { navItems } = useLayout();
// Render nav in the operator's configured order, hidden items removed,
// pinned items forced on, and forward-compatible with future routes.
const orderedNav = reconcileLayoutItems(navItems, NAV_ITEMS.map((i) => i.to), PINNED_NAV)
.filter((i) => i.visible)
.map((i) => NAV_ITEMS.find((n) => n.to === i.id))
.filter((n): n is NavItem => !!n);
return (
<div className="flex h-full w-full flex-col bg-sidebar text-sidebar-foreground">
@@ -53,7 +68,7 @@ export function Sidebar({ collapsed = false, onItemClick }: SidebarProps) {
{/* Nav */}
<ScrollArea className="flex-1 min-h-0" viewportClassName="[&>div]:!block">
<nav className={cn('flex flex-col gap-1 p-2', collapsed && 'items-center')}>
{NAV_ITEMS.map(({ to, label, icon: Icon, description }) => {
{orderedNav.map(({ to, label, icon: Icon, description }) => {
const isActive = pathname === to;
return (
<Link