diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx
index f23f32bf..9652ec0c 100644
--- a/app/courrier/page.tsx
+++ b/app/courrier/page.tsx
@@ -55,6 +55,15 @@ import { decodeEmail, cleanHtml } from '@/lib/mail-parser-wrapper';
import { Attachment as MailParserAttachment } from 'mailparser';
import { LoadingFix } from './loading-fix';
+// Import centralized email formatters
+import {
+ formatForwardedEmail,
+ formatReplyEmail,
+ formatEmailForReplyOrForward,
+ EmailMessage as FormatterEmailMessage,
+ cleanHtmlContent
+} from '@/lib/utils/email-formatter';
+
export interface Account {
id: number;
name: string;
@@ -369,34 +378,36 @@ function ReplyContent({ email, type }: { email: Email; type: 'reply' | 'reply-al
return;
}
- const decoded = await decodeEmail(email.content);
+ // Create a formatter-compatible email object
+ const emailForFormatter: FormatterEmailMessage = {
+ id: email.id,
+ subject: email.subject || '',
+ from: [{
+ name: email.fromName || email.from.split('@')[0] || '',
+ address: email.from
+ }],
+ to: [{
+ name: '',
+ address: email.to
+ }],
+ date: new Date(email.date),
+ content: email.content,
+ html: email.content,
+ text: ''
+ };
- if (mounted) {
- let formattedContent = '';
-
- if (type === 'forward') {
- formattedContent = `
-
-
---------- Forwarded message ---------
-
From: ${decoded.from || ''}
-
Date: ${formatDate(decoded.date ? new Date(decoded.date) : null)}
-
Subject: ${decoded.subject || ''}
-
To: ${decoded.to || ''}
-
- ${decoded.html || `
${decoded.text || ''}`}
-
- `;
- } else {
- formattedContent = `
-
-
On ${formatDate(decoded.date ? new Date(decoded.date) : null)}, ${decoded.from || ''} wrote:
-
- ${decoded.html || `${decoded.text || ''}`}
-
-
- `;
- }
+ // Use centralized formatters
+ let formattedContent = '';
+
+ if (type === 'forward') {
+ const formatted = formatForwardedEmail(emailForFormatter);
+ formattedContent = formatted.content;
+ } else {
+ const formatted = formatReplyEmail(emailForFormatter, type as 'reply' | 'reply-all');
+ formattedContent = formatted.content;
+ }
+ if (mounted) {
setContent(formattedContent);
setError(null);
}
@@ -414,7 +425,7 @@ function ReplyContent({ email, type }: { email: Email; type: 'reply' | 'reply-al
return () => {
mounted = false;
};
- }, [email.content, type]);
+ }, [email.content, type, email.id, email.subject, email.from, email.fromName, email.to, email.date]);
if (error) {
return {error}
;
@@ -1617,12 +1628,12 @@ export default function CourrierPage() {
// New helper function to directly format email content
const formatEmailAndShowCompose = (email: Email, type: 'reply' | 'reply-all' | 'forward') => {
// Create an EmailMessage compatible object for the ComposeEmail component
- const emailForCompose = {
+ const emailForCompose: FormatterEmailMessage = {
id: email.id,
messageId: '',
subject: email.subject,
from: [{
- name: email.fromName || email.from,
+ name: email.fromName || email.from.split('@')[0] || '',
address: email.from
}],
to: [{
@@ -1631,48 +1642,26 @@ export default function CourrierPage() {
}],
date: new Date(email.date),
content: email.content,
- // Add html and text properties if needed by the ComposeEmail component
- html: email.content, // Use content as html
+ html: email.content,
text: '',
hasAttachments: email.attachments ? email.attachments.length > 0 : false,
folder: email.folder
};
- // Format appropriate content with headers for reply/forward
+ // Use centralized formatters to ensure consistent formatting
let formattedContent = '';
- const formattedDate = formatDate(new Date(email.date));
+ let formattedSubject = '';
+ let formattedTo = '';
if (type === 'reply' || type === 'reply-all') {
- // Create reply quote header
- formattedContent = `
-
-
-
-
-
- ${email.content || '
No content available
'}
-
-
-
- `;
+ const formatted = formatReplyEmail(emailForCompose, type);
+ formattedContent = formatted.content;
+ formattedSubject = formatted.subject;
+ formattedTo = formatted.to;
} else if (type === 'forward') {
- // Create forward header
- formattedContent = `
-
-
-
---------- Forwarded message ---------
-
From: ${email.fromName ? `${email.fromName} <${email.from}>` : email.from}
-
Date: ${formattedDate}
-
Subject: ${email.subject || ''}
-
To: ${email.to || ''}
-
-
- ${email.content || '
No content available
'}
-
-
- `;
+ const formatted = formatForwardedEmail(emailForCompose);
+ formattedContent = formatted.content;
+ formattedSubject = formatted.subject;
}
// Set state for compose form
@@ -1689,12 +1678,12 @@ export default function CourrierPage() {
setShowCompose(true);
if (type === 'reply' || type === 'reply-all') {
- setComposeTo(type === 'reply' ? email.from : `${email.from}; ${email.to}`);
- setComposeSubject(email.subject.startsWith('Re:') ? email.subject : `Re: ${email.subject}`);
+ setComposeTo(formattedTo);
+ setComposeSubject(formattedSubject);
setComposeBody(formattedContent);
} else if (type === 'forward') {
setComposeTo('');
- setComposeSubject(email.subject.startsWith('Fwd:') ? email.subject : `Fwd: ${email.subject}`);
+ setComposeSubject(formattedSubject);
setComposeBody(formattedContent);
}
};