import { asError } from '@/utils/errorTypes'; import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useWorkflows, useDeleteWorkflow, useUpdateWorkflow, } from '@/api/workflows'; import type { Workflow, WorkflowInstance } from '@/api/workflows'; import { Button } from '@/components/ui/Button'; import { Card } from '@/components/ui/Card'; import { Badge } from '@/components/ui/Badge'; import { Skeleton } from '@/components/ui/Skeleton'; import { EmptyState } from '@/components/ui/EmptyState'; import { ConfirmDialog } from '@/components/ui/ConfirmDialog'; import { useToast } from '@/components/ui/Toast'; import { WorkflowEditor } from '@/components/workflows/WorkflowEditor'; import { WorkflowInstanceList } from '@/components/workflows/WorkflowInstanceList'; import { WorkflowInstanceDetail } from '@/components/workflows/WorkflowInstanceDetail'; import { Plus, Settings2, Trash2, Zap, AlertCircle, Workflow as WorkflowIcon, ListOrdered, } from 'lucide-react'; type Tab = 'definitions' | 'instances'; function activeBadgeVariant(isActive: boolean): 'success' | 'secondary' { return isActive ? 'success' : 'secondary'; } export function WorkflowsPage() { const { t } = useTranslation(); const toast = useToast(); const { data, isLoading, isError, refetch } = useWorkflows(1, 50); const deleteMutation = useDeleteWorkflow(); const updateMutation = useUpdateWorkflow(); const [activeTab, setActiveTab] = useState('definitions'); const [showEditor, setShowEditor] = useState(false); const [editingWorkflow, setEditingWorkflow] = useState(null); const [confirmDelete, setConfirmDelete] = useState(null); const [selectedInstanceId, setSelectedInstanceId] = useState(null); const workflows = data?.items ?? []; const handleToggleActive = async (workflow: Workflow) => { try { await updateMutation.mutateAsync({ id: workflow.id, data: { is_active: !workflow.is_active }, }); toast.success( workflow.is_active ? 'Workflow deaktiviert' : 'Workflow aktiviert' ); } catch (err: unknown) { const errObj = asError(err); toast.error(errObj.message || 'Fehler beim Umschalten'); } }; const handleDelete = async () => { if (!confirmDelete) return; try { await deleteMutation.mutateAsync(confirmDelete.id); toast.success('Workflow geloescht'); setConfirmDelete(null); } catch (err: unknown) { const errObj = asError(err); toast.error(errObj.message || 'Fehler beim Loeschen'); } }; const openCreate = () => { setEditingWorkflow(null); setShowEditor(true); }; const openEdit = (workflow: Workflow) => { setEditingWorkflow(workflow); setShowEditor(true); }; return (
{/* Header */}

{t('workflows.title', 'Workflows')}

Definieren und verwalten Sie automatisierte Workflows

{activeTab === 'definitions' && ( )}
{/* Tabs */}
{/* Definitions Tab */} {activeTab === 'definitions' && ( <> {isLoading && (
{[1, 2, 3].map((i) => ( ))}
)} {isError && (
Fehler beim Laden der Workflows
)} {!isLoading && !isError && workflows.length === 0 && ( } action={ } /> )} {!isLoading && !isError && workflows.length > 0 && (
{workflows.map((wf) => (

{wf.name}

{wf.is_active ? 'Aktiv' : 'Inaktiv'} {wf.trigger_event && (
{wf.trigger_event}
)}
{wf.description && (

{wf.description}

)}
{wf.steps?.length || 0} Schritte {wf.created_at && ( Erstellt: {new Date(wf.created_at).toLocaleDateString()} )}
))}
)} )} {/* Instances Tab */} {activeTab === 'instances' && ( setSelectedInstanceId(inst.id) } /> )} {/* Workflow Editor Modal */} { setShowEditor(false); setEditingWorkflow(null); }} /> {/* Instance Detail Modal */} {selectedInstanceId && ( setSelectedInstanceId(null)} /> )} {/* Delete Confirmation */} setConfirmDelete(null)} onConfirm={handleDelete} title="Workflow loeschen" message={`Moechten Sie den Workflow "${confirmDelete?.name}" wirklich loeschen?`} confirmLabel="Loeschen" variant="danger" />
); }