import { useState } from 'react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { toast } from '@/hooks/use-toast'; import { Eye, Send, Mail } from 'lucide-react'; import { EmailTemplateRenderer, ShortcodeProcessor } from '@/lib/email-templates/master-template'; interface NotificationTemplate { id: string; key: string; name: string; is_active: boolean; email_subject: string; email_body_html: string; webhook_url: string; last_payload_example?: Record | null; } interface EmailTemplatePreviewProps { template: NotificationTemplate; onTest?: (template: NotificationTemplate) => void; isTestSending?: boolean; } export function EmailTemplatePreview({ template, onTest, isTestSending = false }: EmailTemplatePreviewProps) { const [previewMode, setPreviewMode] = useState<'master' | 'content'>('master'); const [testEmail, setTestEmail] = useState(''); const [showTestForm, setShowTestForm] = useState(false); // Generate preview with dummy shortcode data const generatePreview = () => { const processedSubject = ShortcodeProcessor.process(template.email_subject); const processedContent = ShortcodeProcessor.process(template.email_body_html); if (previewMode === 'master') { const fullHtml = EmailTemplateRenderer.render({ subject: processedSubject, content: processedContent, brandName: 'ACCESS HUB' }); return fullHtml; } else { return processedContent; } }; const handleTestEmail = async () => { if (!testEmail) { toast({ title: 'Error', description: 'Masukkan email tujuan', variant: 'destructive' }); return; } if (onTest) { await onTest({ ...template, test_email: testEmail }); } }; const previewHtml = generatePreview(); return (
{/* Preview Controls */}
{/* Test Email Form */} {showTestForm && (
setTestEmail(e.target.value)} placeholder="test@example.com" className="flex-1" />

This will send a test email with dummy data for all available shortcodes.

)} {/* Preview Info */} {previewMode === 'master' ? 'Showing complete email template with header, footer, and styling applied.' : 'Showing only the content section without master template styling.' } {/* Email Preview */}
{previewMode === 'master' ? 'Full Email Preview' : 'Content Preview'}