mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-04-27 07:48:01 -07:00
19 lines
728 B
TypeScript
19 lines
728 B
TypeScript
import { createContext, useContext, useState, ReactNode } from 'react';
|
|
import { WebClient } from '@app/websocket';
|
|
import { createWebClientRequest, createWebClientResponse } from '@app/api';
|
|
|
|
export const WebClientContext = createContext<WebClient | null>(null);
|
|
|
|
export function WebClientProvider({ children }: { children: ReactNode }) {
|
|
const [client] = useState(() => new WebClient(createWebClientRequest(), createWebClientResponse()));
|
|
|
|
return <WebClientContext value={client}>{children}</WebClientContext>;
|
|
}
|
|
|
|
export function useWebClient(): WebClient {
|
|
const client = useContext(WebClientContext);
|
|
if (!client) {
|
|
throw new Error('useWebClient must be used within a WebClientProvider');
|
|
}
|
|
return client;
|
|
}
|