@@ -333,45 +333,23 @@ export const WorkspaceHeader = React.memo<WorkspaceHeaderProps>(
333333 } , [ sessionData ?. user ?. id , fetchSubscriptionStatus , fetchWorkspaces ] )
334334
335335 const switchWorkspace = useCallback (
336- async ( workspace : Workspace ) => {
336+ ( 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- // Close dropdown immediately for responsive feel
343+ setActiveWorkspace ( workspace )
344344 setWorkspaceDropdownOpen ( false )
345345
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- }
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` )
366351 } ,
367- [
368- activeWorkspace ?. id ,
369- switchToWorkspace ,
370- router ,
371- setWorkspaceDropdownOpen ,
372- workspaces ,
373- currentWorkspaceId ,
374- ]
352+ [ activeWorkspace ?. id , switchToWorkspace , router , setWorkspaceDropdownOpen ]
375353 )
376354
377355 const handleCreateWorkspace = useCallback (
@@ -394,11 +372,11 @@ export const WorkspaceHeader = React.memo<WorkspaceHeaderProps>(
394372 setWorkspaces ( ( prev ) => [ ...prev , newWorkspace ] )
395373 setActiveWorkspace ( newWorkspace )
396374
397- // Switch workspace data first with the explicit workspace ID
375+ // Use switchToWorkspace to properly load workflows for the new workspace
398376 // This will clear existing workflows, set loading state, and fetch workflows from DB
399377 switchToWorkspace ( newWorkspace . id )
400378
401- // Then update URL to include new workspace ID
379+ // Update URL to include new workspace ID
402380 router . push ( `/workspace/${ newWorkspace . id } /w` )
403381 }
404382 } catch ( err ) {
@@ -487,14 +465,10 @@ export const WorkspaceHeader = React.memo<WorkspaceHeaderProps>(
487465
488466 // If deleted workspace was active, switch to another workspace
489467 if ( activeWorkspace ?. id === id && updatedWorkspaces . length > 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` )
468+ // Use the specialized method for handling workspace deletion
469+ const newWorkspaceId = updatedWorkspaces [ 0 ] . id
470+ useWorkflowRegistry . getState ( ) . handleWorkspaceDeletion ( newWorkspaceId )
471+ setActiveWorkspace ( updatedWorkspaces [ 0 ] )
498472 }
499473
500474 setWorkspaceDropdownOpen ( false )
@@ -504,7 +478,7 @@ export const WorkspaceHeader = React.memo<WorkspaceHeaderProps>(
504478 setIsDeleting ( false )
505479 }
506480 } ,
507- [ workspaces , activeWorkspace ?. id , router ]
481+ [ workspaces , activeWorkspace ?. id ]
508482 )
509483
510484 const openEditModal = useCallback (
0 commit comments