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'; interface AutomationRunProps { automation: any; onBack: () => void; onComplete: () => void; } interface ExecutionStep { id: string; operation: string; name: string; status: 'pending' | 'running' | 'completed' | 'error'; error?: string; } export default function AutomationRun({ automation, onBack, onComplete }: AutomationRunProps) { const { t } = useTranslation(); const { activeFiles } = useFileContext(); const [isExecuting, setIsExecuting] = useState(false); const [executionSteps, setExecutionSteps] = useState([]); const [currentStepIndex, setCurrentStepIndex] = useState(-1); // Initialize execution steps from automation React.useEffect(() => { if (automation?.operations) { const steps = automation.operations.map((op: any, index: number) => ({ id: `${op.operation}-${index}`, operation: op.operation, name: op.operation, // You might want to get the display name from tool registry status: 'pending' as const })); setExecutionSteps(steps); } }, [automation]); const executeAutomation = async () => { if (!activeFiles || activeFiles.length === 0) { // Show error - need files to execute automation return; } setIsExecuting(true); setCurrentStepIndex(0); try { for (let i = 0; i < executionSteps.length; i++) { setCurrentStepIndex(i); // Update step status to running setExecutionSteps(prev => prev.map((step, idx) => idx === i ? { ...step, status: 'running' } : step )); // Simulate step execution (replace with actual tool execution) await new Promise(resolve => setTimeout(resolve, 2000)); // Update step status to completed setExecutionSteps(prev => prev.map((step, idx) => idx === i ? { ...step, status: 'completed' } : step )); } setCurrentStepIndex(-1); setIsExecuting(false); // All steps completed - show success } catch (error) { // Handle error setExecutionSteps(prev => prev.map((step, idx) => idx === currentStepIndex ? { ...step, status: 'error', error: error?.toString() } : step )); setIsExecuting(false); } }; 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) && ( )}
); }