import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useAutomations, useCreateAutomation, useUpdateAutomation, useDeleteAutomation, useExecuteAutomation, useDryRunAutomation, useAutomationRuns, useAutomationVersions, useRestoreAutomationVersion, } from '@/api/automation'; import { Button } from '@/components/ui/Button'; import { Card } from '@/components/ui/Card'; import { Badge } from '@/components/ui/Badge'; import { Modal } from '@/components/ui/Modal'; import { Select } from '@/components/ui/Select'; import { EmptyState } from '@/components/ui/EmptyState'; import { Skeleton } from '@/components/ui/Skeleton'; import { ConfirmDialog } from '@/components/ui/ConfirmDialog'; import { useToast } from '@/components/ui/Toast'; import type { AutomationDefinition, AutomationCondition, AutomationAction } from '@/types/automation'; import { Plus, Play, RotateCcw, History, GitBranch, Trash2, Settings2, Workflow, Clock, Zap, AlertCircle, CheckCircle2, XCircle, Loader2, } from 'lucide-react'; const triggerTypeOptions = [ { value: 'event', label: 'Event' }, { value: 'schedule', label: 'Schedule' }, { value: 'manual', label: 'Manual' }, ]; const actionTypeOptions = [ { value: 'api_call', label: 'API Call' }, { value: 'notification', label: 'Notification' }, { value: 'workflow_start', label: 'Workflow Start' }, ]; const conditionOperatorOptions = [ { value: 'equals', label: 'Equals' }, { value: 'not_equals', label: 'Not Equals' }, { value: 'contains', label: 'Contains' }, { value: 'greater_than', label: 'Greater Than' }, { value: 'less_than', label: 'Less Than' }, ]; function statusBadgeVariant(status: string): 'success' | 'warning' | 'danger' | 'secondary' { switch (status) { case 'active': return 'success'; case 'inactive': return 'warning'; default: return 'secondary'; } } function runStatusBadgeVariant(status: string): 'success' | 'warning' | 'danger' | 'info' { switch (status) { case 'completed': return 'success'; case 'running': return 'info'; case 'failed': return 'danger'; case 'cancelled': return 'warning'; default: return 'warning'; } } function triggerIcon(type: string) { switch (type) { case 'event': return ; case 'schedule': return ; default: return ; } } interface AutomationFormData { name: string; description: string; trigger_type: 'event' | 'schedule' | 'manual'; trigger_config: Record; conditions: AutomationCondition[]; actions: AutomationAction[]; active: boolean; dry_run: boolean; } const emptyForm: AutomationFormData = { name: '', description: '', trigger_type: 'manual', trigger_config: {}, conditions: [], actions: [], active: true, dry_run: false, }; function AutomationForm({ initial, onSave, onCancel, isSaving, }: { initial?: AutomationDefinition; onSave: (data: Partial) => void; onCancel: () => void; isSaving: boolean; }) { const { t } = useTranslation(); const [form, setForm] = useState(() => { if (initial) { return { name: initial.name, description: initial.description || '', trigger_type: initial.trigger_type, trigger_config: (initial.trigger_config as Record) || {}, conditions: initial.conditions || [], actions: initial.actions || [], active: initial.active, dry_run: initial.dry_run, }; } return { ...emptyForm }; }); const updateField = (key: K, value: AutomationFormData[K]) => { setForm((prev) => ({ ...prev, [key]: value })); }; const addCondition = () => { setForm((prev) => ({ ...prev, conditions: [...prev.conditions, { field: '', operator: 'equals', value: '' }], })); }; const updateCondition = (index: number, field: keyof AutomationCondition, value: string) => { setForm((prev) => { const conditions = [...prev.conditions]; conditions[index] = { ...conditions[index], [field]: value }; return { ...prev, conditions }; }); }; const removeCondition = (index: number) => { setForm((prev) => ({ ...prev, conditions: prev.conditions.filter((_, i) => i !== index), })); }; const addAction = () => { setForm((prev) => ({ ...prev, actions: [...prev.actions, { type: 'api_call', config: {} }], })); }; const updateAction = (index: number, field: 'type' | 'config', value: string | Record) => { setForm((prev) => { const actions = [...prev.actions]; if (field === 'type') { actions[index] = { ...actions[index], type: value as 'api_call' | 'notification' | 'workflow_start' }; } else { actions[index] = { ...actions[index], config: value as Record }; } return { ...prev, actions }; }); }; const removeAction = (index: number) => { setForm((prev) => ({ ...prev, actions: prev.actions.filter((_, i) => i !== index), })); }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); onSave({ name: form.name, description: form.description || undefined, trigger_type: form.trigger_type, trigger_config: form.trigger_config, conditions: form.conditions, actions: form.actions, active: form.active, dry_run: form.dry_run, }); }; return (
{/* Name & Description */}
updateField('name', e.target.value)} required className="w-full rounded-lg border border-secondary-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-primary-500" placeholder="My Automation" />