'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 { formatEmailForReply, formatEmailForForward } from '@/lib/email-formatter'; interface EmailObject { id?: string; from?: string; fromName?: string; to?: string; subject?: string; content?: string; html?: string; text?: string; body?: string; date?: string; read?: boolean; starred?: boolean; attachments?: { name: string; url: string }[]; folder?: string; cc?: string; bcc?: string; } 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?: EmailObject | null; forwardFrom?: EmailObject | 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); const composeBodyRef = useRef(null); useEffect(() => { if (editorRef.current) { editorRef.current.focus(); } }, [showCompose]); // Initialize content when replying or forwarding useEffect(() => { // Initialize reply if replyTo is provided if (replyTo) { // For reply/reply-all const formattedEmail = formatEmailForReply(replyTo as any, 'reply'); setComposeTo(formattedEmail.to); setComposeSubject(formattedEmail.subject); // Use the body but preserve the original UI styling // Extract just the content portion from the client formatter output // and apply the original styling let bodyContent = formattedEmail.body; // Apply DOMPurify to the content for safety bodyContent = DOMPurify.sanitize(bodyContent, { ADD_TAGS: ['style'], FORBID_TAGS: ['script', 'iframe'] }); setComposeBody(bodyContent); // Show CC if needed for reply-all if (formattedEmail.cc) { setComposeCc(formattedEmail.cc); setShowCc(true); } } else if (forwardFrom) { // Initialize forward email if forwardFrom is provided initializeForwardedEmail(forwardFrom); } }, [replyTo, forwardFrom]); // Initialize forwarded email content const initializeForwardedEmail = async (email: any) => { if (!email) return; console.log('Initializing forwarded email:', email); // Use our client-side formatter const formattedEmail = formatEmailForForward(email); // Set the formatted subject with Fwd: prefix setComposeSubject(formattedEmail.subject); // Create header for forwarded email - use the original styling const headerHtml = formattedEmail.headerHtml; // 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'] }); } else if (email.html) { contentHtml = DOMPurify.sanitize(email.html, { ADD_TAGS: ['style'], FORBID_TAGS: ['script', 'iframe'] }); } else if (email.text) { contentHtml = `
${email.text}
`; } else if (email.body) { contentHtml = DOMPurify.sanitize(email.body, { ADD_TAGS: ['style'], FORBID_TAGS: ['script', 'iframe'] }); } // Set body with header and content, preserving the original UI layout 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]); } }; // Body input area const renderBodyInput = () => (
{ const target = e.target as HTMLDivElement; setComposeBody(target.innerHTML); }} ref={composeBodyRef} style={{ direction: 'ltr' }} /> ); return (
{replyTo ? 'Reply' : forwardFrom ? 'Forward' : 'New Message'}
To: setComposeTo(e.target.value)} />
{showCc && (
Cc: setComposeCc(e.target.value)} />
)} {showBcc && (
Bcc: setComposeBcc(e.target.value)} />
)}
Subject: setComposeSubject(e.target.value)} />
{renderBodyInput()} {attachments.length > 0 && (

Attachments:

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