'use client' import { useState } from 'react' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '../ui/dialog' import { Button } from '../ui/button' import { Input } from '../ui/input' import { Label } from '../ui/label' import { Checkbox } from '../ui/checkbox' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '../ui/select' import { Loader2, Send, AlertCircle } from 'lucide-react' import { useToast } from '../toast' interface TestSendRecipient { id: string name: string email?: string | null } interface TestSendPayload { testEmail: string selectedRecipientId: string | undefined includeTracking: boolean } interface TestSendDialogProps { open: boolean onOpenChange: (open: boolean) => void subject: string /** Sample recipients whose data can be used for merge field substitution */ recipients?: TestSendRecipient[] /** Default sample option label shown when no specific recipient is chosen */ defaultSampleLabel?: string /** Callback to send the test email. Receives the test send configuration. */ onSendTest: (payload: TestSendPayload) => Promise /** Merge field help text */ mergeFieldHint?: string } export function TestSendDialog({ open, onOpenChange, subject, recipients = [], defaultSampleLabel = 'Sample Data (John Smith)', onSendTest, mergeFieldHint = 'Merge fields like {{recipient.firstName}} will use this recipient\'s data', }: TestSendDialogProps) { const { toast } = useToast() const [sending, setSending] = useState(false) const [testEmail, setTestEmail] = useState('') const [selectedRecipientId, setSelectedRecipientId] = useState('sample') const [includeTracking, setIncludeTracking] = useState(false) const handleSendTest = async () => { if (!testEmail) { toast({ description: 'Please enter a test email address', variant: 'destructive' }) return } setSending(true) try { await onSendTest({ testEmail, selectedRecipientId: selectedRecipientId !== 'sample' ? selectedRecipientId : undefined, includeTracking, }) toast({ description: `Test email sent to ${testEmail}`, }) onOpenChange(false) } catch (error) { console.error('Error sending test:', error) toast({ description: error instanceof Error ? error.message : 'Failed to send test email', variant: 'destructive' }) } finally { setSending(false) } } return ( Send Test Email Send a test version to yourself to preview how it will look in your inbox.
{/* Subject preview */}

[TEST] {subject || '(no subject)'}

{/* Test email input */}
setTestEmail(e.target.value)} placeholder="your-email@example.com" />
{/* Sample recipient data for merge fields */}

{mergeFieldHint}

{/* Tracking option */}
setIncludeTracking(checked as boolean)} />
{/* Warning */}

Test emails are marked with [TEST] in the subject and include a banner indicating it's a test.

) }