import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Button, Text, Title, Stack, Group, ActionIcon, Progress, Card, Alert } from '@mantine/core'; import ArrowBackIcon from '@mui/icons-material/ArrowBack'; import PlayArrowIcon from '@mui/icons-material/PlayArrow'; import CheckIcon from '@mui/icons-material/Check'; import ErrorIcon from '@mui/icons-material/Error'; import { useFileContext } from '../../../contexts/FileContext'; import { useFlatToolRegistry } from '../../../data/useTranslatedToolRegistry'; import { executeAutomationSequence } from '../../../utils/automationExecutor'; interface AutomationRunProps { automation: any; onBack: () => void; onComplete: () => void; automateOperation?: any; // Add the operation hook to store results } interface ExecutionStep { id: string; operation: string; name: string; status: 'pending' | 'running' | 'completed' | 'error'; error?: string; } export default function AutomationRun({ automation, onBack, onComplete, automateOperation }: AutomationRunProps) { const { t } = useTranslation(); const { activeFiles, consumeFiles } = useFileContext(); const toolRegistry = useFlatToolRegistry(); const [isExecuting, setIsExecuting] = useState(false); const [executionSteps, setExecutionSteps] = useState([]); const [currentStepIndex, setCurrentStepIndex] = useState(-1); const [currentFiles, setCurrentFiles] = useState([]); // Initialize execution steps from automation React.useEffect(() => { if (automation?.operations) { const steps = automation.operations.map((op: any, index: number) => { const tool = toolRegistry[op.operation]; return { id: `${op.operation}-${index}`, operation: op.operation, name: tool?.name || op.operation, status: 'pending' as const }; }); setExecutionSteps(steps); } // Initialize current files with active files if (activeFiles) { setCurrentFiles([...activeFiles]); } }, [automation, toolRegistry, activeFiles]); const executeAutomation = async () => { if (!activeFiles || activeFiles.length === 0) { // Show error - need files to execute automation return; } setIsExecuting(true); setCurrentStepIndex(0); try { // Execute the automation sequence using the new executor const finalResults = await executeAutomationSequence( automation, activeFiles, (stepIndex: number, operationName: string) => { // Step started setCurrentStepIndex(stepIndex); setExecutionSteps(prev => prev.map((step, idx) => idx === stepIndex ? { ...step, status: 'running' } : step )); }, (stepIndex: number, resultFiles: File[]) => { // Step completed setExecutionSteps(prev => prev.map((step, idx) => idx === stepIndex ? { ...step, status: 'completed' } : step )); setCurrentFiles(resultFiles); }, (stepIndex: number, error: string) => { // Step failed setExecutionSteps(prev => prev.map((step, idx) => idx === stepIndex ? { ...step, status: 'error', error } : step )); } ); // All steps completed successfully setCurrentStepIndex(-1); setIsExecuting(false); setCurrentFiles(finalResults); // Properly integrate results with FileContext if (finalResults.length > 0) { console.log(`🎨 Integrating ${finalResults.length} result files with FileContext`); // Use FileContext's consumeFiles to properly add results // This replaces input files with output files (like other tools do) await consumeFiles(activeFiles, finalResults); console.log(`✅ Successfully integrated automation results with FileContext`); } } catch (error: any) { console.error('Automation execution failed:', error); setIsExecuting(false); setCurrentStepIndex(-1); } }; const getStepIcon = (step: ExecutionStep) => { switch (step.status) { case 'completed': return ; case 'error': return ; case 'running': return
; default: return
; } }; const getProgress = () => { const completedSteps = executionSteps.filter(step => step.status === 'completed').length; return (completedSteps / executionSteps.length) * 100; }; const allStepsCompleted = executionSteps.every(step => step.status === 'completed'); const hasErrors = executionSteps.some(step => step.status === 'error'); return (
{/* Automation Info */} {automation?.name || t('automate.sequence.unnamed', 'Unnamed Automation')} {t('automate.sequence.steps', '{{count}} steps', { count: executionSteps.length })} {/* Progress Bar */} {isExecuting && (
{t('automate.sequence.progress', 'Progress: {{current}}/{{total}}', { current: currentStepIndex + 1, total: executionSteps.length })}
)} {/* Execution Steps */} {executionSteps.map((step, index) => ( {index + 1} {getStepIcon(step)}
{step.name} {step.error && ( {step.error} )}
))}
{/* Action Buttons */} {(allStepsCompleted || hasErrors) && ( )}
); }