|
9 | 9 | useRef, |
10 | 10 | useState, |
11 | 11 | } from 'react' |
| 12 | +import { useParams } from 'next/navigation' |
12 | 13 | import { io, type Socket } from 'socket.io-client' |
13 | 14 | import { createLogger } from '@/lib/logs/console-logger' |
14 | 15 |
|
@@ -85,6 +86,10 @@ export function SocketProvider({ children, user }: SocketProviderProps) { |
85 | 86 | const [currentWorkflowId, setCurrentWorkflowId] = useState<string | null>(null) |
86 | 87 | const [presenceUsers, setPresenceUsers] = useState<PresenceUser[]>([]) |
87 | 88 |
|
| 89 | + // Get current workflow ID from URL params |
| 90 | + const params = useParams() |
| 91 | + const urlWorkflowId = params?.workflowId as string | undefined |
| 92 | + |
88 | 93 | // Use refs to store event handlers to avoid stale closures |
89 | 94 | const eventHandlers = useRef<{ |
90 | 95 | workflowOperation?: (data: any) => void |
@@ -169,6 +174,17 @@ export function SocketProvider({ children, user }: SocketProviderProps) { |
169 | 174 | connected: socketInstance.connected, |
170 | 175 | transport: socketInstance.io.engine?.transport?.name, |
171 | 176 | }) |
| 177 | + |
| 178 | + // Automatically join the current workflow room based on URL |
| 179 | + // This handles both initial connections and reconnections |
| 180 | + if (urlWorkflowId) { |
| 181 | + logger.info(`Joining workflow room after connection: ${urlWorkflowId}`) |
| 182 | + socketInstance.emit('join-workflow', { |
| 183 | + workflowId: urlWorkflowId, |
| 184 | + }) |
| 185 | + // Update our internal state to match the URL |
| 186 | + setCurrentWorkflowId(urlWorkflowId) |
| 187 | + } |
172 | 188 | }) |
173 | 189 |
|
174 | 190 | socketInstance.on('disconnect', (reason) => { |
@@ -214,6 +230,7 @@ export function SocketProvider({ children, user }: SocketProviderProps) { |
214 | 230 | socketId: socketInstance.id, |
215 | 231 | transport: socketInstance.io.engine?.transport?.name, |
216 | 232 | }) |
| 233 | + // Note: Workflow rejoining is handled by the 'connect' event which fires for both initial connections and reconnections |
217 | 234 | }) |
218 | 235 |
|
219 | 236 | socketInstance.on('reconnect_attempt', (attemptNumber) => { |
@@ -331,6 +348,29 @@ export function SocketProvider({ children, user }: SocketProviderProps) { |
331 | 348 | } |
332 | 349 | }, [user?.id]) |
333 | 350 |
|
| 351 | + // Handle workflow room switching when URL changes (for navigation between workflows) |
| 352 | + useEffect(() => { |
| 353 | + if (!socket || !isConnected || !urlWorkflowId) return |
| 354 | + |
| 355 | + // If we're already in the correct workflow room, no need to switch |
| 356 | + if (currentWorkflowId === urlWorkflowId) return |
| 357 | + |
| 358 | + logger.info(`URL workflow changed from ${currentWorkflowId} to ${urlWorkflowId}, switching rooms`) |
| 359 | + |
| 360 | + // Leave current workflow first if we're in one |
| 361 | + if (currentWorkflowId) { |
| 362 | + logger.info(`Leaving current workflow ${currentWorkflowId} before joining ${urlWorkflowId}`) |
| 363 | + socket.emit('leave-workflow') |
| 364 | + } |
| 365 | + |
| 366 | + // Join the new workflow room |
| 367 | + logger.info(`Joining workflow room: ${urlWorkflowId}`) |
| 368 | + socket.emit('join-workflow', { |
| 369 | + workflowId: urlWorkflowId, |
| 370 | + }) |
| 371 | + setCurrentWorkflowId(urlWorkflowId) |
| 372 | + }, [socket, isConnected, urlWorkflowId, currentWorkflowId]) |
| 373 | + |
334 | 374 | // Cleanup socket on component unmount |
335 | 375 | useEffect(() => { |
336 | 376 | return () => { |
|
0 commit comments