// https://hexdocs.pm/phoenix/js/ import type { SocketConnectOption } from 'phoenix'; import { Socket } from 'phoenix'; import React, { useEffect, useState } from 'react'; export const SocketContext = React.createContext(null); interface SocketProviderProps { children: React.ReactNode; url?: string; options?: Partial; } export function SocketProvider({ children, options, url }: SocketProviderProps) { const [ socket, setSocket ] = useState(null); useEffect(() => { if (!url) { return; } const socketInstance = new Socket(url, options); socketInstance.connect(); setSocket(socketInstance); return () => { socketInstance.disconnect(); setSocket(null); }; }, [ options, url ]); return ( { children } ); } export function useSocket() { const context = React.useContext(SocketContext); if (context === undefined) { throw new Error('useSocket must be used within a SocketProvider'); } return context; }