diff --git a/app/w/[id]/workflow.tsx b/app/w/[id]/workflow.tsx index 114b0ceb23..d36c9c870f 100644 --- a/app/w/[id]/workflow.tsx +++ b/app/w/[id]/workflow.tsx @@ -27,6 +27,7 @@ import { BlockState } from '@/stores/workflow/types' import { NotificationList } from '@/app/w/components/notifications/notifications' import { useNotificationStore } from '@/stores/notifications/notifications-store' import { executeWorkflow } from '@/lib/workflow' +import { useWorkflowExecution } from '../hooks/use-workflow-execution' /** * Represents the data structure for a workflow node @@ -95,6 +96,7 @@ const edgeTypes: EdgeTypes = { custom: CustomEdge } */ function WorkflowCanvas() { const [selectedBlockId, setSelectedBlockId] = useState(null) + const { isExecuting, executionResult, handleRunWorkflow } = useWorkflowExecution() const { blocks, @@ -108,8 +110,6 @@ function WorkflowCanvas() { undo, redo, } = useWorkflowStore() - const [isExecuting, setIsExecuting] = useState(false) - const [executionResult, setExecutionResult] = useState(null) const { addNotification } = useNotificationStore() // Convert blocks to ReactFlow nodes using local selectedBlockId @@ -236,41 +236,6 @@ function WorkflowCanvas() { } ` - /** - * Handles the execution of the workflow - * Serializes the workflow, executes it, and handles the results - */ - const handleRunWorkflow = async () => { - try { - setIsExecuting(true) - setExecutionResult(null) - - const result = await executeWorkflow( - blocks, - edges, - window.location.pathname.split('/').pop() || 'workflow' - ) - - setExecutionResult(result) - - if (result.success) { - addNotification('console', 'Workflow completed successfully') - } else { - addNotification('error', `Failed to execute workflow: ${result.error}`) - } - } catch (error: any) { - console.error('Error executing workflow:', error) - setExecutionResult({ - success: false, - error: - error instanceof Error ? error.message : 'Unknown error occurred', - }) - addNotification('error', `Failed to execute workflow: ${error.message}`) - } finally { - setIsExecuting(false) - } - } - // Add keyboard shortcut handler useEffect(() => { const handleKeyDown = (event: KeyboardEvent) => { @@ -296,13 +261,6 @@ function WorkflowCanvas() {
- {/* */} { @@ -110,9 +112,13 @@ export function ControlBar() { )} -
diff --git a/app/w/hooks/use-workflow-execution.ts b/app/w/hooks/use-workflow-execution.ts new file mode 100644 index 0000000000..7988487996 --- /dev/null +++ b/app/w/hooks/use-workflow-execution.ts @@ -0,0 +1,48 @@ +import { useState } from 'react' +import { useWorkflowStore } from '@/stores/workflow/workflow-store' +import { useNotificationStore } from '@/stores/notifications/notifications-store' +import { executeWorkflow } from '@/lib/workflow' + +export function useWorkflowExecution() { + const [isExecuting, setIsExecuting] = useState(false) + const [executionResult, setExecutionResult] = useState(null) + const { blocks, edges } = useWorkflowStore() + const { addNotification } = useNotificationStore() + + const handleRunWorkflow = async () => { + try { + setIsExecuting(true) + setExecutionResult(null) + + const result = await executeWorkflow( + blocks, + edges, + window.location.pathname.split('/').pop() || 'workflow' + ) + + setExecutionResult(result) + + if (result.success) { + addNotification('console', 'Workflow completed successfully') + } else { + addNotification('error', `Failed to execute workflow: ${result.error}`) + } + } catch (error: any) { + console.error('Error executing workflow:', error) + setExecutionResult({ + success: false, + error: + error instanceof Error ? error.message : 'Unknown error occurred', + }) + addNotification('error', `Failed to execute workflow: ${error.message}`) + } finally { + setIsExecuting(false) + } + } + + return { + isExecuting, + executionResult, + handleRunWorkflow + } +} \ No newline at end of file