Download src/context/SocketContext.js from SyedFouzan/taskflow-titan-workflow-wizardry: direct link, hf CLI and curl.
- Browser
- Download file 1.17 kB
-
https://huggingface.co/spaces/SyedFouzan/taskflow-titan-workflow-wizardry/resolve/main/src/context/SocketContext.js
- Command line
-
hf download hf://spaces/SyedFouzan/taskflow-titan-workflow-wizardry/src/context/SocketContext.js
-
curl -L -o SocketContext.js https://huggingface.co/spaces/SyedFouzan/taskflow-titan-workflow-wizardry/resolve/main/src/context/SocketContext.js
1.17 kB
| import React, { createContext, useContext, useEffect, useState } from 'react'; | |
| import io from 'socket.io-client'; | |
| import { useAuth } from './AuthContext'; | |
| const SocketContext = createContext(); | |
| export const useSocket = () => { | |
| const context = useContext(SocketContext); | |
| if (!context) { | |
| throw new Error('useSocket must be used within a SocketProvider'); | |
| } | |
| return context; | |
| }; | |
| export const SocketProvider = ({ children }) => { | |
| const [socket, setSocket] = useState(null); | |
| const { user } = useAuth(); | |
| useEffect(() => { | |
| if (user) { | |
| const newSocket = io(process.env.REACT_APP_SOCKET_URL || 'http://localhost:8080', { | |
| auth: { | |
| token: localStorage.getItem('token') | |
| } | |
| }); | |
| newSocket.on('connect', () => { | |
| console.log('Connected to server'); | |
| }); | |
| newSocket.on('disconnect', () => { | |
| console.log('Disconnected from server'); | |
| }); | |
| setSocket(newSocket); | |
| return () => { | |
| newSocket.close(); | |
| }; | |
| } | |
| }, [user]); | |
| const value = { | |
| socket | |
| }; | |
| return ( | |
| <SocketContext.Provider value={value}> | |
| {children} | |
| </SocketContext.Provider> | |
| ); | |
| }; |