Five WebUI changes: 1. Settings page rebuilt around mini tabs (外观 / 数据刷新 / 账号安全) instead of three stacked cards; content and behaviour unchanged. 2. "修改访问密码" now opens an independent ChangePasswordDialog modal rather than swapping the whole page for the forced-change screen. The form is extracted to a shared ChangePasswordForm used by both the modal and the forced full-screen ChangePasswordPage — behaviour identical. 3. Node config: creating / editing / deleting / enable-toggling an HTTP/WS endpoint now persists immediately — clicking 保存 in the editor dialog IS the save. Previously it only marked the config dirty until you also hit the top-right 保存, which silently cost many users their token edits. `save()` takes an explicit config so it persists the freshly computed value, not the stale closed-over state. The general-settings tab keeps its explicit top-right save. 4. Forced first-time change-password no longer renders an old-password field: it carries the password the user just logged in with. Browsers (Edge/Chrome) were autofilling that field on upgrade, so users thought the old password was pre-filled correctly and could never save. A returning already-authed session (no fresh login) still shows the field. 5. Added default error + 404 screens (status-screens.tsx), wired into the TanStack router via defaultErrorComponent / defaultNotFoundComponent.
155 lines
5.1 KiB
TypeScript
155 lines
5.1 KiB
TypeScript
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
import type { OneBotConfig, QQInfo } from '@/types';
|
|
import { useApi } from '@/lib/api';
|
|
|
|
/**
|
|
* Per-UIN OneBotInstance configuration editor. Owns the full lifecycle:
|
|
* selection, load, in-memory edit (with dirty tracking), save (with refetch),
|
|
* and a "switch UIN while dirty" guard that surfaces a pending switch the
|
|
* view can confirm or cancel.
|
|
*/
|
|
export interface UseOneBotInstanceConfig {
|
|
selectedUin: string | null;
|
|
/** Loaded config for the current UIN. Null while loading or before a selection. */
|
|
config: OneBotConfig | null;
|
|
setConfig: (next: OneBotConfig) => void;
|
|
/** True if the in-memory config diverges from the last server-confirmed snapshot. */
|
|
dirty: boolean;
|
|
/**
|
|
* Request a UIN switch. If currently dirty, the switch is parked as
|
|
* `pendingSwitchUin` and must be confirmed or cancelled. Otherwise it
|
|
* applies immediately.
|
|
*/
|
|
requestSwitchUin: (uin: string) => void;
|
|
/** Non-null while a switch is parked waiting for confirmation. */
|
|
pendingSwitchUin: string | null;
|
|
confirmSwitch: () => void;
|
|
cancelSwitch: () => void;
|
|
/**
|
|
* Persist to the backend. Pass an explicit config to save it directly —
|
|
* the node dialog / enable-toggle / delete persist their freshly-computed
|
|
* config in the same tick, before React has flushed `setConfig`, so they
|
|
* can't rely on the (still-stale) closed-over `config`.
|
|
*/
|
|
save: (override?: OneBotConfig) => Promise<void>;
|
|
saveStatus: string;
|
|
}
|
|
|
|
const CLEAR_SAVE_STATUS_MS = 3000;
|
|
|
|
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, onSelectedUinChange } = options;
|
|
const [config, setConfigState] = useState<OneBotConfig | null>(null);
|
|
const [savedSnapshot, setSavedSnapshot] = useState<string | null>(null);
|
|
const [saveStatus, setSaveStatus] = useState('');
|
|
const [pendingSwitchUin, setPendingSwitchUin] = useState<string | null>(null);
|
|
const clearTimerRef = useRef<number | null>(null);
|
|
|
|
// Auto-select first account when none is selected yet.
|
|
useEffect(() => {
|
|
if (!selectedUin && accounts.length > 0) onSelectedUinChange(accounts[0].uin);
|
|
}, [accounts, selectedUin, onSelectedUinChange]);
|
|
|
|
// Load on UIN change. The api client already runs normalizeOneBotConfig.
|
|
useEffect(() => {
|
|
if (!selectedUin) {
|
|
setConfigState(null);
|
|
setSavedSnapshot(null);
|
|
return;
|
|
}
|
|
let cancelled = false;
|
|
(async () => {
|
|
try {
|
|
const loaded = await api.config.get(selectedUin);
|
|
if (cancelled) return;
|
|
setConfigState(loaded);
|
|
setSavedSnapshot(JSON.stringify(loaded));
|
|
} catch (e) {
|
|
console.error('load-config', e);
|
|
}
|
|
})();
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [selectedUin, api]);
|
|
|
|
useEffect(
|
|
() => () => {
|
|
if (clearTimerRef.current != null) window.clearTimeout(clearTimerRef.current);
|
|
},
|
|
[],
|
|
);
|
|
|
|
const dirty = useMemo(() => {
|
|
if (config == null || savedSnapshot == null) return false;
|
|
return JSON.stringify(config) !== savedSnapshot;
|
|
}, [config, savedSnapshot]);
|
|
|
|
const setConfig = useCallback((next: OneBotConfig) => setConfigState(next), []);
|
|
|
|
const requestSwitchUin = useCallback(
|
|
(uin: string) => {
|
|
if (uin === selectedUin) return;
|
|
if (dirty) setPendingSwitchUin(uin);
|
|
else onSelectedUinChange(uin);
|
|
},
|
|
[dirty, selectedUin, onSelectedUinChange],
|
|
);
|
|
|
|
const confirmSwitch = useCallback(() => {
|
|
if (pendingSwitchUin == null) return;
|
|
onSelectedUinChange(pendingSwitchUin);
|
|
setPendingSwitchUin(null);
|
|
}, [pendingSwitchUin, onSelectedUinChange]);
|
|
|
|
const cancelSwitch = useCallback(() => setPendingSwitchUin(null), []);
|
|
|
|
const scheduleStatusClear = useCallback(() => {
|
|
if (clearTimerRef.current != null) window.clearTimeout(clearTimerRef.current);
|
|
clearTimerRef.current = window.setTimeout(() => {
|
|
setSaveStatus('');
|
|
clearTimerRef.current = null;
|
|
}, CLEAR_SAVE_STATUS_MS);
|
|
}, []);
|
|
|
|
const save = useCallback(async (override?: OneBotConfig) => {
|
|
const target = override ?? config;
|
|
if (!selectedUin || !target) return;
|
|
setSaveStatus('保存中...');
|
|
try {
|
|
const serverView = await api.config.save(selectedUin, target);
|
|
setConfigState(serverView);
|
|
setSavedSnapshot(JSON.stringify(serverView));
|
|
setSaveStatus('保存成功');
|
|
} catch (e) {
|
|
setSaveStatus(`保存失败:${e instanceof Error ? e.message : '未知错误'}`);
|
|
} finally {
|
|
scheduleStatusClear();
|
|
}
|
|
}, [api, selectedUin, config, scheduleStatusClear]);
|
|
|
|
return {
|
|
selectedUin,
|
|
config,
|
|
setConfig,
|
|
dirty,
|
|
requestSwitchUin,
|
|
pendingSwitchUin,
|
|
confirmSwitch,
|
|
cancelSwitch,
|
|
save,
|
|
saveStatus,
|
|
};
|
|
}
|