fix(webui): preserve config edits on save and UIN choice across tabs
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.
This commit is contained in:
4 files changed
+38
-18
No files matched your search
@@ -122,6 +122,9 @@ function AppInner({ status, onLogoutComplete }: AppInnerProps) {
|
||||
const [qqList, setQqList] = useState<QQInfo[]>([]);
|
||||
const [processList, setProcessList] = useState<HookProcessInfo[]>([]);
|
||||
const [systemInfo, setSystemInfo] = useState<SystemInfo | null>(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<string | null>(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' && <ConfigPage qqList={qqList} />}
|
||||
{active === 'config' && (
|
||||
<ConfigPage
|
||||
qqList={qqList}
|
||||
selectedUin={selectedUin}
|
||||
onSelectedUinChange={setSelectedUin}
|
||||
/>
|
||||
)}
|
||||
{active === 'logs' && <LogsPage />}
|
||||
{active === 'settings' && <SettingsPage onLogout={handleLogout} />}
|
||||
</MainLayout>
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<string | null>(null);
|
||||
const { selectedUin, onSelectedUinChange } = options;
|
||||
const [config, setConfigState] = useState<OneBotConfig | null>(null);
|
||||
const [savedSnapshot, setSavedSnapshot] = useState<string | null>(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), []);
|
||||
|
||||
|
||||
@@ -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<unknown>(url, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(config),
|
||||
});
|
||||
const raw =
|
||||
typeof data === 'object' && data != null && 'config' in (data as Record<string, unknown>)
|
||||
? (data as { config: unknown }).config
|
||||
: data;
|
||||
return normalizeOneBotConfig(raw);
|
||||
return this.config.get(uin);
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user