feat(webui): show saved and applied config state
Surface whether a OneBot configuration was persisted, applied live, or left degraded. Keep failed adapters and apply errors visible in the config and overview pages instead of reporting a false success.
This commit is contained in:
7 files changed
+145
-24
No files matched your search
@@ -17,12 +17,14 @@ const LIVE_STYLE: Record<AdapterStatus['status'], string> = {
|
||||
ok: 'bg-success/10 text-success',
|
||||
warn: 'bg-warning/10 text-warning',
|
||||
down: 'bg-destructive/10 text-destructive',
|
||||
degraded: 'bg-destructive/10 text-destructive',
|
||||
disabled: 'bg-muted text-muted-foreground',
|
||||
};
|
||||
const LIVE_LABEL: Record<AdapterStatus['status'], string> = {
|
||||
ok: '正常',
|
||||
warn: '注意',
|
||||
down: '异常',
|
||||
degraded: '应用失败',
|
||||
disabled: '未启用',
|
||||
};
|
||||
|
||||
@@ -74,7 +76,9 @@ export function NodeSummaryCard<T extends AdapterCommon>({
|
||||
{liveStatus && (
|
||||
<span
|
||||
className={cn('rounded px-1.5 py-0.5 text-[10px] font-medium', LIVE_STYLE[liveStatus.status])}
|
||||
title={liveStatus.detail}
|
||||
title={liveStatus.lastError
|
||||
? `${liveStatus.detail}\n${liveStatus.lastErrorAt ? new Date(liveStatus.lastErrorAt).toLocaleString() : '时间未知'} · ${liveStatus.lastError}`
|
||||
: liveStatus.detail}
|
||||
>
|
||||
{LIVE_LABEL[liveStatus.status]} · {liveStatus.detail}
|
||||
</span>
|
||||
@@ -118,4 +122,3 @@ export function NodeSummaryCard<T extends AdapterCommon>({
|
||||
</motion.div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -51,6 +51,7 @@ export function ConfigPage() {
|
||||
cancelSwitch,
|
||||
save,
|
||||
saveStatus,
|
||||
saveStatusTone,
|
||||
} = useOneBotInstanceConfig(qqList, {
|
||||
selectedUin,
|
||||
onSelectedUinChange: setSelectedUin,
|
||||
@@ -187,6 +188,7 @@ export function ConfigPage() {
|
||||
selectedUin={selectedUin}
|
||||
dirty={dirty}
|
||||
saveStatus={saveStatus}
|
||||
saveStatusTone={saveStatusTone}
|
||||
onSave={immediateSave}
|
||||
activeTab={activeTab}
|
||||
onCreate={
|
||||
@@ -262,12 +264,13 @@ interface HeaderBarProps {
|
||||
selectedUin: string;
|
||||
dirty: boolean;
|
||||
saveStatus: string;
|
||||
saveStatusTone: 'idle' | 'saving' | 'success' | 'warning' | 'error';
|
||||
onSave: () => void;
|
||||
activeTab: TabKey;
|
||||
onCreate?: () => void;
|
||||
}
|
||||
|
||||
function HeaderBar({ selectedUin, dirty, saveStatus, onSave, activeTab, onCreate }: HeaderBarProps) {
|
||||
function HeaderBar({ selectedUin, dirty, saveStatus, saveStatusTone, onSave, activeTab, onCreate }: HeaderBarProps) {
|
||||
return (
|
||||
<div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div className="min-w-0">
|
||||
@@ -279,11 +282,13 @@ function HeaderBar({ selectedUin, dirty, saveStatus, onSave, activeTab, onCreate
|
||||
<div className="flex flex-wrap items-center gap-3">
|
||||
{(saveStatus || dirty) && (
|
||||
<span className="inline-flex items-center gap-1.5 text-[11px] font-medium">
|
||||
{saveStatus === '保存中...' ? (
|
||||
{saveStatusTone === 'saving' ? (
|
||||
<><Loader2 className="size-3.5 animate-spin text-muted-foreground" /><span className="text-muted-foreground">保存中</span></>
|
||||
) : saveStatus === '保存成功' ? (
|
||||
<><Check className="size-3.5 text-success" /><span className="text-success">已保存</span></>
|
||||
) : saveStatus ? (
|
||||
) : saveStatusTone === 'success' ? (
|
||||
<><Check className="size-3.5 text-success" /><span className="text-success">{saveStatus}</span></>
|
||||
) : saveStatusTone === 'warning' ? (
|
||||
<><span className="size-1.5 rounded-full bg-warning" /><span className="text-warning">{saveStatus}</span></>
|
||||
) : saveStatusTone === 'error' ? (
|
||||
<><span className="size-1.5 rounded-full bg-destructive" /><span className="text-destructive">{saveStatus}</span></>
|
||||
) : (
|
||||
<><span className="size-1.5 rounded-full bg-warning" /><span className="text-warning">未保存</span></>
|
||||
|
||||
@@ -731,17 +731,24 @@ const CONN_STATUS_STYLE: Record<AdapterStatusLevel, string> = {
|
||||
ok: 'bg-success/10 text-success',
|
||||
warn: 'bg-warning/10 text-warning',
|
||||
down: 'bg-destructive/10 text-destructive',
|
||||
degraded: 'bg-destructive/10 text-destructive',
|
||||
disabled: 'bg-muted text-muted-foreground',
|
||||
};
|
||||
const CONN_STATUS_LABEL: Record<AdapterStatusLevel, string> = {
|
||||
ok: '正常', warn: '注意', down: '异常', disabled: '未启用',
|
||||
ok: '正常', warn: '注意', down: '异常', degraded: '应用失败', disabled: '未启用',
|
||||
};
|
||||
const ADAPTER_KIND_LABEL: Record<AdapterStatus['kind'], string> = {
|
||||
httpServer: 'HTTP 服务端', httpClient: 'HTTP 上报', wsServer: 'WS 服务端', wsClient: 'WS 客户端',
|
||||
};
|
||||
|
||||
// Worst-status-first ordering (down → warn → disabled → ok).
|
||||
const CONN_STATUS_RANK: Record<AdapterStatusLevel, number> = { down: 0, warn: 1, disabled: 2, ok: 3 };
|
||||
const CONN_STATUS_RANK: Record<AdapterStatusLevel, number> = {
|
||||
degraded: 0,
|
||||
down: 1,
|
||||
warn: 2,
|
||||
disabled: 3,
|
||||
ok: 4,
|
||||
};
|
||||
|
||||
function ConnectionsBlock({ config }: { config: ConnectionsConfig }) {
|
||||
const { connections } = useAppState();
|
||||
@@ -804,6 +811,11 @@ function ConnectionsBlock({ config }: { config: ConnectionsConfig }) {
|
||||
<span className="shrink-0 text-[10px] text-muted-foreground">{ADAPTER_KIND_LABEL[adp.kind]}</span>
|
||||
</div>
|
||||
<div className="truncate text-[11px] text-muted-foreground">{adp.detail}</div>
|
||||
{adp.lastError && adp.lastErrorAt && (
|
||||
<div className="truncate text-[10px] text-destructive/80">
|
||||
{new Date(adp.lastErrorAt).toLocaleString()} · {adp.lastError}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
|
||||
@@ -33,6 +33,7 @@ export interface UseOneBotInstanceConfig {
|
||||
*/
|
||||
save: (override?: OneBotConfig) => Promise<void>;
|
||||
saveStatus: string;
|
||||
saveStatusTone: 'idle' | 'saving' | 'success' | 'warning' | 'error';
|
||||
}
|
||||
|
||||
const CLEAR_SAVE_STATUS_MS = 3000;
|
||||
@@ -53,8 +54,13 @@ export function useOneBotInstanceConfig(
|
||||
const [config, setConfigState] = useState<OneBotConfig | null>(null);
|
||||
const [savedSnapshot, setSavedSnapshot] = useState<string | null>(null);
|
||||
const [saveStatus, setSaveStatus] = useState('');
|
||||
const [saveStatusTone, setSaveStatusTone] = useState<UseOneBotInstanceConfig['saveStatusTone']>('idle');
|
||||
const [pendingSwitchUin, setPendingSwitchUin] = useState<string | null>(null);
|
||||
const clearTimerRef = useRef<number | null>(null);
|
||||
const editRevisionRef = useRef(0);
|
||||
const saveGenerationRef = useRef(0);
|
||||
const selectedUinRef = useRef(selectedUin);
|
||||
selectedUinRef.current = selectedUin;
|
||||
|
||||
// Auto-select first account when none is selected yet.
|
||||
useEffect(() => {
|
||||
@@ -63,6 +69,14 @@ export function useOneBotInstanceConfig(
|
||||
|
||||
// Load on UIN change. The api client already runs normalizeOneBotConfig.
|
||||
useEffect(() => {
|
||||
editRevisionRef.current += 1;
|
||||
saveGenerationRef.current += 1;
|
||||
if (clearTimerRef.current != null) {
|
||||
window.clearTimeout(clearTimerRef.current);
|
||||
clearTimerRef.current = null;
|
||||
}
|
||||
setSaveStatus('');
|
||||
setSaveStatusTone('idle');
|
||||
if (!selectedUin) {
|
||||
setConfigState(null);
|
||||
setSavedSnapshot(null);
|
||||
@@ -73,6 +87,7 @@ export function useOneBotInstanceConfig(
|
||||
try {
|
||||
const loaded = await api.config.get(selectedUin);
|
||||
if (cancelled) return;
|
||||
editRevisionRef.current += 1;
|
||||
setConfigState(loaded);
|
||||
setSavedSnapshot(JSON.stringify(loaded));
|
||||
} catch (e) {
|
||||
@@ -96,7 +111,10 @@ export function useOneBotInstanceConfig(
|
||||
return JSON.stringify(config) !== savedSnapshot;
|
||||
}, [config, savedSnapshot]);
|
||||
|
||||
const setConfig = useCallback((next: OneBotConfig) => setConfigState(next), []);
|
||||
const setConfig = useCallback((next: OneBotConfig) => {
|
||||
editRevisionRef.current += 1;
|
||||
setConfigState(next);
|
||||
}, []);
|
||||
|
||||
const requestSwitchUin = useCallback(
|
||||
(uin: string) => {
|
||||
@@ -115,27 +133,54 @@ export function useOneBotInstanceConfig(
|
||||
|
||||
const cancelSwitch = useCallback(() => setPendingSwitchUin(null), []);
|
||||
|
||||
const scheduleStatusClear = useCallback(() => {
|
||||
const scheduleStatusClear = useCallback((uin: string, generation: number) => {
|
||||
if (clearTimerRef.current != null) window.clearTimeout(clearTimerRef.current);
|
||||
clearTimerRef.current = window.setTimeout(() => {
|
||||
setSaveStatus('');
|
||||
clearTimerRef.current = null;
|
||||
if (selectedUinRef.current !== uin || saveGenerationRef.current !== generation) return;
|
||||
setSaveStatus('');
|
||||
setSaveStatusTone('idle');
|
||||
}, CLEAR_SAVE_STATUS_MS);
|
||||
}, []);
|
||||
|
||||
const save = useCallback(async (override?: OneBotConfig) => {
|
||||
const target = override ?? config;
|
||||
if (!selectedUin || !target) return;
|
||||
const uin = selectedUin;
|
||||
const editRevision = editRevisionRef.current;
|
||||
const generation = ++saveGenerationRef.current;
|
||||
setSaveStatus('保存中...');
|
||||
setSaveStatusTone('saving');
|
||||
try {
|
||||
const serverView = await api.config.save(selectedUin, target);
|
||||
setConfigState(serverView);
|
||||
setSavedSnapshot(JSON.stringify(serverView));
|
||||
setSaveStatus('保存成功');
|
||||
const result = await api.config.save(uin, target);
|
||||
if (selectedUinRef.current !== uin || saveGenerationRef.current !== generation) return;
|
||||
setSavedSnapshot(JSON.stringify(result.config));
|
||||
if (editRevisionRef.current === editRevision) {
|
||||
setConfigState(result.config);
|
||||
} else {
|
||||
setSaveStatus('上一版本已保存,当前修改仍待保存');
|
||||
setSaveStatusTone('warning');
|
||||
return;
|
||||
}
|
||||
if (!result.online) {
|
||||
setSaveStatus('保存成功;当前离线,将在下次连接时应用');
|
||||
setSaveStatusTone('warning');
|
||||
} else if (result.applied) {
|
||||
setSaveStatus('保存成功,热重载完成');
|
||||
setSaveStatusTone('success');
|
||||
} else {
|
||||
const first = result.errors[0]?.message;
|
||||
setSaveStatus(`保存成功,但热重载失败${first ? `:${first}` : ''}`);
|
||||
setSaveStatusTone('warning');
|
||||
}
|
||||
} catch (e) {
|
||||
if (selectedUinRef.current !== uin || saveGenerationRef.current !== generation) return;
|
||||
setSaveStatus(`保存失败:${e instanceof Error ? e.message : '未知错误'}`);
|
||||
setSaveStatusTone('error');
|
||||
} finally {
|
||||
scheduleStatusClear();
|
||||
if (selectedUinRef.current === uin && saveGenerationRef.current === generation) {
|
||||
scheduleStatusClear(uin, generation);
|
||||
}
|
||||
}
|
||||
}, [api, selectedUin, config, scheduleStatusClear]);
|
||||
|
||||
@@ -150,5 +195,6 @@ export function useOneBotInstanceConfig(
|
||||
cancelSwitch,
|
||||
save,
|
||||
saveStatus,
|
||||
saveStatusTone,
|
||||
};
|
||||
}
|
||||
@@ -86,14 +86,49 @@ class HttpApiClient implements ApiClient {
|
||||
},
|
||||
save: async (uin, config) => {
|
||||
const url = `/api/config/${encodeURIComponent(uin)}`;
|
||||
// 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, {
|
||||
const result = await this.fetchJson<{
|
||||
success?: boolean;
|
||||
saved?: boolean;
|
||||
applied?: boolean;
|
||||
online?: boolean;
|
||||
reloaded?: boolean;
|
||||
errors?: Array<{
|
||||
name: string;
|
||||
kind?: 'httpServer' | 'httpClient' | 'wsServer' | 'wsClient';
|
||||
phase: string;
|
||||
message: string;
|
||||
at: number;
|
||||
restored?: boolean;
|
||||
}>;
|
||||
config?: unknown;
|
||||
message?: string;
|
||||
}>(url, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(config),
|
||||
});
|
||||
return this.config.get(uin);
|
||||
// Refetch the canonical persisted view after the save. Runtime apply
|
||||
// is a separate fact and is preserved alongside that config.
|
||||
let canonical = result.config === undefined
|
||||
? null
|
||||
: normalizeOneBotConfig(result.config);
|
||||
if (canonical === null) {
|
||||
try {
|
||||
canonical = await this.config.get(uin);
|
||||
} catch (error) {
|
||||
// Legacy servers omit `config`. Their POST still confirmed the
|
||||
// save, so a follow-up GET failure must not become "保存失败".
|
||||
console.warn('config saved but canonical refetch failed', error);
|
||||
canonical = config;
|
||||
}
|
||||
}
|
||||
return {
|
||||
config: canonical,
|
||||
saved: result.saved ?? result.success === true,
|
||||
applied: result.applied ?? result.reloaded === true,
|
||||
online: result.online ?? result.reloaded === true,
|
||||
errors: Array.isArray(result.errors) ? result.errors : [],
|
||||
message: result.message ?? '配置保存成功',
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -69,6 +69,24 @@ export type ProcessActionResult = {
|
||||
process?: HookProcessInfo & { error?: string };
|
||||
};
|
||||
|
||||
export interface ConfigApplyError {
|
||||
name: string;
|
||||
kind?: 'httpServer' | 'httpClient' | 'wsServer' | 'wsClient';
|
||||
phase: string;
|
||||
message: string;
|
||||
at: number;
|
||||
restored?: boolean;
|
||||
}
|
||||
|
||||
export interface ConfigSaveResult {
|
||||
config: OneBotConfig;
|
||||
saved: boolean;
|
||||
applied: boolean;
|
||||
online: boolean;
|
||||
errors: ConfigApplyError[];
|
||||
message: string;
|
||||
}
|
||||
|
||||
export type StreamStatus = 'open' | 'reconnecting' | 'closed';
|
||||
|
||||
export interface LogsStreamOptions {
|
||||
@@ -134,7 +152,7 @@ export interface ApiClient {
|
||||
// ---- OneBotInstance per-UIN config ----
|
||||
config: {
|
||||
get(uin: string): Promise<OneBotConfig>;
|
||||
save(uin: string, config: OneBotConfig): Promise<OneBotConfig>;
|
||||
save(uin: string, config: OneBotConfig): Promise<ConfigSaveResult>;
|
||||
};
|
||||
|
||||
// ---- WebUI listener self-config (port / host / TLS / trust-proxy) ----
|
||||
|
||||
@@ -27,13 +27,15 @@ export interface QQInfo {
|
||||
nickname: string;
|
||||
}
|
||||
|
||||
export type AdapterStatusLevel = 'ok' | 'warn' | 'down' | 'disabled';
|
||||
export type AdapterStatusLevel = 'ok' | 'warn' | 'down' | 'disabled' | 'degraded';
|
||||
|
||||
export interface AdapterStatus {
|
||||
name: string;
|
||||
kind: 'httpServer' | 'httpClient' | 'wsServer' | 'wsClient';
|
||||
status: AdapterStatusLevel;
|
||||
detail: string;
|
||||
lastError?: string;
|
||||
lastErrorAt?: number;
|
||||
}
|
||||
|
||||
export interface AccountConnections {
|
||||
|
||||
Reference in new issue
Block a user