feat(webui): redesign adapter edit dialog (Apple-HIG) + custom dropdown

Rework the OneBot adapter create/edit modal into an iOS grouped-list
layout matching the debug-page language: captioned soft-card sections,
a per-kind icon chip, settings rows, and staggered motion entrance.

- reportSelfMessage: segmented control -> shared ToggleSwitch
- messageFormat (+ WS role): segmented/native select -> new custom
  DropdownSelect (accessible listbox, keyboard nav, spring popover)
- enable toggle promoted to a prominent top card
- new components/ui/dropdown-select.tsx (zero new deps, hand-rolled)

Presentation-only; draft/validation/data flow unchanged.
This commit is contained in:
DrSmoothl committed 2026-06-28 01:14:37 +08:00
1 parent e1f5267764
commit 04591517fa
2 files changed
+313 -192

No files matched your search

@@ -2,9 +2,24 @@
// 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.
//
// Visual language: Apple-HIG grouped-list flavour (matches debug-page) —
// captioned sections, soft rounded cards, settings rows with a label on the
// left and a control (toggle / custom dropdown) on the right.
import { useRef, useState } from 'react';
import { Check, Copy, Eye, EyeOff } from 'lucide-react';
import { useRef, useState, type ReactNode } from 'react';
import { motion } from 'motion/react';
import {
ArrowLeftRight,
ArrowUpRight,
Check,
Copy,
Eye,
EyeOff,
Radio,
Server,
type LucideIcon,
} from 'lucide-react';
import { Button } from '@/components/ui/button';
import {
Dialog,
@@ -14,9 +29,10 @@ import {
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import { DropdownSelect, type DropdownOption } from '@/components/ui/dropdown-select';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Select } from '@/components/ui/select';
import { ToggleSwitch } from '@/components/ui/toggle-switch';
import { cn } from '@/lib/utils';
import type {
HttpClientNetwork,
@@ -32,6 +48,13 @@ import { NETWORK_TABS } from './defaults';
type AnyAdapter<K extends NetworkKind> = OneBotNetworks[K][number];
const KIND_ICON: Record<NetworkKind, LucideIcon> = {
httpServers: Server,
httpClients: ArrowUpRight,
wsServers: Radio,
wsClients: ArrowLeftRight,
};
interface NodeEditDialogProps<K extends NetworkKind> {
open: boolean;
onOpenChange: (open: boolean) => void;
@@ -49,6 +72,7 @@ interface NodeEditDialogProps<K extends NetworkKind> {
export function NodeEditDialog<K extends NetworkKind>(props: NodeEditDialogProps<K>) {
const { open, onOpenChange, kind, initial, isEdit, otherNames, onSubmit } = props;
const tab = NETWORK_TABS[kind];
const Icon: LucideIcon = KIND_ICON[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
@@ -63,62 +87,101 @@ export function NodeEditDialog<K extends NetworkKind>(props: NodeEditDialogProps
const patch = (changes: Partial<AnyAdapter<K>>) => setDraft({ ...draft, ...changes } as AnyAdapter<K>);
const isWs = kind === 'wsServers' || kind === 'wsClients';
const role = ((draft as WsServerNetwork | WsClientNetwork).role ?? 'Universal') as WsRole;
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="max-w-xl">
<DialogHeader>
<DialogTitle>{isEdit ? `编辑 ${tab.noun}` : `新建 ${tab.noun}`}</DialogTitle>
<DialogDescription>{tab.description}</DialogDescription>
<div className="flex items-center gap-3">
<div className="flex size-11 shrink-0 items-center justify-center rounded-2xl bg-primary/10 text-primary">
<Icon className="size-5" />
</div>
<div className="min-w-0">
<DialogTitle>{isEdit ? `编辑 ${tab.noun}` : `新建 ${tab.noun}`}</DialogTitle>
<DialogDescription className="mt-0.5">{tab.description}</DialogDescription>
</div>
</div>
</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} />
<TokenField
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">
<div className="flex flex-col gap-5">
<motion.div
initial={{ opacity: 0, y: 8 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.3, delay: 0 }}
className="flex items-center justify-between gap-4 rounded-2xl border border-border/60 bg-card/60 px-4 py-3.5 shadow-[0_1px_2px_rgb(0_0_0/0.04)]"
>
<div className="min-w-0">
<Label className="text-sm font-medium text-foreground">启用</Label>
<p className="mt-0.5 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>>)
}
onChange={(v) => patch({ enabled: v ? undefined : false } as Partial<AnyAdapter<K>>)}
ariaLabel="启用"
/>
</div>
</motion.div>
<Section caption="连接" delay={0.05}>
<div className="flex flex-col gap-3 p-4">
<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} />
</div>
</Section>
<Section caption="鉴权" delay={0.1}>
<div className="p-4">
<TokenField
label="授权 Token"
placeholder="不填则无密码"
value={draft.accessToken}
onChange={(v) => patch({ accessToken: v || undefined } as Partial<AnyAdapter<K>>)}
/>
</div>
</Section>
<Section caption="行为" delay={0.15}>
<div className="divide-y divide-border/60">
<SettingRow label="消息格式" desc="数组为标准 OneBot 段,CQ 码为兼容字符串">
<DropdownSelect
className="w-32"
ariaLabel="消息格式"
value={(draft.messageFormat ?? 'array') as MessageFormat}
options={FORMAT_OPTIONS}
onChange={(v) => patch({ messageFormat: v } as Partial<AnyAdapter<K>>)}
/>
</SettingRow>
{isWs && (
<SettingRow label="角色" desc="Universal 收发合一,Event / Api 分离">
<DropdownSelect
className="w-32"
ariaLabel="角色"
value={role}
options={WS_ROLE_OPTIONS}
onChange={(v) => patch({ role: v } as unknown as Partial<AnyAdapter<K>>)}
/>
</SettingRow>
)}
<SettingRow label="上报自身消息" desc="将机器人自己发送的消息也作为 message_sent 事件上报">
<ToggleSwitch
value={!!draft.reportSelfMessage}
onChange={(v) => patch({ reportSelfMessage: v } as Partial<AnyAdapter<K>>)}
ariaLabel="上报自身消息"
/>
</SettingRow>
</div>
</Section>
</div>
<DialogFooter>
@@ -141,6 +204,36 @@ export function NodeEditDialog<K extends NetworkKind>(props: NodeEditDialogProps
);
}
// ─────────────── grouped-list scaffolding ───────────────
/** Captioned inset section — the iOS "grouped list" unit. */
function Section({ caption, delay, children }: { caption: string; delay: number; children: ReactNode }) {
return (
<motion.section
initial={{ opacity: 0, y: 8 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.3, delay }}
className="flex flex-col gap-1.5"
>
<span className="px-1 text-[11px] font-medium text-muted-foreground">{caption}</span>
<div className="overflow-hidden rounded-2xl border border-border/60 bg-card/40">{children}</div>
</motion.section>
);
}
/** Settings row — label (+ optional subtitle) on the left, control on the right. */
function SettingRow({ label, desc, children }: { label: string; desc?: string; children: ReactNode }) {
return (
<div className="flex items-center justify-between gap-4 px-4 py-3">
<div className="min-w-0">
<Label className="text-[13px] text-foreground">{label}</Label>
{desc && <p className="mt-0.5 text-[11px] leading-relaxed text-muted-foreground">{desc}</p>}
</div>
<div className="shrink-0">{children}</div>
</div>
);
}
// ─────────────── kind-specific field strips ───────────────
interface KindFieldsProps<K extends NetworkKind> {
@@ -151,7 +244,8 @@ interface KindFieldsProps<K extends NetworkKind> {
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.
// each arm casts once. Field components receive specific shapes. The WS
// `role` lives in the behaviour section, so it is intentionally absent here.
if (kind === 'httpServers') {
const it = draft as HttpServerNetwork;
const set = patch as (c: Partial<HttpServerNetwork>) => void;
@@ -204,41 +298,33 @@ function KindFields<K extends NetworkKind>({ kind, draft, patch }: KindFieldsPro
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 })}
<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 === 'wsClients') {
const it = draft as WsClientNetwork;
const set = patch as (c: Partial<WsClientNetwork>) => void;
return (
<>
<div className="grid gap-3 sm:grid-cols-[1fr_160px]">
<Field
label="目标 URL"
type="url"
@@ -246,21 +332,13 @@ function KindFields<K extends NetworkKind>({ kind, draft, patch }: KindFieldsPro
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>
</>
<Field
label="重连间隔 (ms)"
type="number"
value={it.reconnectIntervalMs}
onChange={(v) => set({ reconnectIntervalMs: Number(v) || undefined })}
/>
</div>
);
}
return null;
@@ -393,112 +471,12 @@ function TokenField({ label, value, onChange, placeholder }: TokenFieldProps) {
);
}
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)}>
{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 ? 'true' : 'false'}
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>> = [
const FORMAT_OPTIONS: ReadonlyArray<DropdownOption<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>> = [
const WS_ROLE_OPTIONS: ReadonlyArray<DropdownOption<WsRole>> = [
{ value: 'Universal', label: 'Universal' },
{ value: 'Event', label: 'Event' },
{ value: 'Api', label: 'Api' },
@@ -0,0 +1,143 @@
import { useEffect, useRef, useState } from 'react';
import { AnimatePresence, motion } from 'motion/react';
import { Check, ChevronDown } from 'lucide-react';
import { cn } from '@/lib/utils';
export interface DropdownOption<T extends string> {
value: T;
label: string;
}
interface DropdownSelectProps<T extends string> {
value: T;
options: ReadonlyArray<DropdownOption<T>>;
onChange: (v: T) => void;
ariaLabel: string;
className?: string;
}
/**
* Custom (non-native) dropdown with an Apple-HIG flavour: a calm trigger that
* mirrors {@link Input}'s height/border, a soft floating menu that springs in,
* and a trailing check on the selected row. Keyboard-driven (arrows / enter /
* escape) and closes on outside pointer-down. Use when the design wants a menu
* that matches the surrounding chrome — the plain `Select` stays the zero-dep
* native fallback for dense forms.
*/
export function DropdownSelect<T extends string>({
value,
options,
onChange,
ariaLabel,
className,
}: DropdownSelectProps<T>) {
const [open, setOpen] = useState(false);
const [active, setActive] = useState(0);
const rootRef = useRef<HTMLDivElement>(null);
const selected = options.find((o) => o.value === value);
useEffect(() => {
if (!open) return;
// Seed the keyboard highlight on the current selection each time we open.
setActive(Math.max(0, options.findIndex((o) => o.value === value)));
const onPointer = (e: PointerEvent) => {
if (rootRef.current && !rootRef.current.contains(e.target as Node)) setOpen(false);
};
document.addEventListener('pointerdown', onPointer);
return () => document.removeEventListener('pointerdown', onPointer);
}, [open]); // eslint-disable-line react-hooks/exhaustive-deps
const choose = (v: T) => {
onChange(v);
setOpen(false);
};
const onKeyDown = (e: React.KeyboardEvent) => {
if (e.key === 'Escape' && open) {
// Swallow so the surrounding Dialog doesn't also close on the same key.
e.preventDefault();
e.stopPropagation();
setOpen(false);
return;
}
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
e.preventDefault();
if (!open) {
setOpen(true);
return;
}
setActive((i) => {
const n = options.length;
return e.key === 'ArrowDown' ? (i + 1) % n : (i - 1 + n) % n;
});
return;
}
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
if (!open) setOpen(true);
else if (options[active]) choose(options[active].value);
}
};
return (
<div ref={rootRef} className={cn('relative', className)}>
<button
type="button"
aria-haspopup="listbox"
aria-expanded={open}
aria-label={ariaLabel}
onClick={() => setOpen((o) => !o)}
onKeyDown={onKeyDown}
className={cn(
'flex h-9 w-full items-center justify-between gap-2 rounded-lg border border-border bg-transparent px-3 text-sm shadow-xs outline-none transition-[color,box-shadow]',
'hover:bg-accent/40 focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/40',
)}
>
<span className="truncate">{selected?.label ?? ''}</span>
<ChevronDown
className={cn(
'size-4 shrink-0 text-muted-foreground transition-transform duration-200',
open && 'rotate-180',
)}
/>
</button>
<AnimatePresence>
{open && (
<motion.ul
role="listbox"
aria-label={ariaLabel}
initial={{ opacity: 0, y: -4, scale: 0.97 }}
animate={{ opacity: 1, y: 0, scale: 1 }}
exit={{ opacity: 0, y: -4, scale: 0.97 }}
transition={{ type: 'spring', stiffness: 600, damping: 38 }}
className="absolute inset-x-0 top-[calc(100%+4px)] z-50 origin-top overflow-hidden rounded-xl border border-border/60 bg-card p-1 shadow-lg backdrop-blur-sm"
>
{options.map((o, i) => {
const isSel = o.value === value;
const isActive = i === active;
return (
<li key={o.value} role="option" aria-selected={isSel}>
<button
type="button"
tabIndex={-1}
onMouseEnter={() => setActive(i)}
onClick={() => choose(o.value)}
className={cn(
'flex w-full items-center justify-between gap-2 rounded-lg px-2.5 py-1.5 text-left text-sm transition-colors',
isActive ? 'bg-accent text-accent-foreground' : 'text-foreground',
)}
>
<span className="truncate">{o.label}</span>
{isSel && <Check className="size-4 shrink-0 text-primary" />}
</button>
</li>
);
})}
</motion.ul>
)}
</AnimatePresence>
</div>
);
}