refactor(webui): 移除冗余的手动保存按钮喵,通用设置改为 debounce 自动保存喵 (#83)
* refactor(webui): 移除冗余的手动保存按钮喵,通用设置改为 debounce 自动保存喵 * fix(webui): 修复保存按钮状态喵,允许在自动保存时手动触发喵
This commit is contained in:
1 parent
0cc7ef2f30
commit
34ea2ce438
2 files changed
+32
-25
No files matched your search
@@ -1,7 +1,7 @@
|
|||||||
// "通用设置" tab — fields that apply to the whole OneBotInstance rather
|
// "通用设置" tab — fields that apply to the whole OneBotInstance rather
|
||||||
// than any specific adapter: the music-sign service URL and the built-in
|
// 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
|
// `#sl` status command. Edits here mark the config dirty and are
|
||||||
// via the page's explicit top-right 保存 (a continuously-edited surface).
|
// auto-saved with debounce by the parent ConfigPage.
|
||||||
|
|
||||||
import { Input } from '@/components/ui/input';
|
import { Input } from '@/components/ui/input';
|
||||||
import { Label } from '@/components/ui/label';
|
import { Label } from '@/components/ui/label';
|
||||||
|
|||||||
@@ -3,11 +3,10 @@
|
|||||||
// Layout: collapsible left sidebar for account selection + tabbed right
|
// Layout: collapsible left sidebar for account selection + tabbed right
|
||||||
// pane (通用 / 4 network kinds). Each network tab is a list view of
|
// pane (通用 / 4 network kinds). Each network tab is a list view of
|
||||||
// summary cards with inline enable/disable; create + edit both go
|
// summary cards with inline enable/disable; create + edit both go
|
||||||
// through `NodeEditDialog`. The dirty-modify guard from the original
|
// through `NodeEditDialog`. All changes auto-save: network mutations
|
||||||
// hook is preserved end-to-end — switching accounts or kinds doesn't
|
// persist immediately; general-settings edits are debounced (500 ms).
|
||||||
// drop unsaved changes silently.
|
|
||||||
|
|
||||||
import { useMemo, useState } from 'react';
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||||
import { motion } from 'motion/react';
|
import { motion } from 'motion/react';
|
||||||
import { MousePointerClick, Plus, Save } from 'lucide-react';
|
import { MousePointerClick, Plus, Save } from 'lucide-react';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
@@ -62,7 +61,6 @@ export function ConfigPage() {
|
|||||||
const [sidebarCollapsed, setSidebarCollapsed] = useState(
|
const [sidebarCollapsed, setSidebarCollapsed] = useState(
|
||||||
() => typeof window !== 'undefined' && window.matchMedia('(max-width: 1024px)').matches,
|
() => 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,
|
// The edit dialog is modal and blocks every other click in the page,
|
||||||
// so `selectedUin` cannot change while it's open — no defensive close
|
// so `selectedUin` cannot change while it's open — no defensive close
|
||||||
// wiring needed beyond the dialog's own open/close.
|
// wiring needed beyond the dialog's own open/close.
|
||||||
@@ -82,13 +80,31 @@ export function ConfigPage() {
|
|||||||
return map;
|
return map;
|
||||||
}, [connections, selectedUin]);
|
}, [connections, selectedUin]);
|
||||||
|
|
||||||
// A discrete node mutation (create / edit / delete / enable-toggle) is
|
// Auto-save general settings with debounce. Network mutations
|
||||||
// persisted the moment it happens — clicking 保存 inside the editor dialog
|
// (create / edit / delete / enable-toggle) persist immediately in commitKind.
|
||||||
// (or flipping the enable switch) IS the save. This removes the old
|
const debounceRef = useRef<number | null>(null);
|
||||||
// two-step trap where editing a token in the dialog only marked the config
|
useEffect(() => {
|
||||||
// "dirty" until you also pressed the top-right 保存, which silently cost
|
if (debounceRef.current != null) window.clearTimeout(debounceRef.current);
|
||||||
// many users their token edits. The general-settings tab keeps its explicit
|
if (dirty) {
|
||||||
// top-right save (it's a continuously-edited free-form surface).
|
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 {
|
function commitKind<K extends NetworkKind>(kind: K, nextList: OneBotNetworks[K]): void {
|
||||||
if (!config) return;
|
if (!config) return;
|
||||||
const next = { ...config, networks: { ...config.networks, [kind]: nextList } };
|
const next = { ...config, networks: { ...config.networks, [kind]: nextList } };
|
||||||
@@ -161,7 +177,7 @@ export function ConfigPage() {
|
|||||||
selectedUin={selectedUin}
|
selectedUin={selectedUin}
|
||||||
dirty={dirty}
|
dirty={dirty}
|
||||||
saveStatus={saveStatus}
|
saveStatus={saveStatus}
|
||||||
onSave={() => setConfirmSave(true)}
|
onSave={immediateSave}
|
||||||
activeTab={activeTab}
|
activeTab={activeTab}
|
||||||
onCreate={
|
onCreate={
|
||||||
activeTab !== 'general' ? () => openCreate(activeTab as NetworkKind) : undefined
|
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
|
<ConfirmDialog
|
||||||
open={pendingSwitchUin != null}
|
open={pendingSwitchUin != null}
|
||||||
onOpenChange={(open) => !open && cancelSwitch()}
|
onOpenChange={(open) => !open && cancelSwitch()}
|
||||||
@@ -285,7 +292,7 @@ function HeaderBar({ selectedUin, dirty, saveStatus, onSave, activeTab, onCreate
|
|||||||
新建{activeTab === 'general' ? '' : NETWORK_TABS[activeTab as NetworkKind].title}
|
新建{activeTab === 'general' ? '' : NETWORK_TABS[activeTab as NetworkKind].title}
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
<Button onClick={onSave} size="sm" disabled={!dirty}>
|
<Button onClick={onSave} size="sm">
|
||||||
<Save className="size-3.5" /> 保存
|
<Save className="size-3.5" /> 保存
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in new issue
Block a user