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.
15 lines
521 B
TypeScript
15 lines
521 B
TypeScript
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;
|
|
}
|