feat(webui): standardize content loading states
Use consistent delayed placeholders across data-backed console surfaces. Avoid flashing loading UI for fast local responses while keeping slower transitions stable. Keep failures visible and prevent gated content from opening on incomplete checks.
This commit is contained in:
22 files changed
+2493
-1527
No files matched your search
@@ -13,6 +13,9 @@ export interface UseOneBotInstanceConfig {
|
||||
selectedUin: string | null;
|
||||
/** Loaded config for the current UIN. Null while loading or before a selection. */
|
||||
config: OneBotConfig | null;
|
||||
loading: boolean;
|
||||
loadError: string | null;
|
||||
reload: () => void;
|
||||
setConfig: (next: OneBotConfig) => void;
|
||||
/** True if the in-memory config diverges from the last server-confirmed snapshot. */
|
||||
dirty: boolean;
|
||||
@@ -54,6 +57,10 @@ export function useOneBotInstanceConfig(
|
||||
const { runAction } = useActionFeedback();
|
||||
const { selectedUin, onSelectedUinChange } = options;
|
||||
const [config, setConfigState] = useState<OneBotConfig | null>(null);
|
||||
const [loadedUin, setLoadedUin] = useState<string | null>(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [loadError, setLoadError] = useState<string | null>(null);
|
||||
const [reloadGeneration, setReloadGeneration] = useState(0);
|
||||
const [savedSnapshot, setSavedSnapshot] = useState<string | null>(null);
|
||||
const [saveStatus, setSaveStatus] = useState('');
|
||||
const [saveStatusTone, setSaveStatusTone] = useState<UseOneBotInstanceConfig['saveStatusTone']>('idle');
|
||||
@@ -80,10 +87,18 @@ export function useOneBotInstanceConfig(
|
||||
setSaveStatus('');
|
||||
setSaveStatusTone('idle');
|
||||
if (!selectedUin) {
|
||||
setLoading(false);
|
||||
setLoadError(null);
|
||||
setConfigState(null);
|
||||
setLoadedUin(null);
|
||||
setSavedSnapshot(null);
|
||||
return;
|
||||
}
|
||||
setLoading(true);
|
||||
setLoadError(null);
|
||||
setConfigState(null);
|
||||
setLoadedUin(null);
|
||||
setSavedSnapshot(null);
|
||||
let cancelled = false;
|
||||
(async () => {
|
||||
try {
|
||||
@@ -91,15 +106,21 @@ export function useOneBotInstanceConfig(
|
||||
if (cancelled) return;
|
||||
editRevisionRef.current += 1;
|
||||
setConfigState(loaded);
|
||||
setLoadedUin(selectedUin);
|
||||
setSavedSnapshot(JSON.stringify(loaded));
|
||||
} catch (e) {
|
||||
console.error('load-config', e);
|
||||
if (!cancelled) {
|
||||
setLoadError(e instanceof Error ? e.message : '加载账号配置失败');
|
||||
}
|
||||
} finally {
|
||||
if (!cancelled) setLoading(false);
|
||||
}
|
||||
})();
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [selectedUin, api]);
|
||||
}, [selectedUin, api, reloadGeneration]);
|
||||
|
||||
useEffect(
|
||||
() => () => {
|
||||
@@ -108,15 +129,20 @@ export function useOneBotInstanceConfig(
|
||||
[],
|
||||
);
|
||||
|
||||
const currentConfig = loadedUin === selectedUin ? config : null;
|
||||
const currentLoading = selectedUin !== null
|
||||
&& (loading || (loadError === null && currentConfig === null));
|
||||
|
||||
const dirty = useMemo(() => {
|
||||
if (config == null || savedSnapshot == null) return false;
|
||||
return JSON.stringify(config) !== savedSnapshot;
|
||||
}, [config, savedSnapshot]);
|
||||
if (currentConfig == null || savedSnapshot == null) return false;
|
||||
return JSON.stringify(currentConfig) !== savedSnapshot;
|
||||
}, [currentConfig, savedSnapshot]);
|
||||
|
||||
const setConfig = useCallback((next: OneBotConfig) => {
|
||||
editRevisionRef.current += 1;
|
||||
setConfigState(next);
|
||||
}, []);
|
||||
const reload = useCallback(() => setReloadGeneration((generation) => generation + 1), []);
|
||||
|
||||
const requestSwitchUin = useCallback(
|
||||
(uin: string) => {
|
||||
@@ -146,7 +172,7 @@ export function useOneBotInstanceConfig(
|
||||
}, []);
|
||||
|
||||
const save = useCallback(async (override?: OneBotConfig) => {
|
||||
const target = override ?? config;
|
||||
const target = override ?? currentConfig;
|
||||
if (!selectedUin || !target) return;
|
||||
const uin = selectedUin;
|
||||
const editRevision = editRevisionRef.current;
|
||||
@@ -211,11 +237,14 @@ export function useOneBotInstanceConfig(
|
||||
scheduleStatusClear(uin, generation);
|
||||
}
|
||||
}
|
||||
}, [api, selectedUin, config, scheduleStatusClear, runAction]);
|
||||
}, [api, selectedUin, currentConfig, scheduleStatusClear, runAction]);
|
||||
|
||||
return {
|
||||
selectedUin,
|
||||
config,
|
||||
config: currentConfig,
|
||||
loading: currentLoading,
|
||||
loadError,
|
||||
reload,
|
||||
setConfig,
|
||||
dirty,
|
||||
requestSwitchUin,
|
||||
|
||||
Reference in new issue
Block a user