refactor(webui): redesign per-UIN config page into sidebar + tabs + modal
The original page stacked four expanded network sections vertically with inline editing, so the first view drowned in every field across every adapter at once. Restructured to a sidebar + tabs + modal layout: - Account sidebar collapses to a 56px avatar column with name/UIN tooltips; expanded it keeps the original 260px nickname + UIN layout. - Right pane swaps the stacked sections for a 5-tab strip (通用设置 / HTTP API / HTTP 客户端 / WS 服务端 / WS 客户端) with a per-tab badge count. - Each network tab is a list of summary cards: name + projected key info (host:port/path, url · role, etc.) + format/report/token badges + inline enable toggle + edit/delete buttons. No live-typed fields. - Create and edit both route through a single NodeEditDialog with kind-specific field strips; cancel truly discards the draft. - Per-kind metadata (title, summary projector, defaultEntry factory) lives in config/defaults.ts so adding a new adapter kind only needs one descriptor entry. Behavior preserved end-to-end: same per-UIN config payload, same useOneBotInstanceConfig hook, same dirty-switch confirm flow, same save endpoint.
This commit is contained in:
6 files changed
+1159
-645
No files matched your search
@@ -0,0 +1,114 @@
|
||||
// Account selector strip. Collapsing to a 56px avatar-only column keeps
|
||||
// the right-side editor breathing on smaller screens; expanded it shows
|
||||
// nickname + UIN like the original. Selection routes through
|
||||
// `requestSwitchUin` so the dirty-modify guard still gates the switch.
|
||||
|
||||
import { motion } from 'motion/react';
|
||||
import { ChevronLeft, ChevronRight } from 'lucide-react';
|
||||
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { ScrollArea } from '@/components/ui/scroll-area';
|
||||
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip';
|
||||
import { cn } from '@/lib/utils';
|
||||
import type { QQInfo } from '@/types';
|
||||
|
||||
function qqAvatarUrl(uin: string) {
|
||||
return `/avatar/${encodeURIComponent(uin)}`;
|
||||
}
|
||||
|
||||
interface AccountSidebarProps {
|
||||
accounts: QQInfo[];
|
||||
selectedUin: string | null;
|
||||
onSelect: (uin: string) => void;
|
||||
collapsed: boolean;
|
||||
onToggleCollapsed: () => void;
|
||||
}
|
||||
|
||||
export function AccountSidebar({
|
||||
accounts,
|
||||
selectedUin,
|
||||
onSelect,
|
||||
collapsed,
|
||||
onToggleCollapsed,
|
||||
}: AccountSidebarProps) {
|
||||
return (
|
||||
<motion.aside
|
||||
animate={{ width: collapsed ? 56 : 248 }}
|
||||
transition={{ type: 'spring', stiffness: 300, damping: 32 }}
|
||||
className="shrink-0 rounded-lg border bg-card/40"
|
||||
>
|
||||
<div className={cn('flex items-center', collapsed ? 'justify-center px-1.5 py-2' : 'justify-between px-3 py-2')}>
|
||||
{!collapsed && <span className="text-xs font-medium text-muted-foreground">在线连接</span>}
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon-sm"
|
||||
onClick={onToggleCollapsed}
|
||||
aria-label={collapsed ? '展开账号列表' : '收起账号列表'}
|
||||
className="text-muted-foreground"
|
||||
>
|
||||
{collapsed ? <ChevronRight className="size-4" /> : <ChevronLeft className="size-4" />}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{accounts.length === 0 ? (
|
||||
<p className={cn('py-6 text-center text-xs text-muted-foreground', collapsed && 'px-1')}>
|
||||
{collapsed ? '—' : '暂无在线会话'}
|
||||
</p>
|
||||
) : (
|
||||
<ScrollArea className="max-h-[70vh]" viewportClassName="[&>div]:!block">
|
||||
<TooltipProvider delayDuration={300}>
|
||||
<div className={cn('flex flex-col gap-1', collapsed ? 'px-1.5 pb-2' : 'px-2 pb-2')}>
|
||||
{accounts.map((q) => {
|
||||
const isActive = selectedUin === q.uin;
|
||||
const button = (
|
||||
<motion.button
|
||||
key={q.uin}
|
||||
type="button"
|
||||
whileHover={{ scale: 1.01 }}
|
||||
whileTap={{ scale: 0.98 }}
|
||||
onClick={() => onSelect(q.uin)}
|
||||
className={cn(
|
||||
'flex items-center gap-2.5 rounded-lg border text-left transition-colors cursor-pointer',
|
||||
collapsed ? 'justify-center p-1.5' : 'px-2.5 py-2',
|
||||
isActive ? 'border-primary/30 bg-primary/10' : 'border-transparent hover:bg-accent/40',
|
||||
)}
|
||||
>
|
||||
<Avatar size={collapsed ? 32 : 28}>
|
||||
<AvatarImage src={qqAvatarUrl(q.uin)} alt={q.nickname || q.uin} />
|
||||
<AvatarFallback>{(q.nickname || q.uin).slice(0, 2)}</AvatarFallback>
|
||||
</Avatar>
|
||||
{!collapsed && (
|
||||
<div className="min-w-0 flex-1">
|
||||
<div
|
||||
className={cn('truncate text-sm font-medium', isActive ? 'text-primary' : 'text-foreground')}
|
||||
>
|
||||
{q.nickname || q.uin}
|
||||
</div>
|
||||
<div className="truncate font-mono text-[10px] text-muted-foreground tabular-nums">
|
||||
{q.uin}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</motion.button>
|
||||
);
|
||||
return collapsed ? (
|
||||
<Tooltip key={q.uin}>
|
||||
<TooltipTrigger asChild>{button}</TooltipTrigger>
|
||||
<TooltipContent side="right" sideOffset={8}>
|
||||
<div className="flex flex-col leading-tight">
|
||||
<span className="font-medium">{q.nickname || q.uin}</span>
|
||||
<span className="font-mono text-[10px] text-muted-foreground tabular-nums">{q.uin}</span>
|
||||
</div>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
) : (
|
||||
button
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</TooltipProvider>
|
||||
</ScrollArea>
|
||||
)}
|
||||
</motion.aside>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
// Per-network-kind metadata: localized title, summary projector for the
|
||||
// list view, and factory for "create new" defaults. Centralised here so the
|
||||
// page / list / dialog all agree on naming and shape and a new adapter
|
||||
// kind only needs one entry in NETWORK_TABS to wire end-to-end.
|
||||
|
||||
import type {
|
||||
HttpClientNetwork,
|
||||
HttpServerNetwork,
|
||||
MessageFormat,
|
||||
NetworkKind,
|
||||
OneBotNetworks,
|
||||
WsClientNetwork,
|
||||
WsRole,
|
||||
WsServerNetwork,
|
||||
} from '@/types';
|
||||
|
||||
export type TabKey = 'general' | NetworkKind;
|
||||
|
||||
export interface NetworkTabDescriptor<K extends NetworkKind> {
|
||||
key: K;
|
||||
/** Tab label and dialog title prefix. */
|
||||
title: string;
|
||||
/** Short subtitle shown under the tab when active. */
|
||||
description: string;
|
||||
/** Singular noun for "新建 / 编辑 X". */
|
||||
noun: string;
|
||||
/** Project the adapter into a short list-card summary string. */
|
||||
summarize: (item: OneBotNetworks[K][number]) => string;
|
||||
/** Factory producing a fresh adapter with a unique suffix in its name. */
|
||||
defaultEntry: (suffix: number) => OneBotNetworks[K][number];
|
||||
}
|
||||
|
||||
function genToken(): string {
|
||||
const bytes = new Uint8Array(32);
|
||||
globalThis.crypto.getRandomValues(bytes);
|
||||
return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
|
||||
}
|
||||
|
||||
const httpServersTab: NetworkTabDescriptor<'httpServers'> = {
|
||||
key: 'httpServers',
|
||||
title: 'HTTP API',
|
||||
description: '本地监听 HTTP 端口,OneBot 客户端发起请求',
|
||||
noun: 'HTTP API 服务',
|
||||
summarize: (it: HttpServerNetwork) => {
|
||||
const host = it.host?.trim() || '0.0.0.0';
|
||||
const path = (it.path?.trim() || '/').replace(/^\/?/, '/');
|
||||
return `${host}:${it.port}${path}`;
|
||||
},
|
||||
defaultEntry: (suffix): HttpServerNetwork => ({
|
||||
name: `http-${suffix}`,
|
||||
host: '0.0.0.0',
|
||||
port: 3000,
|
||||
path: '/',
|
||||
accessToken: genToken(),
|
||||
messageFormat: 'array',
|
||||
reportSelfMessage: false,
|
||||
}),
|
||||
};
|
||||
|
||||
const httpClientsTab: NetworkTabDescriptor<'httpClients'> = {
|
||||
key: 'httpClients',
|
||||
title: 'HTTP 客户端',
|
||||
description: '主动向远端 URL POST 推送事件',
|
||||
noun: 'HTTP 推送客户端',
|
||||
summarize: (it: HttpClientNetwork) => it.url || '(未设置 URL)',
|
||||
defaultEntry: (suffix): HttpClientNetwork => ({
|
||||
name: `httppost-${suffix}`,
|
||||
url: 'http://127.0.0.1:5700',
|
||||
messageFormat: 'array',
|
||||
reportSelfMessage: false,
|
||||
}),
|
||||
};
|
||||
|
||||
const wsServersTab: NetworkTabDescriptor<'wsServers'> = {
|
||||
key: 'wsServers',
|
||||
title: 'WS 服务端',
|
||||
description: '本地监听 WebSocket 端口,客户端建立持久连接',
|
||||
noun: 'WebSocket 服务',
|
||||
summarize: (it: WsServerNetwork) => {
|
||||
const host = it.host?.trim() || '0.0.0.0';
|
||||
const path = (it.path?.trim() || '/').replace(/^\/?/, '/');
|
||||
const role = it.role ?? 'Universal';
|
||||
return `${host}:${it.port}${path} · ${role}`;
|
||||
},
|
||||
defaultEntry: (suffix): WsServerNetwork => ({
|
||||
name: `ws-${suffix}`,
|
||||
host: '0.0.0.0',
|
||||
port: 3001,
|
||||
path: '/',
|
||||
role: 'Universal' as WsRole,
|
||||
accessToken: genToken(),
|
||||
messageFormat: 'array',
|
||||
reportSelfMessage: false,
|
||||
}),
|
||||
};
|
||||
|
||||
const wsClientsTab: NetworkTabDescriptor<'wsClients'> = {
|
||||
key: 'wsClients',
|
||||
title: 'WS 客户端',
|
||||
description: '主动连接到外部 WebSocket 服务器(reverse-ws)',
|
||||
noun: 'WebSocket 反向客户端',
|
||||
summarize: (it: WsClientNetwork) => {
|
||||
const role = it.role ?? 'Universal';
|
||||
return `${it.url || '(未设置 URL)'} · ${role}`;
|
||||
},
|
||||
defaultEntry: (suffix): WsClientNetwork => ({
|
||||
name: `wsclient-${suffix}`,
|
||||
url: 'ws://127.0.0.1:8080/ws',
|
||||
role: 'Universal' as WsRole,
|
||||
reconnectIntervalMs: 5000,
|
||||
messageFormat: 'array',
|
||||
reportSelfMessage: false,
|
||||
}),
|
||||
};
|
||||
|
||||
/**
|
||||
* Order here drives both the tab strip order and the dialog/page wiring.
|
||||
* Each entry stays strictly typed on its kind so callers get the narrow
|
||||
* adapter shape (no NetworkBase widening).
|
||||
*/
|
||||
export const NETWORK_TABS = {
|
||||
httpServers: httpServersTab,
|
||||
httpClients: httpClientsTab,
|
||||
wsServers: wsServersTab,
|
||||
wsClients: wsClientsTab,
|
||||
} satisfies { [K in NetworkKind]: NetworkTabDescriptor<K> };
|
||||
|
||||
export const NETWORK_TAB_ORDER: NetworkKind[] = [
|
||||
'httpServers',
|
||||
'httpClients',
|
||||
'wsServers',
|
||||
'wsClients',
|
||||
];
|
||||
|
||||
export const ALL_TABS: TabKey[] = ['general', ...NETWORK_TAB_ORDER];
|
||||
|
||||
/**
|
||||
* Find the smallest suffix N (starting at list.length + 1) such that
|
||||
* `factory(N).name` doesn't collide with any existing adapter. Used by
|
||||
* the page to seed a fresh-default adapter when the user clicks "create".
|
||||
*/
|
||||
export function nextUniqueSuffix(
|
||||
networks: { name: string }[],
|
||||
factory: (suffix: number) => { name: string },
|
||||
): number {
|
||||
const used = new Set(networks.map((n) => n.name));
|
||||
let suffix = networks.length + 1;
|
||||
while (used.has(factory(suffix).name)) suffix += 1;
|
||||
return suffix;
|
||||
}
|
||||
|
||||
// Re-exported for callers that just need to type a generic adapter.
|
||||
export interface AdapterCommon {
|
||||
name: string;
|
||||
enabled?: boolean;
|
||||
accessToken?: string;
|
||||
messageFormat: MessageFormat;
|
||||
reportSelfMessage: boolean;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// "通用设置" tab — fields that apply to the whole OneBotInstance rather
|
||||
// than any specific adapter. Currently just the music-sign service URL,
|
||||
// but the layout has room for additional cross-cutting toggles.
|
||||
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import type { OneBotConfig } from '@/types';
|
||||
|
||||
interface GeneralSettingsTabProps {
|
||||
config: OneBotConfig;
|
||||
onChange: (next: OneBotConfig) => void;
|
||||
}
|
||||
|
||||
export function GeneralSettingsTab({ config, onChange }: GeneralSettingsTabProps) {
|
||||
return (
|
||||
<div className="flex flex-col gap-4">
|
||||
<div className="flex flex-col gap-1.5 rounded-lg border bg-card/40 p-4">
|
||||
<Label>音乐签名服务 URL</Label>
|
||||
<Input
|
||||
type="url"
|
||||
placeholder="留空则不启用"
|
||||
value={config.musicSignUrl ?? ''}
|
||||
onChange={(e) => onChange({ ...config, musicSignUrl: e.target.value || undefined })}
|
||||
/>
|
||||
<p className="text-[11px] leading-relaxed text-muted-foreground">
|
||||
用于音乐分享卡片签名。未配置时音乐相关消息段会回落为普通文本。
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,407 @@
|
||||
// Modal editor used for both "create" and "edit". Holds a local draft
|
||||
// copy of the adapter until the user clicks save (so cancel really does
|
||||
// throw away changes). Validation is minimal — blank-name and dup-name
|
||||
// disable the save button; everything else is best-effort coercion.
|
||||
|
||||
import { useState } from 'react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from '@/components/ui/dialog';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { cn } from '@/lib/utils';
|
||||
import type {
|
||||
HttpClientNetwork,
|
||||
HttpServerNetwork,
|
||||
MessageFormat,
|
||||
NetworkKind,
|
||||
OneBotNetworks,
|
||||
WsClientNetwork,
|
||||
WsRole,
|
||||
WsServerNetwork,
|
||||
} from '@/types';
|
||||
import { NETWORK_TABS } from './defaults';
|
||||
|
||||
type AnyAdapter<K extends NetworkKind> = OneBotNetworks[K][number];
|
||||
|
||||
interface NodeEditDialogProps<K extends NetworkKind> {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
kind: K;
|
||||
/** Initial draft state — caller seeds it with `defaultEntry` for create
|
||||
* or the existing item for edit. */
|
||||
initial: AnyAdapter<K>;
|
||||
/** True when editing an existing adapter (drives title + button copy). */
|
||||
isEdit: boolean;
|
||||
/** Names of every other adapter in the same list, for duplicate check. */
|
||||
otherNames: string[];
|
||||
onSubmit: (item: AnyAdapter<K>) => void;
|
||||
}
|
||||
|
||||
export function NodeEditDialog<K extends NetworkKind>(props: NodeEditDialogProps<K>) {
|
||||
const { open, onOpenChange, kind, initial, isEdit, otherNames, onSubmit } = props;
|
||||
const tab = NETWORK_TABS[kind];
|
||||
|
||||
// Local draft. The parent unmounts this component on close so each
|
||||
// open gets a fresh `initial` via useState's lazy init — no effect-based
|
||||
// resync needed, which keeps the lifecycle linear.
|
||||
const [draft, setDraft] = useState<AnyAdapter<K>>(initial);
|
||||
|
||||
const trimmedName = draft.name?.trim() ?? '';
|
||||
const blankName = trimmedName.length === 0;
|
||||
const duplicateName = !blankName && otherNames.includes(trimmedName);
|
||||
|
||||
const canSave = !blankName && !duplicateName;
|
||||
|
||||
const patch = (changes: Partial<AnyAdapter<K>>) => setDraft({ ...draft, ...changes } as AnyAdapter<K>);
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="max-w-xl">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{isEdit ? `编辑 ${tab.noun}` : `新建 ${tab.noun}`}</DialogTitle>
|
||||
<DialogDescription>{tab.description}</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="flex flex-col gap-3">
|
||||
<Field
|
||||
label="名称"
|
||||
placeholder="自定义"
|
||||
value={draft.name}
|
||||
onChange={(v) => patch({ name: v } as Partial<AnyAdapter<K>>)}
|
||||
error={blankName ? '请填写名称' : duplicateName ? '名称与其它节点重复' : undefined}
|
||||
/>
|
||||
|
||||
<KindFields kind={kind} draft={draft} patch={patch} />
|
||||
|
||||
<Field
|
||||
label="授权 Token"
|
||||
placeholder="不填则无密码"
|
||||
value={draft.accessToken}
|
||||
onChange={(v) => patch({ accessToken: v || undefined } as Partial<AnyAdapter<K>>)}
|
||||
/>
|
||||
|
||||
<div className="grid gap-3 border-t pt-3 sm:grid-cols-2">
|
||||
<SegmentedField
|
||||
label="消息格式"
|
||||
value={(draft.messageFormat ?? 'array') as MessageFormat}
|
||||
options={FORMAT_OPTIONS}
|
||||
onChange={(v) => patch({ messageFormat: v } as Partial<AnyAdapter<K>>)}
|
||||
/>
|
||||
<SegmentedField
|
||||
label="上报自身消息"
|
||||
value={draft.reportSelfMessage ? 'on' : 'off'}
|
||||
options={REPORT_OPTIONS}
|
||||
onChange={(v) => patch({ reportSelfMessage: v === 'on' } as Partial<AnyAdapter<K>>)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between rounded-lg border bg-card/40 p-3">
|
||||
<div>
|
||||
<Label className="text-sm">启用</Label>
|
||||
<p className="mt-1 text-[11px] leading-relaxed text-muted-foreground">
|
||||
关闭后保存即可保留配置但不启动该节点
|
||||
</p>
|
||||
</div>
|
||||
<ToggleSwitch
|
||||
value={draft.enabled !== false}
|
||||
onChange={(v) =>
|
||||
patch({ enabled: v ? undefined : false } as Partial<AnyAdapter<K>>)
|
||||
}
|
||||
ariaLabel="启用"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={() => onOpenChange(false)}>
|
||||
取消
|
||||
</Button>
|
||||
<Button
|
||||
disabled={!canSave}
|
||||
onClick={() => {
|
||||
const cleaned = { ...draft, name: trimmedName } as AnyAdapter<K>;
|
||||
onSubmit(cleaned);
|
||||
onOpenChange(false);
|
||||
}}
|
||||
>
|
||||
{isEdit ? '保存修改' : '创建节点'}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
||||
// ─────────────── kind-specific field strips ───────────────
|
||||
|
||||
interface KindFieldsProps<K extends NetworkKind> {
|
||||
kind: K;
|
||||
draft: AnyAdapter<K>;
|
||||
patch: (changes: Partial<AnyAdapter<K>>) => void;
|
||||
}
|
||||
|
||||
function KindFields<K extends NetworkKind>({ kind, draft, patch }: KindFieldsProps<K>) {
|
||||
// Per-branch narrowing — TS can't follow the generic relationship so
|
||||
// each arm casts once. Field components receive specific shapes.
|
||||
if (kind === 'httpServers') {
|
||||
const it = draft as HttpServerNetwork;
|
||||
const set = patch as (c: Partial<HttpServerNetwork>) => void;
|
||||
return (
|
||||
<div className="grid gap-3 sm:grid-cols-[1fr_120px_140px]">
|
||||
<Field
|
||||
label="主机"
|
||||
placeholder="0.0.0.0"
|
||||
value={it.host}
|
||||
onChange={(v) => set({ host: v || undefined })}
|
||||
/>
|
||||
<Field
|
||||
label="端口"
|
||||
type="number"
|
||||
value={it.port}
|
||||
onChange={(v) => set({ port: Number(v) || 0 })}
|
||||
/>
|
||||
<Field
|
||||
label="路径"
|
||||
placeholder="/"
|
||||
value={it.path}
|
||||
onChange={(v) => set({ path: v || undefined })}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
if (kind === 'httpClients') {
|
||||
const it = draft as HttpClientNetwork;
|
||||
const set = patch as (c: Partial<HttpClientNetwork>) => void;
|
||||
return (
|
||||
<div className="grid gap-3 sm:grid-cols-[1fr_140px]">
|
||||
<Field
|
||||
label="目标 URL"
|
||||
type="url"
|
||||
placeholder="http://..."
|
||||
value={it.url}
|
||||
onChange={(v) => set({ url: v })}
|
||||
/>
|
||||
<Field
|
||||
label="超时 (ms)"
|
||||
type="number"
|
||||
placeholder="5000"
|
||||
value={it.timeoutMs}
|
||||
onChange={(v) => set({ timeoutMs: Number(v) || undefined })}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
if (kind === 'wsServers') {
|
||||
const it = draft as WsServerNetwork;
|
||||
const set = patch as (c: Partial<WsServerNetwork>) => void;
|
||||
return (
|
||||
<>
|
||||
<div className="grid gap-3 sm:grid-cols-[1fr_120px_140px]">
|
||||
<Field
|
||||
label="主机"
|
||||
placeholder="0.0.0.0"
|
||||
value={it.host}
|
||||
onChange={(v) => set({ host: v || undefined })}
|
||||
/>
|
||||
<Field
|
||||
label="端口"
|
||||
type="number"
|
||||
value={it.port}
|
||||
onChange={(v) => set({ port: Number(v) || 0 })}
|
||||
/>
|
||||
<Field
|
||||
label="路径"
|
||||
placeholder="/"
|
||||
value={it.path}
|
||||
onChange={(v) => set({ path: v || undefined })}
|
||||
/>
|
||||
</div>
|
||||
<SelectField
|
||||
label="角色"
|
||||
value={(it.role ?? 'Universal') as WsRole}
|
||||
options={WS_ROLE_OPTIONS}
|
||||
onChange={(v) => set({ role: v })}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
if (kind === 'wsClients') {
|
||||
const it = draft as WsClientNetwork;
|
||||
const set = patch as (c: Partial<WsClientNetwork>) => void;
|
||||
return (
|
||||
<>
|
||||
<Field
|
||||
label="目标 URL"
|
||||
type="url"
|
||||
placeholder="ws://..."
|
||||
value={it.url}
|
||||
onChange={(v) => set({ url: v })}
|
||||
/>
|
||||
<div className="grid gap-3 sm:grid-cols-2">
|
||||
<SelectField
|
||||
label="角色"
|
||||
value={(it.role ?? 'Universal') as WsRole}
|
||||
options={WS_ROLE_OPTIONS}
|
||||
onChange={(v) => set({ role: v })}
|
||||
/>
|
||||
<Field
|
||||
label="重连间隔 (ms)"
|
||||
type="number"
|
||||
value={it.reconnectIntervalMs}
|
||||
onChange={(v) => set({ reconnectIntervalMs: Number(v) || undefined })}
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// ─────────────── shared form bits ───────────────
|
||||
|
||||
interface FieldProps {
|
||||
label: string;
|
||||
value: string | number | undefined;
|
||||
onChange: (v: string) => void;
|
||||
placeholder?: string;
|
||||
type?: 'text' | 'number' | 'url';
|
||||
error?: string;
|
||||
}
|
||||
|
||||
function Field({ label, value, onChange, placeholder, type = 'text', error }: FieldProps) {
|
||||
return (
|
||||
<div className="flex flex-col gap-1.5">
|
||||
<Label>{label}</Label>
|
||||
<Input
|
||||
type={type}
|
||||
value={value ?? ''}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
placeholder={placeholder}
|
||||
className={cn(error && 'border-destructive focus-visible:ring-destructive/40')}
|
||||
/>
|
||||
{error && <p className="text-[11px] text-destructive">{error}</p>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
interface SegmentedOption<T extends string> {
|
||||
value: T;
|
||||
label: string;
|
||||
}
|
||||
|
||||
function SegmentedField<T extends string>({
|
||||
label,
|
||||
value,
|
||||
options,
|
||||
onChange,
|
||||
}: {
|
||||
label: string;
|
||||
value: T;
|
||||
options: ReadonlyArray<SegmentedOption<T>>;
|
||||
onChange: (v: T) => void;
|
||||
}) {
|
||||
return (
|
||||
<div className="flex flex-col gap-1.5">
|
||||
<Label>{label}</Label>
|
||||
<div className="flex flex-wrap gap-1 rounded-md border bg-muted/30 p-1">
|
||||
{options.map((opt) => {
|
||||
const active = value === opt.value;
|
||||
return (
|
||||
<button
|
||||
key={opt.value}
|
||||
type="button"
|
||||
onClick={() => onChange(opt.value)}
|
||||
className={cn(
|
||||
'flex-1 rounded px-2.5 py-1 text-xs transition-colors cursor-pointer',
|
||||
active ? 'bg-primary text-primary-foreground' : 'text-muted-foreground hover:bg-accent/50',
|
||||
)}
|
||||
>
|
||||
{opt.label}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function SelectField<T extends string>({
|
||||
label,
|
||||
value,
|
||||
options,
|
||||
onChange,
|
||||
}: {
|
||||
label: string;
|
||||
value: T;
|
||||
options: ReadonlyArray<SegmentedOption<T>>;
|
||||
onChange: (v: T) => void;
|
||||
}) {
|
||||
return (
|
||||
<div className="flex flex-col gap-1.5">
|
||||
<Label>{label}</Label>
|
||||
<select
|
||||
value={value}
|
||||
onChange={(e) => onChange(e.target.value as T)}
|
||||
className="h-9 rounded-md border bg-background px-2 text-sm shadow-xs"
|
||||
>
|
||||
{options.map((opt) => (
|
||||
<option key={opt.value} value={opt.value}>
|
||||
{opt.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
interface ToggleSwitchProps {
|
||||
value: boolean;
|
||||
onChange: (v: boolean) => void;
|
||||
ariaLabel: string;
|
||||
}
|
||||
|
||||
function ToggleSwitch({ value, onChange, ariaLabel }: ToggleSwitchProps) {
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
role="switch"
|
||||
aria-checked={value}
|
||||
aria-label={ariaLabel}
|
||||
onClick={() => onChange(!value)}
|
||||
className={cn(
|
||||
'relative inline-flex h-6 w-11 shrink-0 cursor-pointer items-center rounded-full border transition-colors',
|
||||
value ? 'border-primary bg-primary' : 'border-input bg-muted',
|
||||
)}
|
||||
>
|
||||
<span
|
||||
className={cn(
|
||||
'inline-block size-4 rounded-full bg-background shadow-sm transition-transform',
|
||||
value ? 'translate-x-[22px]' : 'translate-x-1',
|
||||
)}
|
||||
/>
|
||||
</button>
|
||||
);
|
||||
}
|
||||
|
||||
const FORMAT_OPTIONS: ReadonlyArray<SegmentedOption<MessageFormat>> = [
|
||||
{ value: 'array', label: '数组' },
|
||||
{ value: 'string', label: 'CQ 码' },
|
||||
];
|
||||
|
||||
const REPORT_OPTIONS: ReadonlyArray<SegmentedOption<'on' | 'off'>> = [
|
||||
{ value: 'on', label: '开启' },
|
||||
{ value: 'off', label: '关闭' },
|
||||
];
|
||||
|
||||
const WS_ROLE_OPTIONS: ReadonlyArray<SegmentedOption<WsRole>> = [
|
||||
{ value: 'Universal', label: 'Universal' },
|
||||
{ value: 'Event', label: 'Event' },
|
||||
{ value: 'Api', label: 'Api' },
|
||||
];
|
||||
@@ -0,0 +1,130 @@
|
||||
// One row in the per-kind list view. Shows just enough to identify the
|
||||
// adapter at a glance (name + projected summary + token state + the two
|
||||
// flags) plus the inline enable toggle. Real editing always goes through
|
||||
// the dialog — there are no live-typed inputs here.
|
||||
|
||||
import { motion } from 'motion/react';
|
||||
import { Pencil, Trash2 } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { cn } from '@/lib/utils';
|
||||
import type { AdapterCommon } from './defaults';
|
||||
|
||||
interface NodeSummaryCardProps<T extends AdapterCommon> {
|
||||
item: T;
|
||||
summary: string;
|
||||
duplicateName: boolean;
|
||||
onToggleEnabled: (next: boolean) => void;
|
||||
onEdit: () => void;
|
||||
onDelete: () => void;
|
||||
}
|
||||
|
||||
export function NodeSummaryCard<T extends AdapterCommon>({
|
||||
item,
|
||||
summary,
|
||||
duplicateName,
|
||||
onToggleEnabled,
|
||||
onEdit,
|
||||
onDelete,
|
||||
}: NodeSummaryCardProps<T>) {
|
||||
const enabled = item.enabled !== false;
|
||||
const blankName = !item.name?.trim();
|
||||
|
||||
return (
|
||||
<motion.div
|
||||
initial={{ opacity: 0, y: 4 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
transition={{ duration: 0.18 }}
|
||||
className={cn(
|
||||
'flex flex-col gap-2 rounded-lg border bg-card/40 p-3 transition-opacity sm:flex-row sm:items-center sm:gap-3',
|
||||
!enabled && 'opacity-60',
|
||||
)}
|
||||
>
|
||||
<div className="min-w-0 flex-1">
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<span className={cn('font-medium', blankName && 'text-destructive')}>
|
||||
{item.name || '(未命名)'}
|
||||
</span>
|
||||
{duplicateName && (
|
||||
<Badge variant="destructive" className="font-normal">名称重复</Badge>
|
||||
)}
|
||||
{!enabled && (
|
||||
<Badge variant="secondary" className="font-normal">已停用</Badge>
|
||||
)}
|
||||
</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">
|
||||
<Badge variant="outline" className="font-normal">
|
||||
{item.messageFormat === 'string' ? 'CQ 码' : '数组'}
|
||||
</Badge>
|
||||
<Badge variant="outline" className="font-normal">
|
||||
{item.reportSelfMessage ? '上报自身' : '不上报自身'}
|
||||
</Badge>
|
||||
{item.accessToken ? (
|
||||
<Badge variant="outline" className="font-normal">
|
||||
已设 Token
|
||||
</Badge>
|
||||
) : (
|
||||
<Badge variant="outline" className="font-normal text-muted-foreground">
|
||||
无 Token
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-1.5 sm:gap-2 sm:justify-end">
|
||||
<ToggleSwitch
|
||||
value={enabled}
|
||||
onChange={onToggleEnabled}
|
||||
ariaLabel={`启用 ${item.name || '该节点'}`}
|
||||
/>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon-sm"
|
||||
onClick={onEdit}
|
||||
aria-label="编辑"
|
||||
className="text-muted-foreground hover:text-foreground"
|
||||
>
|
||||
<Pencil className="size-4" />
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon-sm"
|
||||
onClick={onDelete}
|
||||
aria-label="删除"
|
||||
className="text-muted-foreground hover:text-destructive"
|
||||
>
|
||||
<Trash2 className="size-4" />
|
||||
</Button>
|
||||
</div>
|
||||
</motion.div>
|
||||
);
|
||||
}
|
||||
|
||||
interface ToggleSwitchProps {
|
||||
value: boolean;
|
||||
onChange: (v: boolean) => void;
|
||||
ariaLabel: string;
|
||||
}
|
||||
|
||||
function ToggleSwitch({ value, onChange, ariaLabel }: ToggleSwitchProps) {
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
role="switch"
|
||||
aria-checked={value}
|
||||
aria-label={ariaLabel}
|
||||
onClick={() => onChange(!value)}
|
||||
className={cn(
|
||||
'relative inline-flex h-6 w-11 shrink-0 cursor-pointer items-center rounded-full border transition-colors',
|
||||
value ? 'border-primary bg-primary' : 'border-input bg-muted',
|
||||
)}
|
||||
>
|
||||
<motion.span
|
||||
className="inline-block size-4 rounded-full bg-background shadow-sm"
|
||||
animate={{ x: value ? 22 : 4 }}
|
||||
transition={{ type: 'spring', stiffness: 500, damping: 30 }}
|
||||
/>
|
||||
</button>
|
||||
);
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user