'use client'; import { useState, useRef, useEffect } from 'react'; import { X, Paperclip, ChevronDown, ChevronUp, SendHorizontal, Loader2 } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Card, CardContent, CardHeader, CardTitle, CardFooter } from '@/components/ui/card'; import DOMPurify from 'isomorphic-dompurify'; import { formatEmailForReplyOrForward } from '@/lib/services/email-service'; import { Email } from '@/app/courrier/page'; interface ComposeEmailProps { showCompose: boolean; setShowCompose: (show: boolean) => void; composeTo: string; setComposeTo: (to: string) => void; composeCc: string; setComposeCc: (cc: string) => void; composeBcc: string; setComposeBcc: (bcc: string) => void; composeSubject: string; setComposeSubject: (subject: string) => void; composeBody: string; setComposeBody: (body: string) => void; showCc: boolean; setShowCc: (show: boolean) => void; showBcc: boolean; setShowBcc: (show: boolean) => void; attachments: any[]; setAttachments: (attachments: any[]) => void; handleSend: () => Promise; originalEmail?: { content: string; type: 'reply' | 'reply-all' | 'forward'; }; onSend: (email: any) => Promise; onCancel: () => void; replyTo?: Email | null; forwardFrom?: Email | null; } export default function ComposeEmail({ showCompose, setShowCompose, composeTo, setComposeTo, composeCc, setComposeCc, composeBcc, setComposeBcc, composeSubject, setComposeSubject, composeBody, setComposeBody, showCc, setShowCc, showBcc, setShowBcc, attachments, setAttachments, handleSend, originalEmail, replyTo, forwardFrom, onSend, onCancel }: ComposeEmailProps) { const [sending, setSending] = useState(false); const editorRef = useRef(null); const fileInputRef = useRef(null); useEffect(() => { if (editorRef.current) { editorRef.current.focus(); } }, [showCompose]); // Initialize content when replying or forwarding useEffect(() => { // Handle reply or forward initialization if (replyTo) { // For reply/reply-all const formattedEmail = formatEmailForReplyOrForward(replyTo as any, 'reply'); setComposeTo(formattedEmail.to); setComposeSubject(formattedEmail.subject); setComposeBody(formattedEmail.body); // Show CC if needed for reply-all if (formattedEmail.cc) { setComposeCc(formattedEmail.cc); setShowCc(true); } } else if (forwardFrom) { // For forward initializeForwardedEmail(forwardFrom); } }, [replyTo, forwardFrom]); // Initialize forwarded email content const initializeForwardedEmail = async (email: any) => { if (!email) return; // Format subject with Fwd: prefix if needed const subjectBase = email.subject || '(No subject)'; const subjectRegex = /^(Fwd|FW|Forward):\s*/i; const subject = subjectRegex.test(subjectBase) ? subjectBase : `Fwd: ${subjectBase}`; setComposeSubject(subject); // Create header for forwarded email const headerHtml = `
---------- Forwarded message ---------
From: ${email.fromName || email.from}
Date: ${new Date(email.date).toLocaleString()}
Subject: ${email.subject || '(No subject)'}
To: ${email.to}
`; // Prepare content let contentHtml = '
No content available
'; if (email.content) { // Sanitize the content contentHtml = DOMPurify.sanitize(email.content, { ADD_TAGS: ['style'], FORBID_TAGS: ['script', 'iframe'] }); } // Set body with header and content setComposeBody(` ${headerHtml}
${contentHtml}
`); }; if (!showCompose) return null; const handleFileSelection = (e: React.ChangeEvent) => { if (e.target.files && e.target.files.length > 0) { const newAttachments = Array.from(e.target.files).map(file => ({ name: file.name, type: file.type, size: file.size, file })); setAttachments([...attachments, ...newAttachments]); } }; return (
{replyTo ? 'Reply' : forwardFrom ? 'Forward' : 'New Message'}
To: setComposeTo(e.target.value)} className="border-0 focus-visible:ring-0" placeholder="recipient@example.com" />
{showCc && (
Cc: setComposeCc(e.target.value)} className="border-0 focus-visible:ring-0" placeholder="cc@example.com" />
)} {showBcc && (
Bcc: setComposeBcc(e.target.value)} className="border-0 focus-visible:ring-0" placeholder="bcc@example.com" />
)}
Subject: setComposeSubject(e.target.value)} className="border-0 focus-visible:ring-0" placeholder="Subject" />
{!showCc && ( )} {!showBcc && ( )}
setComposeBody(e.currentTarget.innerHTML)} /> {attachments.length > 0 && (

Attachments

{attachments.map((attachment, index) => (
{attachment.name}
))}
)}
); }