From caf724b25c35ad3aad23d2160b821fd7d2038b9f Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Sat, 16 May 2026 02:22:48 +0800 Subject: [PATCH] fix(webui): preserve config edits on save and UIN choice across tabs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two regressions slipped into the ApiClient/ConfigPage refactor: 1. api.config.save() ran the POST response through normalizeOneBotConfig and used it to overwrite local state. But POST /api/config/:uin returns { success, reloaded, message } — no config body — so the normaliser produced an empty config and wiped the user's edits as soon as the save succeeded. Honour the "save returns canonical view" contract by following the POST with a GET inside the client; the round-trip stays hidden behind the seam. 2. selectedUin lived inside useOneBotInstanceConfig, so navigating away from ConfigPage unmounted the hook and lost the user's selection. Lift selectedUin to AppInner and make the hook externally controlled via { selectedUin, onSelectedUinChange }. Clear it on logout. --- packages/webui/src/App.tsx | 12 ++++++++- .../src/components/pages/config-page.tsx | 7 ++--- .../src/hooks/use-onebot-instance-config.ts | 26 +++++++++++++------ packages/webui/src/lib/api/client.ts | 11 ++++---- 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/packages/webui/src/App.tsx b/packages/webui/src/App.tsx index 14e8b49c..05df2512 100644 --- a/packages/webui/src/App.tsx +++ b/packages/webui/src/App.tsx @@ -122,6 +122,9 @@ function AppInner({ status, onLogoutComplete }: AppInnerProps) { const [qqList, setQqList] = useState([]); const [processList, setProcessList] = useState([]); const [systemInfo, setSystemInfo] = useState(null); + // Lives at AppInner level so the user's selection survives across page + // navigation (ConfigPage gets unmounted when you switch tabs). + const [selectedUin, setSelectedUin] = useState(null); const refreshQqList = useCallback(async () => { try { @@ -171,6 +174,7 @@ function AppInner({ status, onLogoutComplete }: AppInnerProps) { setQqList([]); setProcessList([]); setSystemInfo(null); + setSelectedUin(null); onLogoutComplete(); }, [api, onLogoutComplete]); @@ -188,7 +192,13 @@ function AppInner({ status, onLogoutComplete }: AppInnerProps) { processOps={processOps} /> )} - {active === 'config' && } + {active === 'config' && ( + + )} {active === 'logs' && } {active === 'settings' && } diff --git a/packages/webui/src/components/pages/config-page.tsx b/packages/webui/src/components/pages/config-page.tsx index 71076e57..729db9f2 100644 --- a/packages/webui/src/components/pages/config-page.tsx +++ b/packages/webui/src/components/pages/config-page.tsx @@ -23,6 +23,8 @@ import { useOneBotInstanceConfig } from '@/hooks/use-onebot-instance-config'; interface ConfigPageProps { qqList: QQInfo[]; + selectedUin: string | null; + onSelectedUinChange: (uin: string | null) => void; } function qqAvatarUrl(uin: string) { @@ -93,9 +95,8 @@ function Section({ title, description, onAdd, children, count }: SectionProps) { ); } -export function ConfigPage({ qqList }: ConfigPageProps) { +export function ConfigPage({ qqList, selectedUin, onSelectedUinChange }: ConfigPageProps) { const { - selectedUin, config, setConfig, dirty, @@ -105,7 +106,7 @@ export function ConfigPage({ qqList }: ConfigPageProps) { cancelSwitch, save, saveStatus, - } = useOneBotInstanceConfig(qqList); + } = useOneBotInstanceConfig(qqList, { selectedUin, onSelectedUinChange }); const [confirmSave, setConfirmSave] = useState(false); const update = (next: OneBotConfig) => setConfig(next); diff --git a/packages/webui/src/hooks/use-onebot-instance-config.ts b/packages/webui/src/hooks/use-onebot-instance-config.ts index 99b44668..dc4b2cd0 100644 --- a/packages/webui/src/hooks/use-onebot-instance-config.ts +++ b/packages/webui/src/hooks/use-onebot-instance-config.ts @@ -31,9 +31,19 @@ export interface UseOneBotInstanceConfig { const CLEAR_SAVE_STATUS_MS = 3000; -export function useOneBotInstanceConfig(accounts: QQInfo[]): UseOneBotInstanceConfig { +export interface UseOneBotInstanceConfigOptions { + /** Externally-owned current UIN. The hook reads it but does not own it. */ + selectedUin: string | null; + /** Called when the hook wants to mutate selection (auto-select / confirmed switch). */ + onSelectedUinChange: (uin: string | null) => void; +} + +export function useOneBotInstanceConfig( + accounts: QQInfo[], + options: UseOneBotInstanceConfigOptions, +): UseOneBotInstanceConfig { const api = useApi(); - const [selectedUin, setSelectedUin] = useState(null); + const { selectedUin, onSelectedUinChange } = options; const [config, setConfigState] = useState(null); const [savedSnapshot, setSavedSnapshot] = useState(null); const [saveStatus, setSaveStatus] = useState(''); @@ -42,8 +52,8 @@ export function useOneBotInstanceConfig(accounts: QQInfo[]): UseOneBotInstanceCo // Auto-select first account when none is selected yet. useEffect(() => { - if (!selectedUin && accounts.length > 0) setSelectedUin(accounts[0].uin); - }, [accounts, selectedUin]); + if (!selectedUin && accounts.length > 0) onSelectedUinChange(accounts[0].uin); + }, [accounts, selectedUin, onSelectedUinChange]); // Load on UIN change. The api client already runs normalizeOneBotConfig. useEffect(() => { @@ -86,16 +96,16 @@ export function useOneBotInstanceConfig(accounts: QQInfo[]): UseOneBotInstanceCo (uin: string) => { if (uin === selectedUin) return; if (dirty) setPendingSwitchUin(uin); - else setSelectedUin(uin); + else onSelectedUinChange(uin); }, - [dirty, selectedUin], + [dirty, selectedUin, onSelectedUinChange], ); const confirmSwitch = useCallback(() => { if (pendingSwitchUin == null) return; - setSelectedUin(pendingSwitchUin); + onSelectedUinChange(pendingSwitchUin); setPendingSwitchUin(null); - }, [pendingSwitchUin]); + }, [pendingSwitchUin, onSelectedUinChange]); const cancelSwitch = useCallback(() => setPendingSwitchUin(null), []); diff --git a/packages/webui/src/lib/api/client.ts b/packages/webui/src/lib/api/client.ts index 156197d2..b9fafc63 100644 --- a/packages/webui/src/lib/api/client.ts +++ b/packages/webui/src/lib/api/client.ts @@ -67,15 +67,14 @@ class HttpApiClient implements ApiClient { }, save: async (uin, config) => { const url = `/api/config/${encodeURIComponent(uin)}`; - const data = await this.fetchJson<{ config?: unknown } | unknown>(url, { + // POST returns { success, reloaded, message } — no config body. To + // honour the "save returns canonical server view" contract, refetch + // after a successful POST. + await this.fetchJson(url, { method: 'POST', body: JSON.stringify(config), }); - const raw = - typeof data === 'object' && data != null && 'config' in (data as Record) - ? (data as { config: unknown }).config - : data; - return normalizeOneBotConfig(raw); + return this.config.get(uin); }, };