refactor(webui): extract ApiClient + lift OneBotInstance config editor

App.tsx was the single source of truth for token, fetch policy, 401
handling, and per-UIN config edit state — fetchApi was drilled into
every page and LogsPage read localStorage directly to build its SSE
URL, giving two parallel truths for the bearer token.

Promote the closure to a deep ApiClient module (domain methods +
request() escape hatch) injected via ApiProvider/useApi, and lift
selectedUin/config/saveStatus into useOneBotInstanceConfig so
ConfigPage is self-contained. normalizeOneBotConfig becomes a pure
function. ConfigPage now guards UIN switches when the editor is
dirty and refetches the server-normalised config after save.

ConfigPage prop count drops from 7 to 1; App.tsx from 421 to 270 LOC.
This commit is contained in:
DrSmoothl committed 2026-05-16 02:04:11 +08:00
1 parent 47921eee12
commit b89d51d984
11 files changed
+818 -398

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
import { createContext, useContext, type ReactNode } from 'react';
import type { ApiClient } from './types';
const ApiContext = createContext<ApiClient | null>(null);
export function ApiProvider({ client, children }: { client: ApiClient; children: ReactNode }) {
return <ApiContext.Provider value={client}>{children}</ApiContext.Provider>;
}
export function useApi(): ApiClient {
const client = useContext(ApiContext);
if (!client) throw new Error('useApi must be used inside <ApiProvider>');
return client;
}