diff --git a/components/ComposeEmail.tsx b/components/ComposeEmail.tsx index 4c5c656a..b730f479 100644 --- a/components/ComposeEmail.tsx +++ b/components/ComposeEmail.tsx @@ -9,6 +9,7 @@ import { Textarea } from '@/components/ui/textarea'; import { decodeComposeContent, encodeComposeContent } from '@/lib/compose-mime-decoder'; import { Email } from '@/app/courrier/page'; import mime from 'mime'; +import { simpleParser } from 'mailparser'; interface ComposeEmailProps { showCompose: boolean; @@ -90,49 +91,79 @@ export default function ComposeEmail({ // Get the original email content const originalContent = replyTo?.body || forwardFrom?.body || ''; - // Create the reply/forward structure - content = ` -
-
- ${forwardFrom ? ` -
- ---------- Forwarded message ---------
- From: ${forwardFrom.from}
- Date: ${new Date(forwardFrom.date).toLocaleString()}
- Subject: ${forwardFrom.subject}
- To: ${forwardFrom.to}
- ${forwardFrom.cc ? `Cc: ${forwardFrom.cc}
` : ''} -
- ${originalContent} -
- ` : ` -
- On ${new Date(replyTo?.date || '').toLocaleString()}, ${replyTo?.from} wrote: -
-
- ${originalContent} -
- `} -
- `; + // Parse the original email content using the API + fetch('/api/parse-email', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ emailContent: originalContent }), + }) + .then(response => response.json()) + .then(parsed => { + // Create the reply/forward structure + content = ` +
+
+ ${forwardFrom ? ` +
+ ---------- Forwarded message ---------
+ From: ${forwardFrom.from}
+ Date: ${new Date(forwardFrom.date).toLocaleString()}
+ Subject: ${forwardFrom.subject}
+ To: ${forwardFrom.to}
+ ${forwardFrom.cc ? `Cc: ${forwardFrom.cc}
` : ''} +
+ ${parsed.html || parsed.text} +
+ ` : ` +
+ On ${new Date(replyTo?.date || '').toLocaleString()}, ${replyTo?.from} wrote: +
+
+ ${parsed.html || parsed.text} +
+ `} +
+ `; + + if (composeBodyRef.current) { + composeBodyRef.current.innerHTML = content; + setIsInitialized(true); + + // Place cursor at the beginning of the compose area + const composeArea = composeBodyRef.current.querySelector('.compose-area'); + if (composeArea) { + const range = document.createRange(); + const sel = window.getSelection(); + range.setStart(composeArea, 0); + range.collapse(true); + sel?.removeAllRanges(); + sel?.addRange(range); + (composeArea as HTMLElement).focus(); + } + } + }) + .catch(error => { + console.error('Error parsing email:', error); + }); } else { // For new messages content = `
`; - } - - composeBodyRef.current.innerHTML = content; - setIsInitialized(true); - - // Place cursor at the beginning of the compose area - const composeArea = composeBodyRef.current.querySelector('.compose-area'); - if (composeArea) { - const range = document.createRange(); - const sel = window.getSelection(); - range.setStart(composeArea, 0); - range.collapse(true); - sel?.removeAllRanges(); - sel?.addRange(range); - (composeArea as HTMLElement).focus(); + composeBodyRef.current.innerHTML = content; + setIsInitialized(true); + + // Place cursor at the beginning of the compose area + const composeArea = composeBodyRef.current.querySelector('.compose-area'); + if (composeArea) { + const range = document.createRange(); + const sel = window.getSelection(); + range.setStart(composeArea, 0); + range.collapse(true); + sel?.removeAllRanges(); + sel?.addRange(range); + (composeArea as HTMLElement).focus(); + } } } }, [composeBody, replyTo, forwardFrom, isInitialized]);