'use client'; import { useState, useMemo } from 'react'; import { useTranslations } from 'next-intl'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Star, Plus } from 'lucide-react'; import { cn } from '@/lib/utils'; import { validateTemplateName } from '@/lib/template-utils'; import { BUILT_IN_PLACEHOLDERS } from '@/lib/template-types'; import type { EmailTemplate } from '@/lib/template-types'; import { useTemplateStore } from '@/stores/template-store'; import { useAuthStore } from '@/stores/auth-store'; interface TemplateFormProps { template?: EmailTemplate; initialData?: { subject?: string; body?: string; to?: string[]; cc?: string[]; bcc?: string[]; }; onSave: (data: Omit) => void; onCancel: () => void; } export function TemplateForm({ template, initialData, onSave, onCancel }: TemplateFormProps) { const t = useTranslations('templates'); const tSettings = useTranslations('settings.templates'); const tComposer = useTranslations('email_composer'); const { identities } = useAuthStore(); const templates = useTemplateStore((s) => s.templates); const [name, setName] = useState(template?.name || ''); const [category, setCategory] = useState(template?.category || ''); const [subject, setSubject] = useState(template?.subject || initialData?.subject || ''); const [body, setBody] = useState(template?.body || initialData?.body || ''); const [toRecipients, setToRecipients] = useState( template?.defaultRecipients?.to?.join(', ') || initialData?.to?.join(', ') || '' ); const [ccRecipients, setCcRecipients] = useState( template?.defaultRecipients?.cc?.join(', ') || initialData?.cc?.join(', ') || '' ); const [bccRecipients, setBccRecipients] = useState( template?.defaultRecipients?.bcc?.join(', ') || initialData?.bcc?.join(', ') || '' ); const [identityId, setIdentityId] = useState(template?.identityId || ''); const [isFavorite, setIsFavorite] = useState(template?.isFavorite || false); const [nameError, setNameError] = useState(null); const [showPlaceholderMenu, setShowPlaceholderMenu] = useState<'subject' | 'body' | null>(null); const existingCategories = useMemo(() => { const cats = new Set(templates.map((t) => t.category).filter(Boolean)); return Array.from(cats).sort(); }, [templates]); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); const error = validateTemplateName(name); if (error) { setNameError(error); return; } const parseRecipients = (val: string) => val.split(',').map((s) => s.trim()).filter(Boolean); const to = parseRecipients(toRecipients); const cc = parseRecipients(ccRecipients); const bcc = parseRecipients(bccRecipients); onSave({ name: name.trim(), subject, body, category: category.trim(), defaultRecipients: to.length || cc.length || bcc.length ? { to: to.length ? to : undefined, cc: cc.length ? cc : undefined, bcc: bcc.length ? bcc : undefined } : undefined, identityId: identityId || undefined, isFavorite, }); }; const insertPlaceholder = (placeholder: string, field: 'subject' | 'body') => { const tag = `{{${placeholder}}}`; if (field === 'subject') { setSubject((prev) => prev + tag); } else { setBody((prev) => prev + tag); } setShowPlaceholderMenu(null); }; return (
{ setName(e.target.value); setNameError(null); }} placeholder={tSettings('name_placeholder')} className={cn('mt-1', nameError && 'border-red-500')} autoFocus /> {nameError && (

{tSettings(`validation.${nameError}`)}

)}
setCategory(e.target.value)} placeholder={tSettings('category_placeholder')} className="mt-1" list="template-categories" /> {existingCategories.length > 0 && ( {existingCategories.map((cat) => ( )}
{showPlaceholderMenu === 'subject' && ( insertPlaceholder(p, 'subject')} onClose={() => setShowPlaceholderMenu(null)} /> )}
setSubject(e.target.value)} placeholder={tSettings('subject_placeholder')} className="mt-1" />
{showPlaceholderMenu === 'body' && ( insertPlaceholder(p, 'body')} onClose={() => setShowPlaceholderMenu(null)} /> )}