diff --git a/packages/webui/src/components/layout/main-layout.tsx b/packages/webui/src/components/layout/main-layout.tsx index 0006f9dc..2c8b96df 100644 --- a/packages/webui/src/components/layout/main-layout.tsx +++ b/packages/webui/src/components/layout/main-layout.tsx @@ -7,6 +7,7 @@ import { Sidebar } from '@/components/layout/sidebar'; import { TopBar } from '@/components/layout/top-bar'; import { useMediaQuery } from '@/hooks/use-media-query'; import { useTheme } from '@/contexts/ThemeContext'; +import { useLayout } from '@/contexts/LayoutContext'; import { cn } from '@/lib/utils'; interface MainLayoutProps { @@ -27,6 +28,10 @@ export function MainLayout({ status, onLogout, children }: MainLayoutProps) { return appearance.sidebarDefaultCollapsed; }); const customBg = appearance.background.type !== 'none'; + const { editing } = useLayout(); + // Force the sidebar open while editing layout so its drag-to-reorder list + // has room (it returns to the user's collapsed pref on 完成). + const effectiveCollapsed = collapsed && !editing; const [mobileOpen, setMobileOpen] = useState(false); const pathname = useRouterState({ select: (s) => s.location.pathname }); @@ -40,11 +45,11 @@ export function MainLayout({ status, onLogout, children }: MainLayoutProps) { {!isMobile && ( - + )} @@ -66,11 +71,12 @@ export function MainLayout({ status, onLogout, children }: MainLayoutProps) {
setCollapsed((v) => !v)} onOpenMobile={() => setMobileOpen(true)} onLogout={onLogout} isMobile={isMobile} + editing={editing} />
diff --git a/packages/webui/src/components/layout/sidebar.tsx b/packages/webui/src/components/layout/sidebar.tsx index 37024a0b..e92f30dd 100644 --- a/packages/webui/src/components/layout/sidebar.tsx +++ b/packages/webui/src/components/layout/sidebar.tsx @@ -1,5 +1,5 @@ -import { LayoutDashboard, PlugZap, Settings, Sparkles, SlidersHorizontal, Terminal } from 'lucide-react'; -import { motion } from 'motion/react'; +import { Check, Eye, EyeOff, GripVertical, LayoutDashboard, Lock, PlugZap, Settings, Sparkles, SlidersHorizontal, Terminal } from 'lucide-react'; +import { motion, Reorder } from 'motion/react'; import { Link, useRouterState } from '@tanstack/react-router'; import { ScrollArea } from '@/components/ui/scroll-area'; import { Separator } from '@/components/ui/separator'; @@ -38,15 +38,23 @@ interface SidebarProps { export function Sidebar({ collapsed = false, onItemClick }: SidebarProps) { const pathname = useRouterState({ select: (s) => s.location.pathname }); const { updateInfo } = useAppState(); - const { navItems } = useLayout(); + const { navItems, setNavItems, editing, setEditing } = 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) + // Full reconciled nav (incl. hidden) — pinned forced visible, forward-compat. + const reconciled = reconcileLayoutItems(navItems, NAV_ITEMS.map((i) => i.to), PINNED_NAV); + // View mode: configured order, hidden removed. + const orderedNav = reconciled .filter((i) => i.visible) .map((i) => NAV_ITEMS.find((n) => n.to === i.id)) .filter((n): n is NavItem => !!n); + const reorderNav = (ids: string[]) => { + const byId = new Map(reconciled.map((i) => [i.id, i])); + setNavItems(ids.map((id) => byId.get(id)).filter((x): x is NonNullable => !!x)); + }; + const toggleNav = (id: string) => + setNavItems(reconciled.map((i) => (i.id === id ? { ...i, visible: !i.visible } : i))); + return (
{/* Brand */} @@ -67,42 +75,94 @@ export function Sidebar({ collapsed = false, onItemClick }: SidebarProps) { {/* Nav */} - + 完成 + +
+ i.id)} onReorder={reorderNav} className="flex flex-col gap-1"> + {reconciled.map((item) => { + const meta = NAV_ITEMS.find((n) => n.to === item.id); + if (!meta) return null; + const Icon = meta.icon; + const pinned = (PINNED_NAV as string[]).includes(item.id); + return ( + + + + {meta.label} + {pinned ? ( + + + + ) : ( + + )} + + ); + })} + +
+ ) : ( + + )} diff --git a/packages/webui/src/components/layout/top-bar.tsx b/packages/webui/src/components/layout/top-bar.tsx index b5198cbe..e86993ee 100644 --- a/packages/webui/src/components/layout/top-bar.tsx +++ b/packages/webui/src/components/layout/top-bar.tsx @@ -15,6 +15,8 @@ interface TopBarProps { onOpenMobile: () => void; onLogout: () => void; isMobile: boolean; + /** Layout edit mode force-expands the sidebar, so collapsing is disabled. */ + editing?: boolean; } export function TopBar({ @@ -24,6 +26,7 @@ export function TopBar({ onOpenMobile, onLogout, isMobile, + editing = false, }: TopBarProps) { const [confirmLogout, setConfirmLogout] = useState(false); const pathname = useRouterState({ select: (s) => s.location.pathname }); @@ -43,8 +46,9 @@ export function TopBar({ variant="ghost" size="icon-sm" onClick={onToggleCollapse} + disabled={editing} aria-label={collapsed ? '展开侧边栏' : '收起侧边栏'} - title={collapsed ? '展开侧边栏' : '收起侧边栏'} + title={editing ? '编辑布局时侧栏保持展开' : collapsed ? '展开侧边栏' : '收起侧边栏'} > {collapsed ? : } diff --git a/packages/webui/src/components/pages/dashboard-grid.tsx b/packages/webui/src/components/pages/dashboard-grid.tsx index b6282d44..9a94261a 100644 --- a/packages/webui/src/components/pages/dashboard-grid.tsx +++ b/packages/webui/src/components/pages/dashboard-grid.tsx @@ -1,6 +1,7 @@ import { useLayoutEffect, useRef, useState, type ReactNode } from 'react'; import { createPortal } from 'react-dom'; import { GridStack, type GridStackWidget } from 'gridstack'; +import { Eye, EyeOff, GripVertical, Settings2 } from 'lucide-react'; import 'gridstack/dist/gridstack.css'; import { GRID_CELL_HEIGHT, GRID_MARGIN, minSizeOf } from '@/lib/dashboard-layout'; import { cn } from '@/lib/utils'; @@ -9,7 +10,8 @@ import type { UiLayoutItem } from '@/types'; export interface GridCoord { id: string; x: number; y: number; w: number; h: number } interface DashboardGridProps { - /** Visible blocks, each with x/y/w/h (already migrated/positioned). */ + /** Blocks to render — visible-only in view mode, ALL (incl. hidden) in edit + * mode so hidden widgets show as re-enableable ghosts. Each has x/y/w/h. */ blocks: UiLayoutItem[]; editing: boolean; /** Column count — 12 on desktop, 1 on narrow screens (auto single-column). */ @@ -17,6 +19,11 @@ interface DashboardGridProps { /** Fired (debounced upstream) after a drag/resize settles. */ onChange: (coords: GridCoord[]) => void; renderWidget: (block: UiLayoutItem) => ReactNode; + // ── edit overlay ── + labelFor: (id: string) => string; + configurableIds: ReadonlySet; + onToggleVisible: (id: string) => void; + onConfigOpen: (id: string) => void; } /** @@ -27,7 +34,10 @@ interface DashboardGridProps { * coord change (gridstack already moved the item; the coord round-trips back * through `onChange` → state and matches what gridstack has). */ -export function DashboardGrid({ blocks, editing, cols, onChange, renderWidget }: DashboardGridProps) { +export function DashboardGrid({ + blocks, editing, cols, onChange, renderWidget, + labelFor, configurableIds, onToggleVisible, onConfigOpen, +}: DashboardGridProps) { const elRef = useRef(null); const gridRef = useRef(null); const [contentEls, setContentEls] = useState>({}); @@ -53,7 +63,11 @@ export function DashboardGrid({ blocks, editing, cols, onChange, renderWidget }: float: true, // free placement — items stay where dropped (no auto-compact) disableDrag: !editing, disableResize: !editing, - handle: '.grid-stack-item-content', + // Default handle (.grid-stack-item-content) = the whole card is a drag + // target in edit mode — forgiving + discoverable. The card body is + // pointer-events-none in edit mode so widget controls don't fire, and + // the eye/gear are + )} + + + )} + {hidden ? ( +
+ {labelFor(b.id)} · 已隐藏 +
+ ) : ( +
+ {renderWidget(b)} +
+ )} + , + target, + ); + })} ); } diff --git a/packages/webui/src/components/pages/overview-layout-editor.tsx b/packages/webui/src/components/pages/overview-layout-editor.tsx deleted file mode 100644 index 5215d31c..00000000 --- a/packages/webui/src/components/pages/overview-layout-editor.tsx +++ /dev/null @@ -1,244 +0,0 @@ -import { useMemo, useState } from 'react'; -import { Reorder } from 'motion/react'; -import { Check, Eye, EyeOff, GripVertical, Lock, RotateCcw, Settings2 } from 'lucide-react'; -import { Button } from '@/components/ui/button'; -import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card'; -import { Input } from '@/components/ui/input'; -import { cn } from '@/lib/utils'; -import { - ALL_LOG_LEVELS, CONFIGURABLE_WIDGETS, parseAlertsConfig, parseSessionsConfig, -} from '@/lib/dashboard-layout'; -import type { UiLayoutItem } from '@/types'; - -type SortOpt = 'recent' | 'uin' | 'nickname'; -const SORT_LABELS: Record = { recent: '最近', uin: 'QQ 号', nickname: '昵称' }; - -// Edit panel for the overview's "编辑布局" mode. Overview-card POSITION/SIZE is -// edited in place on the gridstack grid below; this panel only toggles which -// cards are shown, and (re)orders the sidebar nav (a 1-D list, so it keeps the -// motion Reorder drag from phase 3). Pinned nav items can't be hidden. - -// ── widget visibility (no drag — grid owns position) ── - -function AlertsConfigForm({ config, onChange }: { config: Record | undefined; onChange: (c: Record) => void }) { - const c = parseAlertsConfig(config); - return ( -
- -
- 级别 -
- {ALL_LOG_LEVELS.map((lv) => { - const on = c.levels.includes(lv); - return ( - - ); - })} -
-
-
- ); -} - -function SessionsConfigForm({ config, onChange }: { config: Record | undefined; onChange: (c: Record) => void }) { - const c = parseSessionsConfig(config); - return ( -
-
- 排序 -
- {(['recent', 'uin', 'nickname'] as SortOpt[]).map((s) => ( - - ))} -
-
- -
- ); -} - -function VisibilityList({ - items, labelFor, onToggle, onConfig, -}: { - items: UiLayoutItem[]; - labelFor: (id: string) => string; - onToggle: (id: string) => void; - onConfig: (id: string, config: Record) => void; -}) { - const [open, setOpen] = useState(null); - return ( -
- {items.map((item) => { - const configurable = CONFIGURABLE_WIDGETS.has(item.id); - const isOpen = open === item.id; - return ( -
-
- {labelFor(item.id)} - {configurable && ( - - )} - -
- {configurable && isOpen && ( - item.id === 'alerts' - ? onConfig('alerts', c)} /> - : onConfig('sessions', c)} /> - )} -
- ); - })} -
- ); -} - -// ── nav reorder + visibility (1-D list, motion drag) ── - -function NavReorderList({ - items, labelFor, pinned, onChange, -}: { items: UiLayoutItem[]; labelFor: (id: string) => string; pinned: readonly string[]; onChange: (items: UiLayoutItem[]) => void }) { - const ids = useMemo(() => items.map((i) => i.id), [items]); - const byId = useMemo(() => new Map(items.map((i) => [i.id, i])), [items]); - - const reorder = (newIds: string[]) => - onChange(newIds.map((id) => byId.get(id)).filter((x): x is UiLayoutItem => !!x)); - const toggle = (id: string) => - onChange(items.map((i) => (i.id === id ? { ...i, visible: !i.visible } : i))); - - return ( - - {ids.map((id) => { - const item = byId.get(id); - if (!item) return null; - const isPinned = pinned.includes(id); - return ( - - - {labelFor(id)} - {isPinned ? ( - - - - ) : ( - - )} - - ); - })} - - ); -} - -export interface LayoutEditorProps { - blocks: UiLayoutItem[]; - blockLabelFor: (id: string) => string; - navItems: UiLayoutItem[]; - navLabelFor: (id: string) => string; - navPinned: readonly string[]; - onToggleBlock: (id: string) => void; - onBlockConfig: (id: string, config: Record) => void; - onNav: (items: UiLayoutItem[]) => void; - onReset: () => void; - onDone: () => void; -} - -export function LayoutEditor({ - blocks, blockLabelFor, navItems, navLabelFor, navPinned, onToggleBlock, onBlockConfig, onNav, onReset, onDone, -}: LayoutEditorProps) { - return ( - - -
- 编辑布局 - 在下方网格直接拖动卡片、拖角缩放;这里控制卡片显隐与导航。改动自动保存。 -
-
- - -
-
- -
-

