import React from 'react'; import { useTranslation } from 'react-i18next'; import { useQuery } from '@tanstack/react-query'; import { Mail, ExternalLink } from 'lucide-react'; import api from '../api/client'; import { EmailTemplate } from '../types'; interface EmailTemplateSelectorProps { value: string | number | undefined; onChange: (templateId: string | number | undefined) => void; category?: string; placeholder?: string; required?: boolean; disabled?: boolean; className?: string; } const EmailTemplateSelector: React.FC = ({ value, onChange, category, placeholder, required = false, disabled = false, className = '', }) => { const { t } = useTranslation(); // Fetch email templates const { data: templates = [], isLoading } = useQuery({ queryKey: ['email-templates-list', category], queryFn: async () => { const params = new URLSearchParams(); if (category) params.append('category', category); const { data } = await api.get(`/email-templates/?${params.toString()}`); return data.map((t: any) => ({ id: String(t.id), name: t.name, description: t.description, category: t.category, scope: t.scope, updatedAt: t.updated_at, })); }, }); const selectedTemplate = templates.find(t => String(t.id) === String(value)); return (
{/* Selected template info */} {selectedTemplate && (
{selectedTemplate.description || selectedTemplate.name} {t('common.edit', 'Edit')}
)} {/* Empty state with link to create */} {!isLoading && templates.length === 0 && (
{t('emailTemplates.noTemplatesYet', 'No email templates yet.')}{' '} {t('emailTemplates.createFirst', 'Create your first template')}
)}
); }; export default EmailTemplateSelector;