Skip to content

Commit 2a7b4da

Browse files
Vikhyath MondretiVikhyath Mondreti
authored andcommitted
fix(reconn): take workflow id from url
1 parent f5f65fd commit 2a7b4da

File tree

3 files changed

+47
-26
lines changed

3 files changed

+47
-26
lines changed

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/workflow.tsx

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ const WorkflowContent = React.memo(() => {
122122
collaborativeUpdateParentId: updateParentId,
123123
isConnected,
124124
currentWorkflowId,
125-
joinWorkflow,
126125
} = useCollaborativeWorkflow()
127126
const { emitSubblockUpdate } = useSocket()
128127
const { markAllAsRead } = useNotificationStore()
@@ -356,25 +355,8 @@ const WorkflowContent = React.memo(() => {
356355
}
357356
}, [debouncedAutoLayout])
358357

359-
// Listen for active workflow changes and join socket room
360-
useEffect(() => {
361-
const handleActiveWorkflowChanged = (event: CustomEvent) => {
362-
const { workflowId } = event.detail
363-
if (workflowId && isConnected) {
364-
logger.info(`Active workflow changed to ${workflowId}, joining socket room`)
365-
joinWorkflow(workflowId)
366-
}
367-
}
368-
369-
window.addEventListener('active-workflow-changed', handleActiveWorkflowChanged as EventListener)
370-
371-
return () => {
372-
window.removeEventListener(
373-
'active-workflow-changed',
374-
handleActiveWorkflowChanged as EventListener
375-
)
376-
}
377-
}, [isConnected, joinWorkflow])
358+
// Note: Workflow room joining is now handled automatically by socket connect event based on URL
359+
// This eliminates the need for manual joining when active workflow changes
378360

379361
// Note: Workflow initialization now handled by Socket.IO system
380362

apps/sim/contexts/socket-context.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
useRef,
1010
useState,
1111
} from 'react'
12+
import { useParams } from 'next/navigation'
1213
import { io, type Socket } from 'socket.io-client'
1314
import { createLogger } from '@/lib/logs/console-logger'
1415

@@ -85,6 +86,10 @@ export function SocketProvider({ children, user }: SocketProviderProps) {
8586
const [currentWorkflowId, setCurrentWorkflowId] = useState<string | null>(null)
8687
const [presenceUsers, setPresenceUsers] = useState<PresenceUser[]>([])
8788

89+
// Get current workflow ID from URL params
90+
const params = useParams()
91+
const urlWorkflowId = params?.workflowId as string | undefined
92+
8893
// Use refs to store event handlers to avoid stale closures
8994
const eventHandlers = useRef<{
9095
workflowOperation?: (data: any) => void
@@ -169,6 +174,17 @@ export function SocketProvider({ children, user }: SocketProviderProps) {
169174
connected: socketInstance.connected,
170175
transport: socketInstance.io.engine?.transport?.name,
171176
})
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+
}
172188
})
173189

174190
socketInstance.on('disconnect', (reason) => {
@@ -214,6 +230,7 @@ export function SocketProvider({ children, user }: SocketProviderProps) {
214230
socketId: socketInstance.id,
215231
transport: socketInstance.io.engine?.transport?.name,
216232
})
233+
// Note: Workflow rejoining is handled by the 'connect' event which fires for both initial connections and reconnections
217234
})
218235

219236
socketInstance.on('reconnect_attempt', (attemptNumber) => {
@@ -331,6 +348,29 @@ export function SocketProvider({ children, user }: SocketProviderProps) {
331348
}
332349
}, [user?.id])
333350

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+
334374
// Cleanup socket on component unmount
335375
useEffect(() => {
336376
return () => {

apps/sim/hooks/use-collaborative-workflow.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ export function useCollaborativeWorkflow() {
3737
// Track last applied position timestamps to prevent out-of-order updates
3838
const lastPositionTimestamps = useRef<Map<string, number>>(new Map())
3939

40-
// Join workflow room when active workflow changes
40+
// Clear position timestamps when switching workflows
41+
// Note: Workflow joining is now handled automatically by socket connect event based on URL
4142
useEffect(() => {
42-
if (activeWorkflowId && isConnected && currentWorkflowId !== activeWorkflowId) {
43-
logger.info(`Joining workflow room: ${activeWorkflowId}`, {
43+
if (activeWorkflowId && currentWorkflowId !== activeWorkflowId) {
44+
logger.info(`Active workflow changed to: ${activeWorkflowId}`, {
4445
isConnected,
4546
currentWorkflowId,
4647
activeWorkflowId,
@@ -49,10 +50,8 @@ export function useCollaborativeWorkflow() {
4950

5051
// Clear position timestamps when switching workflows
5152
lastPositionTimestamps.current.clear()
52-
53-
joinWorkflow(activeWorkflowId)
5453
}
55-
}, [activeWorkflowId, isConnected, currentWorkflowId, joinWorkflow])
54+
}, [activeWorkflowId, isConnected, currentWorkflowId])
5655

5756
// Log connection status changes
5857
useEffect(() => {

0 commit comments

Comments
 (0)