总览卡片(显隐 / 设置)

- -
-
-

导航栏(拖动排序)

- -
-
-
- ); -} diff --git a/packages/webui/src/components/pages/overview-page.tsx b/packages/webui/src/components/pages/overview-page.tsx index a2c2c3ad..4c9d3f24 100644 --- a/packages/webui/src/components/pages/overview-page.tsx +++ b/packages/webui/src/components/pages/overview-page.tsx @@ -2,8 +2,8 @@ import { useEffect, useMemo, useState, type ReactNode } from 'react'; import { motion } from 'motion/react'; import { Link } from '@tanstack/react-router'; import { - Activity, ArrowRight, Bell, Cable, Cpu, MemoryStick, MonitorCog, Pencil, PlugZap, - RefreshCw, Server, Users, + Activity, ArrowRight, Bell, Cable, Check, Cpu, MemoryStick, MonitorCog, Pencil, PlugZap, + RefreshCw, RotateCcw, Server, Users, } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; @@ -18,22 +18,20 @@ import { useApi } from '@/lib/api'; import { useAppState } from '@/contexts/AppStateContext'; import { useSession } from '@/contexts/SessionContext'; import { useTheme } from '@/contexts/ThemeContext'; -import { reconcileLayoutItems, useLayout } from '@/contexts/LayoutContext'; +import { useLayout } from '@/contexts/LayoutContext'; import { useMediaQuery } from '@/hooks/use-media-query'; import { - GRID_COLS, parseAlertsConfig, parseSessionsConfig, widgetLabel, + CONFIGURABLE_WIDGETS, GRID_COLS, parseAlertsConfig, parseSessionsConfig, widgetLabel, type AlertsConfig, type SessionsConfig, } from '@/lib/dashboard-layout'; -import { NAV_ITEMS, PINNED_NAV } from '@/components/layout/sidebar'; +import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { DashboardGrid, type GridCoord } from '@/components/pages/dashboard-grid'; -import { LayoutEditor } from '@/components/pages/overview-layout-editor'; +import { AlertsConfigForm, SessionsConfigForm } from '@/components/pages/widget-config-forms'; function qqAvatarUrl(uin: string) { return `/avatar/${encodeURIComponent(uin)}`; } -const navLabelFor = (id: string) => NAV_ITEMS.find((n) => n.to === id)?.label ?? id; - function renderWidget(block: UiLayoutItem): ReactNode { if (block.id.startsWith('stat:')) return ; switch (block.id) { @@ -47,16 +45,15 @@ function renderWidget(block: UiLayoutItem): ReactNode { export function OverviewPage() { const { qqList, processList } = useAppState(); - const { overviewBlocks, navItems, setOverviewBlocks, setNavItems, resetLayout } = useLayout(); + const { overviewBlocks, setOverviewBlocks, resetLayout, editing: editingCtx, setEditing } = useLayout(); const isWide = useMediaQuery('(min-width: 768px)'); - const [editingOn, setEditingOn] = useState(false); - const editing = isWide && editingOn; + // Free-grid editing is desktop-only (no room to drag/resize on a phone). + const editing = isWide && editingCtx; + const [configId, setConfigId] = useState(null); const visibleBlocks = useMemo(() => overviewBlocks.filter((b) => b.visible), [overviewBlocks]); - const nav = useMemo( - () => reconcileLayoutItems(navItems, NAV_ITEMS.map((i) => i.to), PINNED_NAV), - [navItems], - ); + // In edit mode show ALL blocks (hidden ones as re-enableable ghosts); else visible-only. + const gridBlocks = editing ? overviewBlocks : visibleBlocks; const onGridChange = (coords: GridCoord[]) => { const cmap = new Map(coords.map((c) => [c.id, c])); @@ -75,31 +72,30 @@ export function OverviewPage() { setOverviewBlocks(overviewBlocks.map((b) => (b.id === id ? { ...b, config: { ...b.config, ...config } } : b))); const loadableProcs = processList.filter((p) => !p.injected).length; + const configBlock = configId ? overviewBlocks.find((b) => b.id === configId) : null; return (
- {/* Toolbar: edit toggle is desktop-only (free grid needs room to drag). */} - {isWide && !editing && ( -
- + )} +
)} {editing && ( - setEditingOn(false)} - /> +

+ 拖动卡片顶部的「拖动」手柄移动位置、拖右下角缩放;眼睛图标显隐、齿轮改设置。左侧导航也可在编辑态拖动排序。 +

)} {/* First-run nudge — always shown (not a grid widget) so it can't be hidden. */} @@ -125,17 +121,31 @@ export function OverviewPage() { )} - {visibleBlocks.length === 0 ? ( + {visibleBlocks.length === 0 && !editing ? ( ) : ( )} + + { if (!o) setConfigId(null); }}> + + + {configBlock ? `${widgetLabel(configBlock.id)} · 设置` : '设置'} + + {configBlock?.id === 'alerts' && setBlockConfig('alerts', c)} />} + {configBlock?.id === 'sessions' && setBlockConfig('sessions', c)} />} + +
); } @@ -163,19 +173,19 @@ function StatTile({ to?: AppPath; }) { const body = ( - +
{icon}
-

{label}

-
{value}
- {subtext &&

{subtext}

} +

{label}

+
{value}
+ {subtext &&

{subtext}

}
{to && }
diff --git a/packages/webui/src/components/pages/widget-config-forms.tsx b/packages/webui/src/components/pages/widget-config-forms.tsx new file mode 100644 index 00000000..bc53d222 --- /dev/null +++ b/packages/webui/src/components/pages/widget-config-forms.tsx @@ -0,0 +1,85 @@ +import { Input } from '@/components/ui/input'; +import { cn } from '@/lib/utils'; +import { ALL_LOG_LEVELS, parseAlertsConfig, parseSessionsConfig } from '@/lib/dashboard-layout'; + +// Per-widget config forms, shown inside a dialog opened from a dashboard +// widget's edit-overlay gear. `config` is the widget's opaque block.config; +// `onChange` merges a partial back via setBlockConfig. + +type SortOpt = 'recent' | 'uin' | 'nickname'; +const SORT_LABELS: Record = { recent: '最近', uin: 'QQ 号', nickname: '昵称' }; + +interface FormProps { + config: Record | undefined; + onChange: (c: Record) => void; +} + +export function AlertsConfigForm({ config, onChange }: FormProps) { + const c = parseAlertsConfig(config); + return ( +
+ +
+ 显示级别 +
+ {ALL_LOG_LEVELS.map((lv) => { + const on = c.levels.includes(lv); + return ( + + ); + })} +
+
+
+ ); +} + +export function SessionsConfigForm({ config, onChange }: FormProps) { + const c = parseSessionsConfig(config); + return ( +
+
+ 排序 +
+ {(['recent', 'uin', 'nickname'] as SortOpt[]).map((s) => ( + + ))} +
+
+ +
+ ); +} diff --git a/packages/webui/src/contexts/LayoutContext.tsx b/packages/webui/src/contexts/LayoutContext.tsx index aca4dbfc..57a07fe0 100644 --- a/packages/webui/src/contexts/LayoutContext.tsx +++ b/packages/webui/src/contexts/LayoutContext.tsx @@ -79,6 +79,10 @@ interface LayoutContextValue { setPages: (patch: Partial) => void; /** True once the authed config load has resolved (gates the default-route redirect). */ ready: boolean; + /** Shared "编辑布局" mode — the dashboard grid AND the sidebar read this so both + * show in-place edit affordances from a single toggle. */ + editing: boolean; + setEditing: (v: boolean) => void; } const LayoutContext = createContext(null); @@ -88,6 +92,7 @@ export function LayoutProvider({ children }: { children: ReactNode }) { const [layout, setLayout] = useState(DEFAULT_LAYOUT); const [pages, setPagesState] = useState(DEFAULT_PAGES); const [ready, setReady] = useState(false); + const [editing, setEditing] = useState(false); const layoutRef = useRef(layout); const pagesRef = useRef(pages); // Set once the user edits, so the initial GET (which may resolve AFTER the @@ -186,7 +191,9 @@ export function LayoutProvider({ children }: { children: ReactNode }) { pages, setPages, ready, - }), [layout, setOverviewBlocks, setNavItems, resetLayout, pages, setPages, ready]); + editing, + setEditing, + }), [layout, setOverviewBlocks, setNavItems, resetLayout, pages, setPages, ready, editing]); return {children}; } diff --git a/packages/webui/src/index.css b/packages/webui/src/index.css index f1d4f1f8..7559510c 100644 --- a/packages/webui/src/index.css +++ b/packages/webui/src/index.css @@ -238,12 +238,21 @@ html[data-reduce-motion="1"] *::after { background: color-mix(in oklab, var(--primary) 8%, transparent); border-radius: var(--radius); } -/* Resize handle — only meaningful while editing (grid is interactive). */ -.grid-stack-item > .ui-resizable-handle { - filter: none; -} +/* Resize handle — only present while editing (grid is interactive). Make the + bottom-right grip obvious so it's clearly draggable. */ .grid-stack-item > .ui-resizable-se { - /* Make the bottom-right resize grip visible against either theme. */ - border-right: 2px solid color-mix(in oklab, var(--primary) 70%, transparent); - border-bottom: 2px solid color-mix(in oklab, var(--primary) 70%, transparent); + width: 18px; + height: 18px; + right: 5px; + bottom: 5px; + background-image: none; + transform: none; + border-right: 3px solid var(--primary); + border-bottom: 3px solid var(--primary); + border-bottom-right-radius: 5px; + opacity: 0.65; + z-index: 30; +} +.grid-stack-item:hover > .ui-resizable-se { + opacity: 1; }