@@ -867,48 +867,41 @@ const WorkflowContent = React.memo(() => {
867867 [ project , isPointInLoopNodeWrapper , getNodes ]
868868 )
869869
870- // Track when workflow is fully ready for rendering
870+ // Initialize workflow when it exists in registry and isn't active
871871 useEffect ( ( ) => {
872872 const currentId = params . workflowId as string
873+ if ( ! currentId || ! workflows [ currentId ] ) return
873874
874- // Reset workflow ready state when workflow changes
875875 if ( activeWorkflowId !== currentId ) {
876- setIsWorkflowReady ( false )
877- return
876+ // Clear diff and set as active
877+ const { clearDiff } = useWorkflowDiffStore . getState ( )
878+ clearDiff ( )
879+ setActiveWorkflow ( currentId )
878880 }
881+ } , [ params . workflowId , workflows , activeWorkflowId , setActiveWorkflow ] )
879882
880- // Check if we have the necessary data to render the workflow
881- const hasActiveWorkflow = activeWorkflowId === currentId
882- const hasWorkflowInRegistry = Boolean ( workflows [ currentId ] )
883- const isNotLoading = ! isLoading
883+ // Track when workflow is ready for rendering
884+ useEffect ( ( ) => {
885+ const currentId = params . workflowId as string
884886
885887 // Workflow is ready when:
886888 // 1. We have an active workflow that matches the URL
887889 // 2. The workflow exists in the registry
888890 // 3. Workflows are not currently loading
889- if ( hasActiveWorkflow && hasWorkflowInRegistry && isNotLoading ) {
890- setIsWorkflowReady ( true )
891- } else {
892- setIsWorkflowReady ( false )
893- }
891+ const shouldBeReady =
892+ activeWorkflowId === currentId && Boolean ( workflows [ currentId ] ) && ! isLoading
893+
894+ setIsWorkflowReady ( shouldBeReady )
894895 } , [ activeWorkflowId , params . workflowId , workflows , isLoading ] )
895896
896- // Init workflow
897+ // Handle navigation and validation
897898 useEffect ( ( ) => {
898899 const validateAndNavigate = async ( ) => {
899900 const workflowIds = Object . keys ( workflows )
900901 const currentId = params . workflowId as string
901902
902- // Check if workflows have been initially loaded at least once
903- // This prevents premature navigation decisions on page refresh
904- if ( ! hasWorkflowsInitiallyLoaded ( ) ) {
905- logger . info ( 'Waiting for initial workflow load...' )
906- return
907- }
908-
909- // Wait for both initialization and workflow loading to complete
910- if ( isLoading ) {
911- logger . info ( 'Workflows still loading, waiting...' )
903+ // Wait for initial load to complete before making navigation decisions
904+ if ( ! hasWorkflowsInitiallyLoaded ( ) || isLoading ) {
912905 return
913906 }
914907
@@ -948,24 +941,10 @@ const WorkflowContent = React.memo(() => {
948941 router . replace ( `/workspace/${ currentWorkflow . workspaceId } /w/${ currentId } ` )
949942 return
950943 }
951-
952- // Get current active workflow state
953- const { activeWorkflowId } = useWorkflowRegistry . getState ( )
954-
955- if ( activeWorkflowId !== currentId ) {
956- // Clear workflow diff store when switching workflows
957- const { clearDiff } = useWorkflowDiffStore . getState ( )
958- clearDiff ( )
959-
960- setActiveWorkflow ( currentId )
961- } else {
962- // Don't reset variables cache if we're not actually switching workflows
963- setActiveWorkflow ( currentId )
964- }
965944 }
966945
967946 validateAndNavigate ( )
968- } , [ params . workflowId , workflows , isLoading , setActiveWorkflow , createWorkflow , router ] )
947+ } , [ params . workflowId , workflows , isLoading , workspaceId , router ] )
969948
970949 // Transform blocks and loops into ReactFlow nodes
971950 const nodes = useMemo ( ( ) => {
0 commit comments