24 lines
684 B
TypeScript
24 lines
684 B
TypeScript
import { createContext, useContext } from "react";
|
|
import { ReadyState } from "react-use-websocket";
|
|
import type { InfoBarData } from "../../types/types";
|
|
|
|
type InfoSocketState = {
|
|
data: InfoBarData | null;
|
|
readyState: ReadyState;
|
|
sendJson: (msg: unknown) => void;
|
|
};
|
|
|
|
export type WebSocketConextValue = {
|
|
info: InfoSocketState;
|
|
};
|
|
|
|
export const WebsocketContext = createContext<WebSocketConextValue | null>(null);
|
|
|
|
const useWebSocketContext = () => {
|
|
const ctx = useContext(WebsocketContext);
|
|
if (!ctx) throw new Error("useWebSocketContext must be used inside <WebSocketConext.Provider>");
|
|
return ctx;
|
|
};
|
|
|
|
export const useInfoSocket = () => useWebSocketContext().info;
|