/** * TemplatePicker - modal to browse and select a workflow template for creating a new workflow. */ import React, { useState, useEffect, useCallback, useMemo } from 'react'; import { FaSpinner } from 'react-icons/fa'; import { fetchTemplates, type AutoWorkflowTemplate, type AutoTemplateScope, type ApiRequestFunction, } from '../../../api/workflowApi'; import styles from './Automation2FlowEditor.module.css'; import { useLanguage } from '../../../providers/language/LanguageContext'; interface TemplatePickerProps { open: boolean; onClose: () => void; onSelect: (templateId: string) => void; instanceId: string; request: ApiRequestFunction; } export const TemplatePicker: React.FC = ({ open, onClose, onSelect, instanceId, request, }) => { const { t } = useLanguage(); const scopeLabels = useMemo( () => ({ all: t('Alle'), user: t('Meine'), instance: t('Instanz'), mandate: t('Mandant'), system: t('System'), }) as Record, [t] ); const [templates, setTemplates] = useState([]); const [loading, setLoading] = useState(false); const [activeScope, setActiveScope] = useState('all'); const [copying, setCopying] = useState(null); const _load = useCallback(async () => { if (!instanceId || !open) return; setLoading(true); try { const scope = activeScope === 'all' ? undefined : activeScope; const result = await fetchTemplates(request, instanceId, scope); setTemplates(Array.isArray(result) ? result : result.items); } catch { setTemplates([]); } finally { setLoading(false); } }, [instanceId, request, open, activeScope]); useEffect(() => { _load(); }, [_load]); const _handleSelect = useCallback( async (templateId: string) => { setCopying(templateId); try { await onSelect(templateId); } finally { setCopying(null); } }, [onSelect] ); if (!open) return null; return (

{t('Neu aus Vorlage')}

{t('Wählen Sie eine Vorlage, um einen neuen Workflow zu erstellen.')}

{(['all', 'user', 'instance', 'mandate', 'system'] as const).map((s) => ( ))}
{loading ? (
) : templates.length === 0 ? (
{t('Keine Vorlagen gefunden.')}
) : ( {templates.map((tpl) => ( ))}
{t('Name')} {t('Scope')}
{tpl.label} {scopeLabels[(tpl.templateScope as AutoTemplateScope) || 'user']}
)}
); };