@@ -333,23 +333,45 @@ export const WorkspaceHeader = React.memo<WorkspaceHeaderProps>(
333333 } , [ sessionData ?. user ?. id , fetchSubscriptionStatus , fetchWorkspaces ] )
334334
335335 const switchWorkspace = useCallback (
336- ( workspace : Workspace ) => {
336+ async ( workspace : Workspace ) => {
337337 // If already on this workspace, close dropdown and do nothing else
338338 if ( activeWorkspace ?. id === workspace . id ) {
339339 setWorkspaceDropdownOpen ( false )
340340 return
341341 }
342342
343- setActiveWorkspace ( workspace )
343+ // Close dropdown immediately for responsive feel
344344 setWorkspaceDropdownOpen ( false )
345345
346- // Use full workspace switch which now handles localStorage automatically
347- switchToWorkspace ( workspace . id )
348-
349- // Update URL to include workspace ID
350- router . push ( `/workspace/${ workspace . id } /w` )
346+ try {
347+ // Update UI state optimistically
348+ setActiveWorkspace ( workspace )
349+
350+ // Switch workspace data first with the explicit workspace ID
351+ // This ensures the data switch happens with the correct ID regardless of URL timing
352+ await switchToWorkspace ( workspace . id )
353+
354+ // Then update URL - this will trigger useParams updates in other components
355+ router . push ( `/workspace/${ workspace . id } /w` )
356+ } catch ( error ) {
357+ // If workspace switch fails, revert the optimistic UI update
358+ logger . error ( 'Failed to switch workspace:' , error )
359+ // Revert to previous workspace if we can identify it
360+ const currentWorkspaces = workspaces
361+ const fallbackWorkspace = currentWorkspaces . find ( ( w ) => w . id === currentWorkspaceId )
362+ if ( fallbackWorkspace ) {
363+ setActiveWorkspace ( fallbackWorkspace )
364+ }
365+ }
351366 } ,
352- [ activeWorkspace ?. id , switchToWorkspace , router , setWorkspaceDropdownOpen ]
367+ [
368+ activeWorkspace ?. id ,
369+ switchToWorkspace ,
370+ router ,
371+ setWorkspaceDropdownOpen ,
372+ workspaces ,
373+ currentWorkspaceId ,
374+ ]
353375 )
354376
355377 const handleCreateWorkspace = useCallback (
@@ -372,11 +394,11 @@ export const WorkspaceHeader = React.memo<WorkspaceHeaderProps>(
372394 setWorkspaces ( ( prev ) => [ ...prev , newWorkspace ] )
373395 setActiveWorkspace ( newWorkspace )
374396
375- // Use switchToWorkspace to properly load workflows for the new workspace
397+ // Switch workspace data first with the explicit workspace ID
376398 // This will clear existing workflows, set loading state, and fetch workflows from DB
377399 switchToWorkspace ( newWorkspace . id )
378400
379- // Update URL to include new workspace ID
401+ // Then update URL to include new workspace ID
380402 router . push ( `/workspace/${ newWorkspace . id } /w` )
381403 }
382404 } catch ( err ) {
@@ -465,10 +487,14 @@ export const WorkspaceHeader = React.memo<WorkspaceHeaderProps>(
465487
466488 // If deleted workspace was active, switch to another workspace
467489 if ( activeWorkspace ?. id === id && updatedWorkspaces . length > 0 ) {
468- // Use the specialized method for handling workspace deletion
469- const newWorkspaceId = updatedWorkspaces [ 0 ] . id
470- useWorkflowRegistry . getState ( ) . handleWorkspaceDeletion ( newWorkspaceId )
471- setActiveWorkspace ( updatedWorkspaces [ 0 ] )
490+ const newWorkspace = updatedWorkspaces [ 0 ]
491+ setActiveWorkspace ( newWorkspace )
492+
493+ // Use the specialized method for handling workspace deletion with explicit workspace ID
494+ useWorkflowRegistry . getState ( ) . handleWorkspaceDeletion ( newWorkspace . id )
495+
496+ // Update URL to the new workspace
497+ router . push ( `/workspace/${ newWorkspace . id } /w` )
472498 }
473499
474500 setWorkspaceDropdownOpen ( false )
@@ -478,7 +504,7 @@ export const WorkspaceHeader = React.memo<WorkspaceHeaderProps>(
478504 setIsDeleting ( false )
479505 }
480506 } ,
481- [ workspaces , activeWorkspace ?. id ]
507+ [ workspaces , activeWorkspace ?. id , router ]
482508 )
483509
484510 const openEditModal = useCallback (
0 commit comments