diff --git a/packages/core/src/webui/ui-config.ts b/packages/core/src/webui/ui-config.ts index 63a03e37..c646f12d 100644 --- a/packages/core/src/webui/ui-config.ts +++ b/packages/core/src/webui/ui-config.ts @@ -102,6 +102,12 @@ export interface UiLayoutItem { y?: number; w?: number; h?: number; + /** + * Per-widget settings (overview blocks only) — an opaque object the client + * interprets by widget type (e.g. alerts: count + levels). The server only + * checks it's a plain object within a size bound; it never reads the keys. + */ + config?: Record; } export interface UiLayout { @@ -245,6 +251,13 @@ function normalizeLayoutItems(value: unknown, fallback: UiLayoutItem[]): UiLayou item.w = clampInt(raw.w, 1, 12, 1); item.h = clampInt(raw.h, 1, 100, 1); } + // Opaque per-widget config: keep only a plain object within a size bound; + // the server never interprets the keys (the client does, per widget type). + if (isObject(raw.config)) { + try { + if (JSON.stringify(raw.config).length <= 4096) item.config = raw.config; + } catch { /* unserializable (cycles) → drop */ } + } out.push(item); } // An array that parsed to nothing usable is indistinguishable from corrupt; diff --git a/packages/webui/src/components/pages/dashboard-grid.tsx b/packages/webui/src/components/pages/dashboard-grid.tsx index f0b31596..b6282d44 100644 --- a/packages/webui/src/components/pages/dashboard-grid.tsx +++ b/packages/webui/src/components/pages/dashboard-grid.tsx @@ -16,7 +16,7 @@ interface DashboardGridProps { cols: number; /** Fired (debounced upstream) after a drag/resize settles. */ onChange: (coords: GridCoord[]) => void; - renderWidget: (id: string) => ReactNode; + renderWidget: (block: UiLayoutItem) => ReactNode; } /** @@ -98,7 +98,7 @@ export function DashboardGrid({ blocks, editing, cols, onChange, renderWidget }: contentEls[b.id] ? createPortal(
- {renderWidget(b.id)} + {renderWidget(b)}
, contentEls[b.id], ) diff --git a/packages/webui/src/components/pages/overview-layout-editor.tsx b/packages/webui/src/components/pages/overview-layout-editor.tsx index f3a60cfa..5215d31c 100644 --- a/packages/webui/src/components/pages/overview-layout-editor.tsx +++ b/packages/webui/src/components/pages/overview-layout-editor.tsx @@ -1,11 +1,18 @@ -import { useMemo } from 'react'; +import { useMemo, useState } from 'react'; import { Reorder } from 'motion/react'; -import { Check, Eye, EyeOff, GripVertical, Lock, RotateCcw } from 'lucide-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 @@ -13,31 +20,126 @@ import type { UiLayoutItem } from '@/types'; // ── 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, -}: { items: UiLayoutItem[]; labelFor: (id: string) => string; onToggle: (id: string) => void }) { + 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) => ( -
- {labelFor(item.id)} - -
- ))} + {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)} /> + )} +
+ ); + })}
); } @@ -102,13 +204,14 @@ export interface LayoutEditorProps { 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, onNav, onReset, onDone, + blocks, blockLabelFor, navItems, navLabelFor, navPinned, onToggleBlock, onBlockConfig, onNav, onReset, onDone, }: LayoutEditorProps) { return ( @@ -128,8 +231,8 @@ export function LayoutEditor({
-

总览卡片(显隐)

- +

总览卡片(显隐 / 设置)

+

导航栏(拖动排序)

diff --git a/packages/webui/src/components/pages/overview-page.tsx b/packages/webui/src/components/pages/overview-page.tsx index 8042a724..a2c2c3ad 100644 --- a/packages/webui/src/components/pages/overview-page.tsx +++ b/packages/webui/src/components/pages/overview-page.tsx @@ -13,14 +13,17 @@ import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; import { Skeleton } from '@/components/ui/skeleton'; import { cn, formatBytes, formatUptime } from '@/lib/utils'; import type { AppPath } from '@/router'; -import type { AdapterStatus, AdapterStatusLevel, LogEntry } from '@/types'; +import type { AdapterStatus, AdapterStatusLevel, LogEntry, LogLevel, UiLayoutItem } from '@/types'; 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 { useMediaQuery } from '@/hooks/use-media-query'; -import { GRID_COLS, widgetLabel } from '@/lib/dashboard-layout'; +import { + GRID_COLS, parseAlertsConfig, parseSessionsConfig, widgetLabel, + type AlertsConfig, type SessionsConfig, +} from '@/lib/dashboard-layout'; import { NAV_ITEMS, PINNED_NAV } from '@/components/layout/sidebar'; import { DashboardGrid, type GridCoord } from '@/components/pages/dashboard-grid'; import { LayoutEditor } from '@/components/pages/overview-layout-editor'; @@ -31,13 +34,13 @@ function qqAvatarUrl(uin: string) { const navLabelFor = (id: string) => NAV_ITEMS.find((n) => n.to === id)?.label ?? id; -function renderWidget(id: string): ReactNode { - if (id.startsWith('stat:')) return ; - switch (id) { +function renderWidget(block: UiLayoutItem): ReactNode { + if (block.id.startsWith('stat:')) return ; + switch (block.id) { case 'connections': return ; - case 'alerts': return ; + case 'alerts': return ; case 'host': return ; - case 'sessions': return ; + case 'sessions': return ; default: return null; } } @@ -68,6 +71,9 @@ export function OverviewPage() { const toggleBlock = (id: string) => setOverviewBlocks(overviewBlocks.map((b) => (b.id === id ? { ...b, visible: !b.visible } : b))); + const setBlockConfig = (id: string, config: Record) => + setOverviewBlocks(overviewBlocks.map((b) => (b.id === id ? { ...b, config: { ...b.config, ...config } } : b))); + const loadableProcs = processList.filter((p) => !p.injected).length; return ( @@ -89,6 +95,7 @@ export function OverviewPage() { navLabelFor={navLabelFor} navPinned={PINNED_NAV} onToggleBlock={toggleBlock} + onBlockConfig={setBlockConfig} onNav={setNavItems} onReset={resetLayout} onDone={() => setEditingOn(false)} @@ -348,24 +355,37 @@ function HostBlock() { // ─────────────── online sessions ─────────────── -function SessionsBlock() { +function SessionsBlock({ config }: { config: SessionsConfig }) { const { qqList } = useAppState(); + const list = useMemo(() => { + const f = config.filter.trim().toLowerCase(); + let arr = f + ? qqList.filter((q) => (q.nickname ?? '').toLowerCase().includes(f) || q.uin.includes(f)) + : qqList; + if (config.sort === 'uin') arr = [...arr].sort((a, b) => a.uin.localeCompare(b.uin, undefined, { numeric: true })); + else if (config.sort === 'nickname') arr = [...arr].sort((a, b) => (a.nickname ?? '').localeCompare(b.nickname ?? '')); + // 'recent' keeps the server/insertion order. + return arr; + }, [qqList, config.sort, config.filter]); + return ( 在线会话 - 当前已接入并完成登录的 QQ 账号 + + 当前已接入并完成登录的 QQ 账号{config.filter.trim() ? `(筛选:${config.filter.trim()})` : ''} + - {qqList.length === 0 ? ( + {list.length === 0 ? (
-

暂无在线会话

+

{qqList.length === 0 ? '暂无在线会话' : '无匹配的会话'}

) : (
- {qqList.map((q, idx) => ( + {list.map((q, idx) => ( = { + trace: 'text-muted-foreground/60', + debug: 'text-muted-foreground', + info: 'text-primary', + success: 'text-success', + warn: 'text-warning', + error: 'text-destructive', +}; + +function RecentAlertsCard({ config }: { config: AlertsConfig }) { const api = useApi(); const { formatClock } = useTheme(); const [alerts, setAlerts] = useState([]); + const { count } = config; + const levelsKey = config.levels.join(','); useEffect(() => { + const levels = new Set(levelsKey.split(',') as LogLevel[]); let active = true; api.logs - .list(200) + .list(Math.max(200, count * 4)) .then((list) => { if (!active) return; - setAlerts(list.filter((l) => l.level === 'warn' || l.level === 'error').slice(-5)); + setAlerts(list.filter((l) => levels.has(l.level)).slice(-count)); }) .catch(() => { /* ignore */ }); const stop = api.logs.stream({ onLine: (entry) => { - if (entry.level !== 'warn' && entry.level !== 'error') return; - setAlerts((prev) => [...prev.filter((a) => a.id !== entry.id), entry].slice(-5)); + if (!levels.has(entry.level)) return; + setAlerts((prev) => [...prev.filter((a) => a.id !== entry.id), entry].slice(-count)); }, }); return () => { active = false; stop(); }; - }, [api]); + }, [api, count, levelsKey]); return ( @@ -493,7 +525,7 @@ function RecentAlertsCard() { 最近告警 - 最近的 warn / error 级别日志 + 最近 {count} 条 · {config.levels.map((l) => l.toUpperCase()).join(' / ')}
查看日志 @@ -510,7 +542,7 @@ function RecentAlertsCard() { {alerts.map((a) => (
{formatClock(a.time)} - + {a.level.toUpperCase()} [{a.scope}] {a.message} diff --git a/packages/webui/src/lib/dashboard-layout.ts b/packages/webui/src/lib/dashboard-layout.ts index c7c78213..b0adb903 100644 --- a/packages/webui/src/lib/dashboard-layout.ts +++ b/packages/webui/src/lib/dashboard-layout.ts @@ -1,4 +1,4 @@ -import type { UiLayoutItem } from '@/types'; +import type { LogLevel, 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 @@ -97,3 +97,36 @@ 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 }; } + +// ─── Per-widget config (interpreted client-side from the opaque block.config) ─── + +export const ALL_LOG_LEVELS: LogLevel[] = ['trace', 'debug', 'info', 'success', 'warn', 'error']; + +export interface AlertsConfig { count: number; levels: LogLevel[] } +export interface SessionsConfig { sort: 'recent' | 'uin' | 'nickname'; filter: string } + +export const DEFAULT_ALERTS_CONFIG: AlertsConfig = { count: 5, levels: ['warn', 'error'] }; +export const DEFAULT_SESSIONS_CONFIG: SessionsConfig = { sort: 'recent', filter: '' }; + +/** Widgets that expose a config form (gear) in the layout editor. */ +export const CONFIGURABLE_WIDGETS = new Set(['alerts', 'sessions']); + +export function parseAlertsConfig(config: Record | undefined): AlertsConfig { + const c = config ?? {}; + const count = typeof c.count === 'number' && Number.isFinite(c.count) + ? Math.min(50, Math.max(1, Math.trunc(c.count))) + : DEFAULT_ALERTS_CONFIG.count; + const levels = Array.isArray(c.levels) + ? ALL_LOG_LEVELS.filter((l) => (c.levels as unknown[]).includes(l)) + : DEFAULT_ALERTS_CONFIG.levels; + return { count, levels: levels.length > 0 ? levels : DEFAULT_ALERTS_CONFIG.levels }; +} + +export function parseSessionsConfig(config: Record | undefined): SessionsConfig { + const c = config ?? {}; + const sort = c.sort === 'uin' || c.sort === 'nickname' || c.sort === 'recent' + ? c.sort + : DEFAULT_SESSIONS_CONFIG.sort; + const filter = typeof c.filter === 'string' ? c.filter.slice(0, 100) : ''; + return { sort, filter }; +} diff --git a/packages/webui/src/types.ts b/packages/webui/src/types.ts index d92ab66f..36321c67 100644 --- a/packages/webui/src/types.ts +++ b/packages/webui/src/types.ts @@ -214,6 +214,8 @@ export interface UiLayoutItem { y?: number; w?: number; h?: number; + /** Per-widget settings; interpreted client-side by widget type. */ + config?: Record; } export interface UiLayout {