mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-06-05 12:44:22 +00:00
Phase 1 of the JS→TS migration: convert three small, isolated files (axios-init, websocket, reality-targets) to typed sources so future phases can lean on their interfaces. - api/axios-init.ts: typed CSRF cache, interceptors, request retry - api/websocket.ts: typed listener map, message envelope guard, reconnect timer - models/reality-targets.ts: RealityTarget interface, readonly list - env.d.ts: minimal qs module shim (stringify/parse) - consumers: drop ".js" extension from @/api imports
32 lines
978 B
TypeScript
32 lines
978 B
TypeScript
import { useEffect } from 'react';
|
|
import { WebSocketClient } from '@/api/websocket';
|
|
|
|
type Handler = (payload: unknown) => void;
|
|
|
|
interface SharedClient {
|
|
connect(): void;
|
|
on(event: string, fn: Handler): void;
|
|
off(event: string, fn: Handler): void;
|
|
}
|
|
|
|
let sharedClient: SharedClient | null = null;
|
|
|
|
function getSharedClient(): SharedClient {
|
|
if (sharedClient) return sharedClient;
|
|
const basePath = (typeof window !== 'undefined' && window.X_UI_BASE_PATH) || '';
|
|
sharedClient = new WebSocketClient(basePath) as SharedClient;
|
|
return sharedClient;
|
|
}
|
|
|
|
export function useWebSocket(handlers: Record<string, Handler>) {
|
|
useEffect(() => {
|
|
const client = getSharedClient();
|
|
const entries = Object.entries(handlers);
|
|
for (const [event, fn] of entries) client.on(event, fn);
|
|
client.connect();
|
|
return () => {
|
|
for (const [event, fn] of entries) client.off(event, fn);
|
|
};
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, []);
|
|
}
|