feat(webui): add live connection status for OneBot adapters in dashboard

This commit is contained in:
DrSmoothl committed 2026-05-30 18:02:01 +08:00
1 parent 90f26ca4f4
commit 0e4b881f76
18 files changed
+351 -13

No files matched your search

@@ -9,11 +9,27 @@ import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { cn } from '@/lib/utils';
import type { AdapterCommon } from './defaults';
import type { AdapterStatus } from '@/types';
const LIVE_STYLE: Record<AdapterStatus['status'], string> = {
ok: 'bg-success/10 text-success',
warn: 'bg-warning/10 text-warning',
down: 'bg-destructive/10 text-destructive',
disabled: 'bg-muted text-muted-foreground',
};
const LIVE_LABEL: Record<AdapterStatus['status'], string> = {
ok: '正常',
warn: '注意',
down: '异常',
disabled: '未启用',
};
interface NodeSummaryCardProps<T extends AdapterCommon> {
item: T;
summary: string;
duplicateName: boolean;
/** Live runtime status from the OneBot manager, matched by adapter name. */
liveStatus?: AdapterStatus;
onToggleEnabled: (next: boolean) => void;
onEdit: () => void;
onDelete: () => void;
@@ -23,6 +39,7 @@ export function NodeSummaryCard<T extends AdapterCommon>({
item,
summary,
duplicateName,
liveStatus,
onToggleEnabled,
onEdit,
onDelete,
@@ -51,6 +68,14 @@ export function NodeSummaryCard<T extends AdapterCommon>({
{!enabled && (
<Badge variant="secondary" className="font-normal">已停用</Badge>
)}
{liveStatus && (
<span
className={cn('rounded px-1.5 py-0.5 text-[10px] font-medium', LIVE_STYLE[liveStatus.status])}
title={liveStatus.detail}
>
{LIVE_LABEL[liveStatus.status]} · {liveStatus.detail}
</span>
)}
</div>
<div className="mt-0.5 truncate font-mono text-xs text-muted-foreground tabular-nums">{summary}</div>
<div className="mt-1.5 flex flex-wrap items-center gap-1.5">
@@ -16,6 +16,7 @@ import { Badge } from '@/components/ui/badge';
import { ConfirmDialog } from '@/components/confirm-dialog';
import { cn } from '@/lib/utils';
import type {
AdapterStatus,
NetworkKind,
OneBotConfig,
OneBotNetworks,
@@ -39,7 +40,7 @@ type DialogState =
// index: null → create with `seed`, otherwise edit the item at that position.
export function ConfigPage() {
const { qqList, selectedUin, setSelectedUin } = useAppState();
const { qqList, connections, selectedUin, setSelectedUin } = useAppState();
const {
config,
setConfig,
@@ -72,6 +73,15 @@ export function ConfigPage() {
[pendingSwitchUin, qqList],
);
// Live adapter status for the selected account, keyed by adapter name so
// each summary card can light up its real connection state.
const liveStatusByName = useMemo(() => {
const acc = connections.find((c) => c.uin === selectedUin);
const map = new Map<string, AdapterStatus>();
for (const a of acc?.adapters ?? []) map.set(a.name, a);
return map;
}, [connections, selectedUin]);
// A discrete node mutation (create / edit / delete / enable-toggle) is
// persisted the moment it happens — clicking 保存 inside the editor dialog
// (or flipping the enable switch) IS the save. This removes the old
@@ -170,6 +180,7 @@ export function ConfigPage() {
<NetworkTabView
kind={activeTab}
config={config}
statusByName={liveStatusByName}
onCreateClick={() => openCreate(activeTab)}
onEdit={(idx) => openEdit(activeTab, idx)}
onDelete={(idx) => handleDelete(activeTab, idx)}
@@ -344,6 +355,7 @@ function countMap(networks: OneBotNetworks): Record<NetworkKind, number> {
interface NetworkTabViewProps {
kind: NetworkKind;
config: OneBotConfig;
statusByName: Map<string, AdapterStatus>;
onCreateClick: () => void;
onEdit: (index: number) => void;
onDelete: (index: number) => void;
@@ -353,6 +365,7 @@ interface NetworkTabViewProps {
function NetworkTabView({
kind,
config,
statusByName,
onCreateClick,
onEdit,
onDelete,
@@ -401,6 +414,7 @@ function NetworkTabView({
item={item}
summary={summarize(item)}
duplicateName={duplicate}
liveStatus={statusByName.get(item.name)}
onToggleEnabled={(v) => onToggleEnabled(idx, v)}
onEdit={() => onEdit(idx)}
onDelete={() => onDelete(idx)}
@@ -4,6 +4,8 @@ import { Link } from '@tanstack/react-router';
import {
Activity,
ArrowRight,
Bell,
Cable,
Cpu,
MemoryStick,
MonitorCog,
@@ -20,6 +22,8 @@ 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 { AccountConnections, AdapterStatus, AdapterStatusLevel, LogEntry } from '@/types';
import { useApi } from '@/lib/api';
import { useAppState } from '@/contexts/AppStateContext';
import { useSession } from '@/contexts/SessionContext';
@@ -73,7 +77,7 @@ function StatTile({
}
export function OverviewPage() {
const { qqList, processList, systemInfo, refreshSystem } = useAppState();
const { qqList, processList, systemInfo, connections, refreshSystem } = useAppState();
const { status } = useSession();
// Lightweight tick to refresh "uptime" pretty-print every 30s
@@ -149,6 +153,12 @@ export function OverviewPage() {
</motion.div>
)}
{/* Operational health — connection status + recent alerts (side by side on wide screens) */}
<div className="grid gap-6 xl:grid-cols-2">
<ConnectionsCard connections={connections} />
<RecentAlertsCard />
</div>
{/* System metrics */}
<Card>
<CardHeader className="flex flex-row items-center justify-between gap-3 space-y-0">
@@ -297,3 +307,157 @@ export function OverviewPage() {
</div>
);
}
// ─────────────── connection health ───────────────
const CONN_STATUS_STYLE: Record<AdapterStatusLevel, string> = {
ok: 'bg-success/10 text-success',
warn: 'bg-warning/10 text-warning',
down: 'bg-destructive/10 text-destructive',
disabled: 'bg-muted text-muted-foreground',
};
const CONN_STATUS_LABEL: Record<AdapterStatusLevel, string> = {
ok: '正常',
warn: '注意',
down: '异常',
disabled: '未启用',
};
const ADAPTER_KIND_LABEL: Record<AdapterStatus['kind'], string> = {
httpServer: 'HTTP 服务端',
httpClient: 'HTTP 上报',
wsServer: 'WS 服务端',
wsClient: 'WS 客户端',
};
function ConnectionsCard({ connections }: { connections: AccountConnections[] }) {
return (
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Cable className="size-4 text-primary" /> OneBot 连接
</CardTitle>
<CardDescription>各账号协议端点的实时连接状态</CardDescription>
</CardHeader>
<CardContent>
{connections.length === 0 ? (
<div className="flex flex-col items-center justify-center gap-2 rounded-lg border border-dashed py-10 text-muted-foreground">
<Cable className="size-7" strokeWidth={1.5} />
<p className="text-sm">暂无已接入的账号实例</p>
</div>
) : (
<div className="flex flex-col gap-4">
{connections.map((acc) => (
<div key={acc.uin} className="flex flex-col gap-2">
<div className="flex items-baseline gap-2">
<span className="text-sm font-semibold">{acc.nickname || acc.uin}</span>
<span className="font-mono text-[11px] text-muted-foreground tabular-nums">{acc.uin}</span>
</div>
{acc.adapters.length === 0 ? (
<p className="rounded-md border border-dashed px-3 py-2 text-[11px] text-muted-foreground">
未配置任何协议端点
</p>
) : (
<div className="flex flex-col gap-1.5">
{acc.adapters.map((a) => (
<div key={a.name} className="flex items-center gap-2 rounded-lg border bg-card/40 px-3 py-2">
<span
className={cn(
'shrink-0 rounded px-1.5 py-0.5 text-[10px] font-medium',
CONN_STATUS_STYLE[a.status],
)}
>
{CONN_STATUS_LABEL[a.status]}
</span>
<div className="min-w-0 flex-1">
<div className="flex items-center gap-1.5">
<span className="truncate text-sm font-medium">{a.name}</span>
<span className="shrink-0 text-[10px] text-muted-foreground">{ADAPTER_KIND_LABEL[a.kind]}</span>
</div>
<div className="truncate text-[11px] text-muted-foreground">{a.detail}</div>
</div>
</div>
))}
</div>
)}
</div>
))}
</div>
)}
</CardContent>
</Card>
);
}
// ─────────────── recent alerts ───────────────
function alertClock(t: string): string {
const d = new Date(t);
return Number.isNaN(d.getTime()) ? t : d.toLocaleTimeString();
}
function RecentAlertsCard() {
const api = useApi();
const [alerts, setAlerts] = useState<LogEntry[]>([]);
useEffect(() => {
let active = true;
api.logs
.list(200)
.then((list) => {
if (!active) return;
setAlerts(list.filter((l) => l.level === 'warn' || l.level === 'error').slice(-5));
})
.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));
},
});
return () => {
active = false;
stop();
};
}, [api]);
return (
<Card>
<CardHeader className="flex flex-row items-center justify-between gap-3 space-y-0">
<div>
<CardTitle className="flex items-center gap-2">
<Bell className="size-4 text-primary" /> 最近告警
</CardTitle>
<CardDescription>最近的 warn / error 级别日志</CardDescription>
</div>
<Link
to="/logs"
className="inline-flex shrink-0 items-center gap-1 text-[11px] text-muted-foreground hover:text-foreground"
>
查看日志 <ArrowRight className="size-3" />
</Link>
</CardHeader>
<CardContent>
{alerts.length === 0 ? (
<div className="flex flex-col items-center justify-center gap-2 rounded-lg border border-dashed py-10 text-muted-foreground">
<Bell className="size-7" strokeWidth={1.5} />
<p className="text-sm">暂无告警</p>
</div>
) : (
<div className="flex flex-col gap-1 font-mono text-[11px]">
{alerts.map((a) => (
<div key={a.id} className="flex gap-2 rounded px-2 py-1 hover:bg-accent/30">
<span className="shrink-0 text-muted-foreground tabular-nums">{alertClock(a.time)}</span>
<span className={cn('shrink-0 font-semibold', a.level === 'error' ? 'text-destructive' : 'text-warning')}>
{a.level.toUpperCase()}
</span>
<span className="min-w-0 flex-1 truncate" title={a.message}>
[{a.scope}] {a.message}
</span>
</div>
))}
</div>
)}
</CardContent>
</Card>
);
}