feat(webui): static note / link / account widgets (B2)
Three opt-in static widgets join the GRID_WIDGETS catalogue (hidden by default — enabled from the layout editor), with content stored in block.config: - 便签 (note): pre-wrap plain text, capped at 4000 chars (under the server's 4096-byte config cap). No Markdown. - 链接卡 (link): label + http(s)-only url (javascript:/data: rejected) + an icon from a curated lucide set; renders as a clickable card. - 账号快捷卡 (account): a specified uin's avatar/nickname/online dot, with a datalist of connected accounts in the config form. GridWidgetSpec gains `defaultVisible`, honored by defaultOverviewGrid/Mobile and migrateOverviewBlocks; reconcileLayoutItems gains a `hiddenByDefault` set so the mobile catalogue appends these hidden too. parse* validators keep config total over corrupt input.
This commit is contained in:
4 files changed
+254
-14
No files matched your search
@@ -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 <RecentAlertsCard config={parseAlertsConfig(block.config)} />;
|
||||
case 'host': return <HostBlock config={parseHostConfig(block.config)} />;
|
||||
case 'sessions': return <SessionsBlock config={parseSessionsConfig(block.config)} />;
|
||||
case 'note': return <NoteWidget config={parseNoteConfig(block.config)} />;
|
||||
case 'link': return <LinkWidget config={parseLinkConfig(block.config)} />;
|
||||
case 'account': return <AccountWidget config={parseAccountConfig(block.config)} />;
|
||||
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' && <SessionsConfigForm config={configBlock.config} onChange={(c) => setBlockConfig('sessions', c)} />}
|
||||
{configBlock?.id === 'host' && <HostConfigForm config={configBlock.config} onChange={(c) => setBlockConfig('host', c)} />}
|
||||
{configBlock?.id === 'connections' && <ConnectionsConfigForm config={configBlock.config} onChange={(c) => setBlockConfig('connections', c)} />}
|
||||
{configBlock?.id === 'note' && <NoteConfigForm config={configBlock.config} onChange={(c) => setBlockConfig('note', c)} />}
|
||||
{configBlock?.id === 'link' && <LinkConfigForm config={configBlock.config} onChange={(c) => setBlockConfig('link', c)} />}
|
||||
{configBlock?.id === 'account' && <AccountConfigForm config={configBlock.config} onChange={(c) => setBlockConfig('account', c)} />}
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</div>
|
||||
@@ -652,6 +661,85 @@ function ConnectionsBlock({ config }: { config: ConnectionsConfig }) {
|
||||
);
|
||||
}
|
||||
|
||||
// ─────────────── static widgets (note / link / account) ───────────────
|
||||
|
||||
function NoteWidget({ config }: { config: NoteConfig }) {
|
||||
return (
|
||||
<Card className="h-full overflow-hidden">
|
||||
<CardContent className="h-full overflow-auto p-4">
|
||||
{config.text.trim() ? (
|
||||
<p className="whitespace-pre-wrap break-words text-sm leading-relaxed">{config.text}</p>
|
||||
) : (
|
||||
<p className="text-sm text-muted-foreground">空便签 · 在编辑模式点齿轮填写内容</p>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
function LinkWidget({ config }: { config: LinkConfig }) {
|
||||
const Icon = LINK_ICON_COMPONENTS[config.icon] ?? LINK_ICON_COMPONENTS.link;
|
||||
if (!config.url) {
|
||||
return (
|
||||
<Card className="h-full overflow-hidden">
|
||||
<CardContent className="flex h-full items-center gap-3 p-4 text-muted-foreground">
|
||||
<Icon className="size-5 shrink-0" />
|
||||
<span className="text-sm">未配置链接 · 点齿轮设置</span>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<a href={config.url} target="_blank" rel="noreferrer noopener" className="block h-full rounded-xl outline-none">
|
||||
<Card className="h-full overflow-hidden transition-colors hover:border-primary/40 hover:bg-accent/30">
|
||||
<CardContent className="flex h-full items-center gap-3 p-4">
|
||||
<div className="flex size-10 shrink-0 items-center justify-center rounded-xl bg-primary/10 text-primary">
|
||||
<Icon className="size-5" />
|
||||
</div>
|
||||
<div className="min-w-0 flex-1">
|
||||
<p className="truncate text-sm font-semibold">{config.label || config.url}</p>
|
||||
<p className="truncate text-[11px] text-muted-foreground">{config.url}</p>
|
||||
</div>
|
||||
<ExternalLink className="size-4 shrink-0 text-muted-foreground/60" />
|
||||
</CardContent>
|
||||
</Card>
|
||||
</a>
|
||||
);
|
||||
}
|
||||
|
||||
function AccountWidget({ config }: { config: AccountConfig }) {
|
||||
const { qqList } = useAppState();
|
||||
const acct = qqList.find((q) => q.uin === config.uin);
|
||||
if (!config.uin) {
|
||||
return (
|
||||
<Card className="h-full overflow-hidden">
|
||||
<CardContent className="flex h-full items-center gap-3 p-4 text-muted-foreground">
|
||||
<Users className="size-5 shrink-0" />
|
||||
<span className="text-sm">未指定账号 · 点齿轮设置</span>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<Card className="h-full overflow-hidden">
|
||||
<CardContent className="flex h-full items-center gap-3 p-4">
|
||||
<Avatar size={40}>
|
||||
<AvatarImage src={qqAvatarUrl(config.uin)} alt={acct?.nickname || config.uin} />
|
||||
<AvatarFallback>{(acct?.nickname || config.uin).slice(0, 2)}</AvatarFallback>
|
||||
</Avatar>
|
||||
<div className="min-w-0 flex-1">
|
||||
<p className="truncate text-sm font-semibold">{acct?.nickname || '未接入账号'}</p>
|
||||
<p className="truncate font-mono text-[11px] text-muted-foreground tabular-nums">{config.uin}</p>
|
||||
</div>
|
||||
<span
|
||||
title={acct ? '在线' : '未接入'}
|
||||
className={cn('size-2 shrink-0 rounded-full', acct ? 'bg-success' : 'bg-muted-foreground/40')}
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
// ─────────────── recent alerts ───────────────
|
||||
|
||||
const ALERT_LEVEL_CLASS: Record<LogLevel, string> = {
|
||||
|
||||
@@ -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<string, typeof Link2> = {
|
||||
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) {
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function NoteConfigForm({ config, onChange }: FormProps) {
|
||||
const c = parseNoteConfig(config);
|
||||
return (
|
||||
<div className="flex flex-col gap-2">
|
||||
<span className="text-sm text-muted-foreground">便签内容(纯文本,保留换行)</span>
|
||||
<textarea
|
||||
value={c.text}
|
||||
onChange={(e) => 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"
|
||||
/>
|
||||
<span className="text-right text-[11px] text-muted-foreground">{c.text.length} / {NOTE_MAX}</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function LinkConfigForm({ config, onChange }: FormProps) {
|
||||
const c = parseLinkConfig(config);
|
||||
const urlInvalid = c_urlRaw(config) !== '' && !c.url;
|
||||
return (
|
||||
<div className="flex flex-col gap-4">
|
||||
<label className="flex flex-col gap-1.5 text-sm">
|
||||
<span className="text-muted-foreground">标题</span>
|
||||
<Input value={c.label} placeholder="例如:项目文档" maxLength={60} onChange={(e) => onChange({ label: e.target.value })} className="h-8" />
|
||||
</label>
|
||||
<label className="flex flex-col gap-1.5 text-sm">
|
||||
<span className="text-muted-foreground">链接(http/https)</span>
|
||||
<Input
|
||||
value={c_urlRaw(config)}
|
||||
placeholder="https://example.com"
|
||||
maxLength={2048}
|
||||
onChange={(e) => onChange({ url: e.target.value })}
|
||||
className={cn('h-8', urlInvalid && 'border-destructive')}
|
||||
/>
|
||||
{urlInvalid && <span className="text-[11px] text-destructive">仅支持 http:// 或 https:// 链接</span>}
|
||||
</label>
|
||||
<div className="flex flex-col gap-1.5">
|
||||
<span className="text-sm text-muted-foreground">图标</span>
|
||||
<div className="flex flex-wrap gap-1.5">
|
||||
{LINK_ICON_IDS.map((ic) => {
|
||||
const Icon = LINK_ICON_COMPONENTS[ic];
|
||||
const on = c.icon === ic;
|
||||
return (
|
||||
<button
|
||||
key={ic}
|
||||
type="button"
|
||||
onClick={() => 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',
|
||||
)}
|
||||
>
|
||||
<Icon className="size-4" />
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// 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<string, unknown> | 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 (
|
||||
<div className="flex flex-col gap-2">
|
||||
<span className="text-sm text-muted-foreground">QQ 号</span>
|
||||
<Input
|
||||
value={c.uin}
|
||||
list={listId}
|
||||
inputMode="numeric"
|
||||
placeholder="指定一个账号的 QQ 号"
|
||||
maxLength={15}
|
||||
onChange={(e) => onChange({ uin: e.target.value.replace(/\D/g, '').slice(0, 15) })}
|
||||
className="h-8"
|
||||
/>
|
||||
<datalist id={listId}>
|
||||
{qqList.map((q) => <option key={q.uin} value={q.uin}>{q.nickname || q.uin}</option>)}
|
||||
</datalist>
|
||||
<span className="text-[11px] text-muted-foreground">可从已接入账号中选择,或手动输入。</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user