Skip to content

Commit 2784b96

Browse files
Adam GoughAdam Gough
authored andcommitted
simplified useEffect
1 parent 78395f6 commit 2784b96

1 file changed

Lines changed: 20 additions & 31 deletions

File tree

apps/sim/app/w/[id]/components/control-bar/control-bar.tsx

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -268,45 +268,34 @@ export function ControlBar() {
268268
}
269269
}, [activeWorkflowId, isDeployed, setNeedsRedeploymentFlag])
270270

271+
// Get current store state for change detection
272+
const currentBlocks = useWorkflowStore((state) => state.blocks)
273+
const subBlockValues = useSubBlockStore((state) =>
274+
activeWorkflowId ? state.workflowValues[activeWorkflowId] : null
275+
)
276+
271277
// Subscribe to workflow and subblock changes to detect differences from deployed state
272278
useEffect(() => {
279+
// Early exit: No workflow or nothing deployed = no changes possible
273280
if (!activeWorkflowId || !deployedState) {
274281
setChangeDetected(false)
275282
return
276283
}
277284

278-
const checkForChanges = () => {
279-
// Get fresh state each time
280-
const currentBlocks = useWorkflowStore.getState().blocks
281-
if (!currentBlocks || !deployedState) return
282-
283-
// Merge current workflow state with subblock values
284-
const currentMergedState = mergeSubblockState(currentBlocks, activeWorkflowId)
285-
286-
// Compare with deployed state
287-
const deployedBlocks = (deployedState as any)?.blocks
288-
if (!deployedBlocks) return
289-
290-
const hasChanges = JSON.stringify(currentMergedState) !== JSON.stringify(deployedBlocks)
291-
292-
// Only update if the state actually changed to prevent unnecessary renders
293-
setChangeDetected(prev => prev !== hasChanges ? hasChanges : prev)
294-
}
295-
296-
// Check immediately
297-
checkForChanges()
298-
299-
// Subscribe to workflow store changes (blocks structure/content changes)
300-
const unsubscribeWorkflow = useWorkflowStore.subscribe(checkForChanges)
301-
302-
// Subscribe to subblock store changes (subblock values changes)
303-
const unsubscribeSubBlocks = useSubBlockStore.subscribe(checkForChanges)
304-
305-
return () => {
306-
unsubscribeWorkflow()
307-
unsubscribeSubBlocks()
285+
// Get current workflow state merged with user inputs
286+
const currentMergedState = mergeSubblockState(currentBlocks, activeWorkflowId)
287+
288+
// Compare current state vs deployed state
289+
const deployedBlocks = deployedState?.blocks
290+
if (!deployedBlocks) {
291+
setChangeDetected(false)
292+
return
308293
}
309-
}, [activeWorkflowId, deployedState])
294+
295+
// Simple JSON comparison - if different, changes detected
296+
const hasChanges = JSON.stringify(currentMergedState) !== JSON.stringify(deployedBlocks)
297+
setChangeDetected(hasChanges)
298+
}, [activeWorkflowId, deployedState, currentBlocks, subBlockValues])
310299

311300
// Check usage limits when component mounts and when user executes a workflow
312301
useEffect(() => {

0 commit comments

Comments
 (0)