/** * AutomationTemplatesView * * View for managing automation templates (CRUD). * System templates (isSystem=true) are read-only for non-SysAdmin, with duplicate option. * Instance templates can be managed by instance admins/editors. */ import React, { useState, useMemo, useEffect } from 'react'; import { useAutomationTemplates, type AutomationTemplate } from '../../../hooks/useAutomations'; import { FormGeneratorTable } from '../../../components/FormGenerator/FormGeneratorTable'; import { AutomationEditor } from '../../../components/AutomationEditor'; import { FaSync, FaPlus, FaFileAlt, FaLock } from 'react-icons/fa'; import { useToast } from '../../../contexts/ToastContext'; import { useCurrentUser } from '../../../hooks/useUsers'; import styles from '../../admin/Admin.module.css'; export const AutomationTemplatesView: React.FC = () => { const { templates, attributes, loading, error, permissions, refetch, fetchTemplates, pagination, createTemplate, updateTemplate, deleteTemplate, duplicateTemplate, getTemplate, } = useAutomationTemplates(); const { user: currentUser } = useCurrentUser(); const isSysAdmin = currentUser?.isSysAdmin || false; const { showSuccess, showError } = useToast(); const [showEditor, setShowEditor] = useState(false); const [editingTemplate, setEditingTemplate] = useState(null); const [saving, setSaving] = useState(false); useEffect(() => { refetch(); }, []); const canCreate = permissions?.create !== 'n'; const canUpdate = permissions?.update !== 'n'; const canDelete = permissions?.delete !== 'n'; const columns = useMemo(() => [ { key: 'label', label: 'Label', type: 'string' as const, sortable: true, searchable: true, width: 200 }, { key: 'overview', label: 'Beschreibung', type: 'string' as const, width: 300 }, { key: 'isSystem', label: 'Typ', type: 'boolean' as const, width: 100, formatter: (value: any) => value ? System : Instanz }, { key: '_createdByUserName', label: 'Erstellt von', type: 'string' as const, width: 150 }, ], []); const handleEditClick = async (template: AutomationTemplate) => { const fullTemplate = await getTemplate(template.id); setEditingTemplate(fullTemplate || template); setShowEditor(true); }; const handleCreateClick = () => { setEditingTemplate(null); setShowEditor(true); }; const handleEditorSave = async (data: Partial) => { setSaving(true); try { if (editingTemplate) { await updateTemplate(editingTemplate.id, data); showSuccess('Vorlage aktualisiert'); } else { await createTemplate(data as any); showSuccess('Vorlage erstellt'); } setShowEditor(false); setEditingTemplate(null); await refetch(); } catch (err: any) { showError(`Fehler: ${err.message}`); } finally { setSaving(false); } }; const handleEditorCancel = () => { setShowEditor(false); setEditingTemplate(null); }; const handleDelete = async (templateId: string): Promise => { try { await deleteTemplate(templateId); showSuccess('Vorlage gelöscht'); return true; } catch (err: any) { showError(`Fehler: ${err.message}`); return false; } }; const handleDuplicate = async (template: AutomationTemplate) => { try { await duplicateTemplate(template.id); showSuccess('Vorlage dupliziert'); await refetch(); } catch (err: any) { showError(`Fehler beim Duplizieren: ${err.message}`); } }; if (error) { return (
⚠️

Fehler beim Laden der Vorlagen: {error}

); } return (

Automation-Vorlagen

Verwalten Sie Ihre Workflow-Vorlagen

{canCreate && ( )}
{loading && (!templates || templates.length === 0) ? (
Lade Vorlagen...
) : !templates || templates.length === 0 ? (

Keine Vorlagen vorhanden

Erstellen Sie eine neue Vorlage für Ihre Workflows.

{canCreate && ( )}
) : ( row.isSystem && !isSysAdmin ? { disabled: true, message: 'System-Vorlagen können nur vom SysAdmin bearbeitet werden' } : !canUpdate ? { disabled: true, message: 'Keine Berechtigung' } : false }, { type: 'delete' as const, title: 'Löschen', disabled: (row: any) => row.isSystem && !isSysAdmin ? { disabled: true, message: 'System-Vorlagen können nur vom SysAdmin gelöscht werden' } : !canDelete ? { disabled: true, message: 'Keine Berechtigung' } : false }, ]} onDelete={(template) => handleDelete(template.id)} hookData={{ refetch: fetchTemplates, pagination, handleDelete, attributes }} emptyMessage="Keine Vorlagen gefunden" /> )}
{showEditor && ( )}
); };