@@ -209,12 +209,16 @@ export function ControlBar() {
209209 // Store the workflow ID at the start of the request to prevent race conditions
210210 const requestWorkflowId = activeWorkflowId
211211
212+ // Helper to get current active workflow ID for race condition checks
213+ const getCurrentActiveWorkflowId = ( ) => useWorkflowRegistry . getState ( ) . activeWorkflowId
214+
212215 try {
213216 setIsLoadingDeployedState ( true )
214217
215218 const response = await fetch ( `/api/workflows/${ requestWorkflowId } /deployed` )
216219
217- if ( requestWorkflowId !== useWorkflowRegistry . getState ( ) . activeWorkflowId ) {
220+ // Check if the workflow ID changed during the request (user navigated away)
221+ if ( requestWorkflowId !== getCurrentActiveWorkflowId ( ) ) {
218222 logger . debug ( 'Workflow changed during deployed state fetch, ignoring response' )
219223 return
220224 }
@@ -229,19 +233,18 @@ export function ControlBar() {
229233
230234 const data = await response . json ( )
231235
232- if ( requestWorkflowId === useWorkflowRegistry . getState ( ) . activeWorkflowId ) {
236+ if ( requestWorkflowId === getCurrentActiveWorkflowId ( ) ) {
233237 setDeployedState ( data . deployedState || null )
234238 } else {
235239 logger . debug ( 'Workflow changed after deployed state response, ignoring result' )
236240 }
237241 } catch ( error ) {
238242 logger . error ( 'Error fetching deployed state:' , { error } )
239- // Only set error state if we're still on the same workflow
240- if ( requestWorkflowId === useWorkflowRegistry . getState ( ) . activeWorkflowId ) {
243+ if ( requestWorkflowId === getCurrentActiveWorkflowId ( ) ) {
241244 setDeployedState ( null )
242245 }
243246 } finally {
244- if ( requestWorkflowId === useWorkflowRegistry . getState ( ) . activeWorkflowId ) {
247+ if ( requestWorkflowId === getCurrentActiveWorkflowId ( ) ) {
245248 setIsLoadingDeployedState ( false )
246249 }
247250 }
0 commit comments