diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 9652ec0c..230a8a7c 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -49,9 +49,7 @@ import { CommandShortcut, } from '@/components/ui/command'; import { useSession } from 'next-auth/react'; -import DOMPurify from 'isomorphic-dompurify'; import ComposeEmail from '@/components/email/ComposeEmail'; -import { decodeEmail, cleanHtml } from '@/lib/mail-parser-wrapper'; import { Attachment as MailParserAttachment } from 'mailparser'; import { LoadingFix } from './loading-fix'; @@ -169,15 +167,15 @@ function EmailContent({ email }: { email: Email }) { // Update the email content with the fetched full content email.content = fullContent.content; - // Render the content - const sanitizedHtml = DOMPurify.sanitize(fullContent.content); + // Render the content using the centralized cleaner + const sanitizedHtml = cleanHtmlContent(fullContent.content); setContent(
); - setDebugInfo('Rendered fetched HTML content'); + setDebugInfo('Rendered fetched HTML content with centralized formatter'); setError(null); setIsLoading(false); } @@ -200,39 +198,36 @@ function EmailContent({ email }: { email: Email }) { // Check if content is already HTML if (formattedEmail.startsWith('<') && formattedEmail.endsWith('>')) { - // Content is likely HTML, sanitize and display directly - const sanitizedHtml = DOMPurify.sanitize(formattedEmail); + // Content is likely HTML, sanitize using the centralized cleaner + const sanitizedHtml = cleanHtmlContent(formattedEmail); setContent(
); - setDebugInfo('Rendered existing HTML content'); + setDebugInfo('Rendered existing HTML content with centralized formatter'); } else { - // Use mailparser for more complex formats - console.log('Parsing email content'); - const parsedEmail = await decodeEmail(formattedEmail); + // For plain text or complex formats, use the centralized formatter + const cleanedContent = cleanHtmlContent(formattedEmail); - if (parsedEmail.html) { - const sanitizedHtml = DOMPurify.sanitize(parsedEmail.html); + // If it looks like HTML, render it as HTML + if (cleanedContent.includes('<') && cleanedContent.includes('>')) { setContent(
); - setDebugInfo('Rendered HTML content from parser'); - } else if (parsedEmail.text) { + setDebugInfo('Rendered content as HTML using centralized formatter'); + } else { + // Otherwise, render as plain text setContent(
- {parsedEmail.text} + {cleanedContent}
); - setDebugInfo('Rendered text content from parser'); - } else { - setContent(
No displayable content available
); - setDebugInfo('No HTML or text content in parsed email'); + setDebugInfo('Rendered content as text using centralized formatter'); } } @@ -458,19 +453,20 @@ function EmailPreview({ email }: { email: Email }) { try { // If we have the content already, extract preview from it if (email.content) { - const plainText = email.content.replace(/<[^>]*>/g, ' ').trim(); + // Use cleanHtmlContent to safely extract text from HTML + const cleanContent = cleanHtmlContent(email.content); + const plainText = cleanContent.replace(/<[^>]*>/g, ' ').trim(); if (mounted) { setPreview(plainText.substring(0, 150) + '...'); } } else { - // Fallback to using parser for older emails - const decoded = await decodeEmail(email.content || ''); + // Use the centralized cleaner instead of decodeEmail + const cleanContent = cleanHtmlContent(email.content || ''); + const plainText = cleanContent.replace(/<[^>]*>/g, ' ').trim(); + if (mounted) { - if (decoded.text) { - setPreview(decoded.text.substring(0, 150) + '...'); - } else if (decoded.html) { - const cleanText = decoded.html.replace(/<[^>]*>/g, ' ').trim(); - setPreview(cleanText.substring(0, 150) + '...'); + if (plainText) { + setPreview(plainText.substring(0, 150) + '...'); } else { setPreview('No preview available'); } @@ -518,6 +514,20 @@ function generateEmailPreview(email: Email) { return ; } +// Add this interface before the CourrierPage component +interface EmailData { + to: string; + cc?: string; + bcc?: string; + subject: string; + body: string; + attachments?: Array<{ + name: string; + content: string; + type: string; + }>; +} + export default function CourrierPage() { const router = useRouter(); const { data: session } = useSession(); @@ -2363,7 +2373,7 @@ export default function CourrierPage() { attachments={attachments} setAttachments={setAttachments} handleSend={handleSend} - onSend={async (emailData) => { + onSend={async (emailData: EmailData) => { console.log('Email sent:', emailData); setShowCompose(false); setIsReplying(false);