diff --git a/components/email/ComposeEmail.tsx b/components/email/ComposeEmail.tsx index ae8fa729..5f3d607f 100644 --- a/components/email/ComposeEmail.tsx +++ b/components/email/ComposeEmail.tsx @@ -6,6 +6,7 @@ import { X, Paperclip, ChevronDown, ChevronUp, SendHorizontal, Loader2 } from 'l import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Card, CardContent, CardHeader, CardTitle, CardFooter } from '@/components/ui/card'; +import { decodeEmail } from '@/lib/mail-parser-wrapper'; interface ComposeEmailProps { initialEmail?: EmailMessage | null; @@ -54,18 +55,24 @@ export default function ComposeEmail({ // Initialize the form when replying to or forwarding an email useEffect(() => { if (initialEmail && type !== 'new') { - const formattedEmail = formatEmailForReplyOrForward(initialEmail, type as 'reply' | 'reply-all' | 'forward'); - - setTo(formattedEmail.to); - - if (formattedEmail.cc) { - setCc(formattedEmail.cc); - setShowCc(true); + // If it's a forward, use the same approach as Panel 3 + if (type === 'forward') { + initializeForwardedEmail(); + } else { + // For reply/reply-all, continue using formatEmailForReplyOrForward + const formattedEmail = formatEmailForReplyOrForward(initialEmail, type as 'reply' | 'reply-all'); + + setTo(formattedEmail.to); + + if (formattedEmail.cc) { + setCc(formattedEmail.cc); + setShowCc(true); + } + + setSubject(formattedEmail.subject); + setBody(formattedEmail.body); } - setSubject(formattedEmail.subject); - setBody(formattedEmail.body); - // Focus editor after initializing setTimeout(() => { if (editorRef.current) { @@ -85,6 +92,69 @@ export default function ComposeEmail({ } }, [initialEmail, type]); + // New function to initialize forwarded email using same approach as Panel 3 + const initializeForwardedEmail = async () => { + if (!initialEmail || !initialEmail.content) { + console.error('No email content available for forwarding'); + setBody('
No content available
'); + return; + } + + try { + setSending(true); // Use sending state to show loading + + // Use the same decoding approach as Panel 3 + const decoded = await decodeEmail(initialEmail.content); + + // Format subject with Fwd: prefix if needed + const cleanSubject = initialEmail.subject.replace(/^(Fwd|FW|Forward):\s*/i, '').trim(); + const formattedSubject = initialEmail.subject.match(/^(Fwd|FW|Forward):/i) + ? initialEmail.subject + : `Fwd: ${cleanSubject}`; + + setSubject(formattedSubject); + + // Format date for the forwarded message header + const formatDate = (date: Date | null): string => { + if (!date) return ''; + + return date.toLocaleString('en-US', { + weekday: 'short', + year: 'numeric', + month: 'short', + day: 'numeric', + hour: '2-digit', + minute: '2-digit' + }); + }; + + // Create the same forwarded message format as Panel 3 + const formattedContent = ` +
+
+---------- Forwarded message ---------
+
+From: ${decoded.from || ''}
+
+Date: ${formatDate(decoded.date)}
+
+Subject: ${decoded.subject || ''}
+
+To: ${decoded.to || ''}
+
+${decoded.html || `
${decoded.text || ''}
`} +
+ `; + + setBody(formattedContent); + } catch (error) { + console.error('Error initializing forwarded email:', error); + setBody('
Error loading forwarded content
'); + } finally { + setSending(false); + } + }; + // Handle attachment selection const handleAttachmentClick = () => { attachmentInputRef.current?.click();