1- import { useEffect , useState } from 'react'
1+ import { useCallback , useEffect , useState } from 'react'
22import { createLogger } from '@sim/logger'
33import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
44import type { WorkflowState } from '@/stores/workflows/workflow/types'
@@ -27,29 +27,27 @@ export function useDeployedState({
2727 ( state ) => state . setWorkflowNeedsRedeployment
2828 )
2929
30- /**
31- * Fetches the deployed state of the workflow from the server
32- * This is the single source of truth for deployed workflow state
33- */
34- const fetchDeployedState = async ( ) => {
35- if ( ! workflowId || ! isDeployed ) {
30+ const fetchDeployedState = useCallback ( async ( ) => {
31+ const registry = useWorkflowRegistry . getState ( )
32+ const currentWorkflowId = registry . activeWorkflowId
33+ const deploymentStatus = currentWorkflowId
34+ ? registry . getWorkflowDeploymentStatus ( currentWorkflowId )
35+ : null
36+ const currentIsDeployed = deploymentStatus ?. isDeployed ?? false
37+
38+ if ( ! currentWorkflowId || ! currentIsDeployed ) {
3639 setDeployedState ( null )
3740 return
3841 }
3942
40- // Store the workflow ID at the start of the request to prevent race conditions
41- const requestWorkflowId = workflowId
42-
43- // Helper to get current active workflow ID for race condition checks
44- const getCurrentActiveWorkflowId = ( ) => useWorkflowRegistry . getState ( ) . activeWorkflowId
43+ const requestWorkflowId = currentWorkflowId
4544
4645 try {
4746 setIsLoadingDeployedState ( true )
4847
4948 const response = await fetch ( `/api/workflows/${ requestWorkflowId } /deployed` )
5049
51- // Check if the workflow ID changed during the request (user navigated away)
52- if ( requestWorkflowId !== getCurrentActiveWorkflowId ( ) ) {
50+ if ( requestWorkflowId !== useWorkflowRegistry . getState ( ) . activeWorkflowId ) {
5351 logger . debug ( 'Workflow changed during deployed state fetch, ignoring response' )
5452 return
5553 }
@@ -64,22 +62,22 @@ export function useDeployedState({
6462
6563 const data = await response . json ( )
6664
67- if ( requestWorkflowId === getCurrentActiveWorkflowId ( ) ) {
65+ if ( requestWorkflowId === useWorkflowRegistry . getState ( ) . activeWorkflowId ) {
6866 setDeployedState ( data . deployedState || null )
6967 } else {
7068 logger . debug ( 'Workflow changed after deployed state response, ignoring result' )
7169 }
7270 } catch ( error ) {
7371 logger . error ( 'Error fetching deployed state:' , { error } )
74- if ( requestWorkflowId === getCurrentActiveWorkflowId ( ) ) {
72+ if ( requestWorkflowId === useWorkflowRegistry . getState ( ) . activeWorkflowId ) {
7573 setDeployedState ( null )
7674 }
7775 } finally {
78- if ( requestWorkflowId === getCurrentActiveWorkflowId ( ) ) {
76+ if ( requestWorkflowId === useWorkflowRegistry . getState ( ) . activeWorkflowId ) {
7977 setIsLoadingDeployedState ( false )
8078 }
8179 }
82- }
80+ } , [ ] )
8381
8482 useEffect ( ( ) => {
8583 if ( ! workflowId ) {
0 commit comments