diff --git a/packages/webui/src/components/pages/overview-page.tsx b/packages/webui/src/components/pages/overview-page.tsx index daea3c8d..c0c66b5c 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, Reorder } from 'motion/react'; import { Link } from '@tanstack/react-router'; import { - Activity, ArrowRight, Bell, Cable, Check, Cpu, Eye, EyeOff, GripVertical, MemoryStick, - MonitorCog, Pencil, PlugZap, RefreshCw, RotateCcw, Server, Settings2, Users, + Activity, ArrowRight, Bell, Cable, Check, Cpu, ExternalLink, Eye, EyeOff, GripVertical, + MemoryStick, MonitorCog, Pencil, PlugZap, RefreshCw, RotateCcw, Server, Settings2, Users, } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; @@ -21,14 +21,17 @@ import { useTheme } from '@/contexts/ThemeContext'; import { reconcileLayoutItems, useLayout } from '@/contexts/LayoutContext'; import { useMediaQuery } from '@/hooks/use-media-query'; import { - CONFIGURABLE_WIDGETS, GRID_COLS, MOBILE_WIDGET_IDS, mobileHeightOf, - parseAlertsConfig, parseConnectionsConfig, parseHostConfig, parseSessionsConfig, widgetLabel, - type AlertsConfig, type ConnectionsConfig, type HostConfig, type SessionsConfig, + CONFIGURABLE_WIDGETS, GRID_COLS, MOBILE_WIDGET_IDS, STATIC_WIDGET_IDS, mobileHeightOf, + parseAccountConfig, parseAlertsConfig, parseConnectionsConfig, parseHostConfig, + parseLinkConfig, parseNoteConfig, parseSessionsConfig, widgetLabel, + type AccountConfig, type AlertsConfig, type ConnectionsConfig, type HostConfig, + type LinkConfig, type NoteConfig, type SessionsConfig, } from '@/lib/dashboard-layout'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { DashboardGrid, type GridCoord } from '@/components/pages/dashboard-grid'; import { - AlertsConfigForm, ConnectionsConfigForm, HostConfigForm, SessionsConfigForm, + AccountConfigForm, AlertsConfigForm, ConnectionsConfigForm, HostConfigForm, + LinkConfigForm, LINK_ICON_COMPONENTS, NoteConfigForm, SessionsConfigForm, } from '@/components/pages/widget-config-forms'; function qqAvatarUrl(uin: string) { @@ -42,6 +45,9 @@ function renderWidget(block: UiLayoutItem): ReactNode { case 'alerts': return ; case 'host': return ; case 'sessions': return ; + case 'note': return ; + case 'link': return ; + case 'account': return ; default: return null; } } @@ -71,7 +77,7 @@ export function OverviewPage() { // Reconcile the mobile order against the live widget catalogue (drop unknown, // append new as visible) — so a widget added later just appears on phones too. const mobileItems = useMemo( - () => reconcileLayoutItems(overviewMobile, MOBILE_WIDGET_IDS), + () => reconcileLayoutItems(overviewMobile, MOBILE_WIDGET_IDS, [], STATIC_WIDGET_IDS), [overviewMobile], ); @@ -184,6 +190,9 @@ export function OverviewPage() { {configBlock?.id === 'sessions' && setBlockConfig('sessions', c)} />} {configBlock?.id === 'host' && setBlockConfig('host', c)} />} {configBlock?.id === 'connections' && setBlockConfig('connections', c)} />} + {configBlock?.id === 'note' && setBlockConfig('note', c)} />} + {configBlock?.id === 'link' && setBlockConfig('link', c)} />} + {configBlock?.id === 'account' && setBlockConfig('account', c)} />} @@ -652,6 +661,85 @@ function ConnectionsBlock({ config }: { config: ConnectionsConfig }) { ); } +// ─────────────── static widgets (note / link / account) ─────────────── + +function NoteWidget({ config }: { config: NoteConfig }) { + return ( + + + {config.text.trim() ? ( + {config.text} + ) : ( + 空便签 · 在编辑模式点齿轮填写内容 + )} + + + ); +} + +function LinkWidget({ config }: { config: LinkConfig }) { + const Icon = LINK_ICON_COMPONENTS[config.icon] ?? LINK_ICON_COMPONENTS.link; + if (!config.url) { + return ( + + + + 未配置链接 · 点齿轮设置 + + + ); + } + return ( + + + + + + + + {config.label || config.url} + {config.url} + + + + + + ); +} + +function AccountWidget({ config }: { config: AccountConfig }) { + const { qqList } = useAppState(); + const acct = qqList.find((q) => q.uin === config.uin); + if (!config.uin) { + return ( + + + + 未指定账号 · 点齿轮设置 + + + ); + } + return ( + + + + + {(acct?.nickname || config.uin).slice(0, 2)} + + + {acct?.nickname || '未接入账号'} + {config.uin} + + + + + ); +} + // ─────────────── recent alerts ─────────────── const ALERT_LEVEL_CLASS: Record = { diff --git a/packages/webui/src/components/pages/widget-config-forms.tsx b/packages/webui/src/components/pages/widget-config-forms.tsx index 719bf4b0..5cacae40 100644 --- a/packages/webui/src/components/pages/widget-config-forms.tsx +++ b/packages/webui/src/components/pages/widget-config-forms.tsx @@ -1,10 +1,21 @@ +import { useId } from 'react'; +import { Bell, BookOpen, ExternalLink, Github, Globe, Link2, Server, Star } from 'lucide-react'; import { Input } from '@/components/ui/input'; import { ToggleSwitch } from '@/components/ui/toggle-switch'; import { cn } from '@/lib/utils'; +import { useAppState } from '@/contexts/AppStateContext'; import { - ALL_LOG_LEVELS, parseAlertsConfig, parseConnectionsConfig, parseHostConfig, parseSessionsConfig, + ALL_LOG_LEVELS, LINK_ICON_IDS, NOTE_MAX, + parseAlertsConfig, parseAccountConfig, parseConnectionsConfig, parseHostConfig, + parseLinkConfig, parseNoteConfig, parseSessionsConfig, } from '@/lib/dashboard-layout'; +/** Curated link-card icons, shared by the picker form and the widget renderer. */ +export const LINK_ICON_COMPONENTS: Record = { + link: Link2, external: ExternalLink, github: Github, book: BookOpen, + server: Server, globe: Globe, star: Star, bell: Bell, +}; + // 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. @@ -155,3 +166,99 @@ export function ConnectionsConfigForm({ config, onChange }: FormProps) { ); } + +export function NoteConfigForm({ config, onChange }: FormProps) { + const c = parseNoteConfig(config); + return ( + + 便签内容(纯文本,保留换行) + onChange({ text: e.target.value.slice(0, NOTE_MAX) })} + maxLength={NOTE_MAX} + spellCheck={false} + placeholder="写点备注、待办或快捷信息…" + className="h-40 w-full resize-y rounded-lg border bg-card/40 p-3 text-sm leading-relaxed outline-none focus:border-primary" + /> + {c.text.length} / {NOTE_MAX} + + ); +} + +export function LinkConfigForm({ config, onChange }: FormProps) { + const c = parseLinkConfig(config); + const urlInvalid = c_urlRaw(config) !== '' && !c.url; + return ( + + + 标题 + onChange({ label: e.target.value })} className="h-8" /> + + + 链接(http/https) + onChange({ url: e.target.value })} + className={cn('h-8', urlInvalid && 'border-destructive')} + /> + {urlInvalid && 仅支持 http:// 或 https:// 链接} + + + 图标 + + {LINK_ICON_IDS.map((ic) => { + const Icon = LINK_ICON_COMPONENTS[ic]; + const on = c.icon === ic; + return ( + onChange({ icon: ic })} + aria-label={ic} + className={cn( + 'inline-flex size-8 items-center justify-center rounded-md border transition-colors cursor-pointer', + on ? 'border-primary bg-primary/10 text-primary' : 'border-border text-muted-foreground hover:bg-accent/40', + )} + > + + + ); + })} + + + + ); +} + +// The raw (unvalidated) url so the field stays editable while typing an +// incomplete URL; parseLinkConfig only blesses it once it's http(s). +function c_urlRaw(config: Record | undefined): string { + const v = config?.url; + return typeof v === 'string' ? v.slice(0, 2048) : ''; +} + +export function AccountConfigForm({ config, onChange }: FormProps) { + const c = parseAccountConfig(config); + const { qqList } = useAppState(); + const listId = useId(); + return ( + + QQ 号 + onChange({ uin: e.target.value.replace(/\D/g, '').slice(0, 15) })} + className="h-8" + /> + + {qqList.map((q) => {q.nickname || q.uin})} + + 可从已接入账号中选择,或手动输入。 + + ); +} diff --git a/packages/webui/src/contexts/LayoutContext.tsx b/packages/webui/src/contexts/LayoutContext.tsx index 52628dba..6ab9d979 100644 --- a/packages/webui/src/contexts/LayoutContext.tsx +++ b/packages/webui/src/contexts/LayoutContext.tsx @@ -50,12 +50,14 @@ export const DEFAULT_PAGES: UiPages = defaultPages(); /** * Order + visibility for a known catalogue: keep stored items that still exist * (in their stored order + visibility), then append any catalogue entries the - * stored layout predates (visible). `pinned` ids are forced visible. + * stored layout predates. `pinned` ids are forced visible; ids in + * `hiddenByDefault` are appended hidden (opt-in widgets), otherwise visible. */ export function reconcileLayoutItems( stored: UiLayoutItem[] | undefined, known: readonly string[], pinned: readonly string[] = [], + hiddenByDefault: ReadonlySet = new Set(), ): UiLayoutItem[] { const knownSet = new Set(known); const seen = new Set(); @@ -66,7 +68,7 @@ export function reconcileLayoutItems( out.push({ id: item.id, visible: pinned.includes(item.id) ? true : item.visible !== false }); } for (const id of known) { - if (!seen.has(id)) out.push({ id, visible: true }); + if (!seen.has(id)) out.push({ id, visible: !hiddenByDefault.has(id) }); } return out; } diff --git a/packages/webui/src/lib/dashboard-layout.ts b/packages/webui/src/lib/dashboard-layout.ts index 6ac574bd..479f04df 100644 --- a/packages/webui/src/lib/dashboard-layout.ts +++ b/packages/webui/src/lib/dashboard-layout.ts @@ -21,6 +21,9 @@ export interface GridWidgetSpec { minH: number; /** Default placement when the operator hasn't positioned it yet. */ def: { x: number; y: number; w: number; h: number }; + /** Whether the widget is shown by default. Static widgets (note/link/account) + * start hidden — they're opt-in and need configuring first. Defaults true. */ + defaultVisible?: boolean; } /** The five top stat tiles, split out of the legacy single `stats` block. */ @@ -38,8 +41,15 @@ export const GRID_WIDGETS: GridWidgetSpec[] = [ { 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 } }, + // Static, opt-in widgets — content lives in block.config, hidden by default. + { id: 'note', label: '便签', minW: 2, minH: 2, def: { x: 0, y: 14, w: 4, h: 2 }, defaultVisible: false }, + { id: 'link', label: '链接卡', minW: 2, minH: 1, def: { x: 4, y: 14, w: 4, h: 1 }, defaultVisible: false }, + { id: 'account', label: '账号快捷卡', minW: 2, minH: 1, def: { x: 8, y: 14, w: 4, h: 1 }, defaultVisible: false }, ]; +/** The opt-in static widgets — appended hidden when reconciling forward. */ +export const STATIC_WIDGET_IDS: ReadonlySet = new Set(['note', 'link', 'account']); + const WIDGET_BY_ID = new Map(GRID_WIDGETS.map((w) => [w.id, w])); export function widgetLabel(id: string): string { @@ -58,7 +68,7 @@ function validCoords(item: UiLayoutItem | undefined): boolean { /** A fully-positioned overview block (coords always present). */ export function defaultOverviewGrid(): UiLayoutItem[] { - return GRID_WIDGETS.map((w) => ({ id: w.id, visible: true, ...w.def })); + return GRID_WIDGETS.map((w) => ({ id: w.id, visible: w.defaultVisible !== false, ...w.def })); } /** The widget ids in single-column (mobile) default order — same catalogue as @@ -67,7 +77,7 @@ export const MOBILE_WIDGET_IDS: readonly string[] = GRID_WIDGETS.map((w) => w.id /** Default single-column mobile overview (id+visible, no coords). */ export function defaultOverviewMobile(): UiLayoutItem[] { - return GRID_WIDGETS.map((w) => ({ id: w.id, visible: true })); + return GRID_WIDGETS.map((w) => ({ id: w.id, visible: w.defaultVisible !== false })); } /** @@ -97,7 +107,7 @@ export function migrateOverviewBlocks(stored: UiLayoutItem[] | undefined): UiLay ? item.visible !== false : isStatTile && legacyStats ? legacyStats.visible !== false - : true; + : spec.defaultVisible !== false; const coords = validCoords(item) ? { x: item!.x!, y: item!.y!, w: item!.w!, h: item!.h! } : { ...spec.def }; @@ -133,8 +143,41 @@ export const DEFAULT_SESSIONS_CONFIG: SessionsConfig = { sort: 'recent', filter: export const DEFAULT_HOST_CONFIG: HostConfig = { cpu: true, memory: true, runtime: true }; export const DEFAULT_CONNECTIONS_CONFIG: ConnectionsConfig = { filter: '', onlyIssues: false, sort: 'default' }; +/** Static-widget content (stored in block.config). */ +export interface NoteConfig { text: string } +export interface LinkConfig { label: string; url: string; icon: string } +export interface AccountConfig { uin: string } + +/** Allowed link-card icon ids (a curated lucide subset). */ +export const LINK_ICON_IDS = ['link', 'external', 'github', 'book', 'server', 'globe', 'star', 'bell'] as const; +/** Max note length — kept under the server's 4096-byte block.config JSON cap. */ +export const NOTE_MAX = 4000; + /** Widgets that expose a config form (gear) in the layout editor. */ -export const CONFIGURABLE_WIDGETS = new Set(['alerts', 'sessions', 'host', 'connections']); +export const CONFIGURABLE_WIDGETS = new Set([ + 'alerts', 'sessions', 'host', 'connections', 'note', 'link', 'account', +]); + +export function parseNoteConfig(config: Record | undefined): NoteConfig { + const c = config ?? {}; + return { text: typeof c.text === 'string' ? c.text.slice(0, NOTE_MAX) : '' }; +} + +// Only http(s) — never javascript:/data: (the url goes into an ). +const SAFE_URL_RE = /^https?:\/\//i; +export function parseLinkConfig(config: Record | undefined): LinkConfig { + const c = config ?? {}; + const label = typeof c.label === 'string' ? c.label.slice(0, 60) : ''; + const url = typeof c.url === 'string' && SAFE_URL_RE.test(c.url.trim()) ? c.url.trim().slice(0, 2048) : ''; + const icon = typeof c.icon === 'string' && (LINK_ICON_IDS as readonly string[]).includes(c.icon) ? c.icon : 'link'; + return { label, url, icon }; +} + +export function parseAccountConfig(config: Record | undefined): AccountConfig { + const c = config ?? {}; + const uin = typeof c.uin === 'string' ? c.uin.replace(/\D/g, '').slice(0, 15) : ''; + return { uin }; +} export function parseAlertsConfig(config: Record | undefined): AlertsConfig { const c = config ?? {};
{config.text}
空便签 · 在编辑模式点齿轮填写内容
{config.label || config.url}
{config.url}
{acct?.nickname || '未接入账号'}
{config.uin}