'use client'; import { useState } from 'react'; import { Copy, FileText, Mail } from 'lucide-react'; import { useAction } from 'next-safe-action/hooks'; import { formatDate } from '@kit/shared/dates'; import { Badge } from '@kit/ui/badge'; import { Button } from '@kit/ui/button'; import { Card, CardContent, CardHeader, CardTitle } from '@kit/ui/card'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@kit/ui/dialog'; import { Input } from '@kit/ui/input'; import { toast } from '@kit/ui/sonner'; import { cloneTemplate } from '../server/actions/hierarchy-actions'; interface SharedTemplate { id: string; account_id: string; account_name: string; template_source: 'newsletter' | 'document'; name: string; description: string | null; template_type: string; shared_with_hierarchy: boolean; created_at: string; } interface SharedTemplatesProps { accountId: string; templates: SharedTemplate[]; } type FilterType = 'all' | 'newsletter' | 'document'; export function SharedTemplates({ accountId, templates, }: SharedTemplatesProps) { const [filter, setFilter] = useState('all'); const [cloneDialogOpen, setCloneDialogOpen] = useState(false); const [selectedTemplate, setSelectedTemplate] = useState(null); const [newName, setNewName] = useState(''); const { execute: executeClone, isPending: isCloning } = useAction( cloneTemplate, { onSuccess: () => { toast.success('Vorlage wurde erfolgreich geklont'); setCloneDialogOpen(false); setSelectedTemplate(null); setNewName(''); }, onError: ({ error }) => { toast.error(error.serverError ?? 'Fehler beim Klonen der Vorlage'); }, }, ); const filteredTemplates = templates.filter((t) => { if (filter === 'all') return true; return t.template_source === filter; }); function openCloneDialog(template: SharedTemplate) { setSelectedTemplate(template); setNewName(`${template.name} (Kopie)`); setCloneDialogOpen(true); } function handleClone() { if (!selectedTemplate) return; executeClone({ templateType: selectedTemplate.template_source, templateId: selectedTemplate.id, targetAccountId: accountId, newName: newName.trim() || undefined, }); } const filterButtons: { value: FilterType; label: string }[] = [ { value: 'all', label: 'Alle' }, { value: 'newsletter', label: 'Newsletter' }, { value: 'document', label: 'Dokumente' }, ]; function getTemplateTypeLabel(type: string): string { const labels: Record = { generic: 'Allgemein', member_card: 'Mitgliedsausweis', invoice: 'Rechnung', certificate: 'Urkunde', confirmation: 'Bestätigung', letter: 'Brief', }; return labels[type] ?? type; } return ( <>
Geteilte Vorlagen
{filterButtons.map((btn) => ( ))}
{filteredTemplates.length === 0 ? (

Keine geteilten Vorlagen vorhanden

Vorlagen, die von anderen Organisationen in Ihrer Hierarchie geteilt werden, erscheinen hier.

) : (
{filteredTemplates.map((template) => ( ))}
Name Typ Template-Typ Organisation Erstellt Aktion
{template.template_source === 'newsletter' ? ( ) : ( )} {template.name}
{template.description && (

{template.description}

)}
{template.template_source === 'newsletter' ? 'Newsletter' : 'Dokument'} {getTemplateTypeLabel(template.template_type)} {template.account_name} {formatDate(template.created_at)}
)}
Vorlage klonen Erstellen Sie eine Kopie der Vorlage{' '} {selectedTemplate?.name} in Ihrer Organisation.
setNewName(e.target.value)} placeholder="Name der neuen Vorlage" maxLength={200} />
); }