diff --git a/packages/webui/src/components/config/general-settings-tab.tsx b/packages/webui/src/components/config/general-settings-tab.tsx index 93290374..727c1a30 100644 --- a/packages/webui/src/components/config/general-settings-tab.tsx +++ b/packages/webui/src/components/config/general-settings-tab.tsx @@ -1,7 +1,7 @@ // "通用设置" tab — fields that apply to the whole OneBotInstance rather // than any specific adapter: the music-sign service URL and the built-in -// `#sl` status command. Edits here mark the config dirty and are persisted -// via the page's explicit top-right 保存 (a continuously-edited surface). +// `#sl` status command. Edits here mark the config dirty and are +// auto-saved with debounce by the parent ConfigPage. import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; diff --git a/packages/webui/src/components/pages/config-page.tsx b/packages/webui/src/components/pages/config-page.tsx index 1fd66961..76701e0f 100644 --- a/packages/webui/src/components/pages/config-page.tsx +++ b/packages/webui/src/components/pages/config-page.tsx @@ -3,11 +3,10 @@ // Layout: collapsible left sidebar for account selection + tabbed right // pane (通用 / 4 network kinds). Each network tab is a list view of // summary cards with inline enable/disable; create + edit both go -// through `NodeEditDialog`. The dirty-modify guard from the original -// hook is preserved end-to-end — switching accounts or kinds doesn't -// drop unsaved changes silently. +// through `NodeEditDialog`. All changes auto-save: network mutations +// persist immediately; general-settings edits are debounced (500 ms). -import { useMemo, useState } from 'react'; +import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { motion } from 'motion/react'; import { MousePointerClick, Plus, Save } from 'lucide-react'; import { Button } from '@/components/ui/button'; @@ -62,7 +61,6 @@ export function ConfigPage() { const [sidebarCollapsed, setSidebarCollapsed] = useState( () => typeof window !== 'undefined' && window.matchMedia('(max-width: 1024px)').matches, ); - const [confirmSave, setConfirmSave] = useState(false); // The edit dialog is modal and blocks every other click in the page, // so `selectedUin` cannot change while it's open — no defensive close // wiring needed beyond the dialog's own open/close. @@ -82,13 +80,31 @@ export function ConfigPage() { 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 - // two-step trap where editing a token in the dialog only marked the config - // "dirty" until you also pressed the top-right 保存, which silently cost - // many users their token edits. The general-settings tab keeps its explicit - // top-right save (it's a continuously-edited free-form surface). + // Auto-save general settings with debounce. Network mutations + // (create / edit / delete / enable-toggle) persist immediately in commitKind. + const debounceRef = useRef(null); + useEffect(() => { + if (debounceRef.current != null) window.clearTimeout(debounceRef.current); + if (dirty) { + debounceRef.current = window.setTimeout(() => { + void save(); + debounceRef.current = null; + }, 500); + } + return () => { + if (debounceRef.current != null) window.clearTimeout(debounceRef.current); + }; + }, [dirty, config, save]); + + /** Immediate save: clear any pending auto-save debounce and persist now. */ + const immediateSave = useCallback(() => { + if (debounceRef.current != null) { + window.clearTimeout(debounceRef.current); + debounceRef.current = null; + } + void save(); + }, [save]); + function commitKind(kind: K, nextList: OneBotNetworks[K]): void { if (!config) return; const next = { ...config, networks: { ...config.networks, [kind]: nextList } }; @@ -161,7 +177,7 @@ export function ConfigPage() { selectedUin={selectedUin} dirty={dirty} saveStatus={saveStatus} - onSave={() => setConfirmSave(true)} + onSave={immediateSave} activeTab={activeTab} onCreate={ activeTab !== 'general' ? () => openCreate(activeTab as NetworkKind) : undefined @@ -207,15 +223,6 @@ export function ConfigPage() { /> )} - - !open && cancelSwitch()} @@ -285,7 +292,7 @@ function HeaderBar({ selectedUin, dirty, saveStatus, onSave, activeTab, onCreate 新建{activeTab === 'general' ? '' : NETWORK_TABS[activeTab as NetworkKind].title} )} -