refactor(webui): 移除冗余的手动保存按钮喵,通用设置改为 debounce 自动保存喵 (#83)

* refactor(webui): 移除冗余的手动保存按钮喵,通用设置改为 debounce 自动保存喵

* fix(webui): 修复保存按钮状态喵,允许在自动保存时手动触发喵
This commit is contained in:
ikun两年半
2026-06-04 18:03:17 +08:00
committed by GitHub
parent 0cc7ef2f30
commit 34ea2ce438
2 changed files with 32 additions and 25 deletions
@@ -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';
@@ -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<number | null>(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<K extends NetworkKind>(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() {
/>
)}
<ConfirmDialog
open={confirmSave}
onOpenChange={setConfirmSave}
title="保存配置变更?"
description={`即将把当前修改保存到 UIN ${selectedUin ?? ''} 的配置文件,并尝试热重载该会话。`}
confirmText="保存"
onConfirm={save}
/>
<ConfirmDialog
open={pendingSwitchUin != null}
onOpenChange={(open) => !open && cancelSwitch()}
@@ -285,7 +292,7 @@ function HeaderBar({ selectedUin, dirty, saveStatus, onSave, activeTab, onCreate
新建{activeTab === 'general' ? '' : NETWORK_TABS[activeTab as NetworkKind].title}
</Button>
)}
<Button onClick={onSave} size="sm" disabled={!dirty}>
<Button onClick={onSave} size="sm">
<Save className="size-3.5" /> 保存
</Button>
</div>