Skip to content

Commit 01183f1

Browse files
authored
fix(executor): consolidate execution hooks (#1950)
1 parent ff08171 commit 01183f1

File tree

4 files changed

+42
-81
lines changed

4 files changed

+42
-81
lines changed
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export { usePanelResize } from './use-panel-resize'
2-
export { useRunWorkflow } from './use-run-workflow'
32
export { type UsageData, useUsageLimits } from './use-usage-limits'

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel-new/hooks/use-run-workflow.ts

Lines changed: 0 additions & 75 deletions
This file was deleted.

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel-new/panel-new.tsx

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { createLogger } from '@/lib/logs/console/logger'
2626
import { useRegisterGlobalCommands } from '@/app/workspace/[workspaceId]/providers/global-commands-provider'
2727
import { useUserPermissionsContext } from '@/app/workspace/[workspaceId]/providers/workspace-permissions-provider'
2828
import { Variables } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/variables/variables'
29+
import { useWorkflowExecution } from '@/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-workflow-execution'
2930
import { useDeleteWorkflow } from '@/app/workspace/[workspaceId]/w/hooks'
3031
import { useChatStore } from '@/stores/chat/store'
3132
import { usePanelStore } from '@/stores/panel-new/store'
@@ -35,7 +36,7 @@ import { useWorkflowJsonStore } from '@/stores/workflows/json/store'
3536
import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
3637
import { useWorkflowStore } from '@/stores/workflows/workflow/store'
3738
import { Copilot, Deploy, Editor, Toolbar } from './components'
38-
import { usePanelResize, useRunWorkflow, useUsageLimits } from './hooks'
39+
import { usePanelResize, useUsageLimits } from './hooks'
3940

4041
const logger = createLogger('Panel')
4142
/**
@@ -99,12 +100,43 @@ export function Panel() {
99100
autoRefresh: !isRegistryLoading,
100101
})
101102

102-
// Run workflow hook
103-
const { runWorkflow, cancelWorkflow, isExecuting } = useRunWorkflow({ usageExceeded })
103+
// Workflow execution hook
104+
const { handleRunWorkflow, handleCancelExecution, isExecuting } = useWorkflowExecution()
104105

105106
// Panel resize hook
106107
const { handleMouseDown } = usePanelResize()
107108

109+
/**
110+
* Opens subscription settings modal
111+
*/
112+
const openSubscriptionSettings = () => {
113+
if (typeof window !== 'undefined') {
114+
window.dispatchEvent(
115+
new CustomEvent('open-settings', {
116+
detail: { tab: 'subscription' },
117+
})
118+
)
119+
}
120+
}
121+
122+
/**
123+
* Runs the workflow with usage limit check
124+
*/
125+
const runWorkflow = useCallback(async () => {
126+
if (usageExceeded) {
127+
openSubscriptionSettings()
128+
return
129+
}
130+
await handleRunWorkflow()
131+
}, [usageExceeded, handleRunWorkflow])
132+
133+
/**
134+
* Cancels the currently executing workflow
135+
*/
136+
const cancelWorkflow = useCallback(async () => {
137+
await handleCancelExecution()
138+
}, [handleCancelExecution])
139+
108140
// Chat state
109141
const { isChatOpen, setIsChatOpen } = useChatStore()
110142
const { isOpen: isVariablesOpen, setIsOpen: setVariablesOpen } = useVariablesStore()

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-workflow-execution.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { type ConsoleEntry, useTerminalConsoleStore } from '@/stores/terminal'
2020
import { useWorkflowDiffStore } from '@/stores/workflow-diff'
2121
import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
2222
import { mergeSubblockState } from '@/stores/workflows/utils'
23+
import { useWorkflowStore } from '@/stores/workflows/workflow/store'
2324
import { useCurrentWorkflow } from './use-current-workflow'
2425

2526
const logger = createLogger('useWorkflowExecution')
@@ -671,10 +672,14 @@ export function useWorkflowExecution() {
671672
const executionWorkflowState =
672673
hasActiveDiffWorkflow && executionDiffWorkflow ? executionDiffWorkflow : null
673674
const usingDiffForExecution = executionWorkflowState !== null
675+
676+
// Read blocks and edges directly from store to ensure we get the latest state,
677+
// even if React hasn't re-rendered yet after adding blocks/edges
678+
const latestWorkflowState = useWorkflowStore.getState().getWorkflowState()
674679
const workflowBlocks = (executionWorkflowState?.blocks ??
675-
currentWorkflow.blocks) as typeof currentWorkflow.blocks
680+
latestWorkflowState.blocks) as typeof currentWorkflow.blocks
676681
const workflowEdges = (executionWorkflowState?.edges ??
677-
currentWorkflow.edges) as typeof currentWorkflow.edges
682+
latestWorkflowState.edges) as typeof currentWorkflow.edges
678683

679684
// Filter out blocks without type (these are layout-only blocks)
680685
const validBlocks = Object.entries(workflowBlocks).reduce(

0 commit comments

Comments
 (0)