From 41480e6666b2f2989463e2e5f4caaa45f924f822 Mon Sep 17 00:00:00 2001 From: alma Date: Wed, 30 Apr 2025 22:00:20 +0200 Subject: [PATCH] courrier preview --- components/email/ComposeEmail.tsx | 57 ++++++++++-------------- components/email/EmailContentDisplay.tsx | 49 +++++++++++++++----- components/email/EmailPreview.tsx | 2 +- 3 files changed, 63 insertions(+), 45 deletions(-) diff --git a/components/email/ComposeEmail.tsx b/components/email/ComposeEmail.tsx index 68fc718c..eb0d7f8d 100644 --- a/components/email/ComposeEmail.tsx +++ b/components/email/ComposeEmail.tsx @@ -17,14 +17,13 @@ import ComposeEmailFooter from './ComposeEmailFooter'; import RichEmailEditor from './RichEmailEditor'; import QuotedEmailContent from './QuotedEmailContent'; -// Import ONLY from the centralized formatter +// Import from the centralized utils import { formatReplyEmail, formatForwardedEmail, - formatEmailAddresses, - type EmailMessage, - type EmailAddress -} from '@/lib/utils/email-formatter'; + formatEmailAddresses +} from '@/lib/utils/email-utils'; +import { EmailMessage, EmailAddress } from '@/types/email'; /** * CENTRAL EMAIL COMPOSER COMPONENT @@ -88,41 +87,31 @@ export default function ComposeEmail(props: ComposeEmailProps) { try { // Set recipients based on type if (type === 'reply' || type === 'reply-all') { - // Reply goes to the original sender - setTo(formatEmailAddresses(initialEmail.from || [])); + // Get formatted data for reply + const formatted = formatReplyEmail(initialEmail, type); - // For reply-all, include all original recipients in CC - if (type === 'reply-all') { - const allRecipients = [ - ...(initialEmail.to || []), - ...(initialEmail.cc || []) - ]; - setCc(formatEmailAddresses(allRecipients)); - } - - // Set subject with Re: prefix - const subjectBase = initialEmail.subject || '(No subject)'; - const subject = subjectBase.match(/^Re:/i) ? subjectBase : `Re: ${subjectBase}`; - setSubject(subject); - - // Format the reply content - const { content } = formatReplyEmail(initialEmail, type); - setEmailContent(content); - - // Show CC field if there are CC recipients - if (initialEmail.cc && initialEmail.cc.length > 0) { + // Set the recipients + setTo(formatted.to); + if (formatted.cc) { + setCc(formatted.cc); setShowCc(true); } + + // Set subject + setSubject(formatted.subject); + + // Set the email content (use HTML if available) + setEmailContent(formatted.content.html || formatted.content.text); } else if (type === 'forward') { - // Set subject with Fwd: prefix - const subjectBase = initialEmail.subject || '(No subject)'; - const subject = subjectBase.match(/^(Fwd|FW|Forward):/i) ? subjectBase : `Fwd: ${subjectBase}`; - setSubject(subject); + // Get formatted data for forward + const formatted = formatForwardedEmail(initialEmail); - // Format the forward content - const { content } = formatForwardedEmail(initialEmail); - setEmailContent(content); + // Set subject + setSubject(formatted.subject); + + // Set the email content (use HTML if available) + setEmailContent(formatted.content.html || formatted.content.text); // If the original email has attachments, we should include them if (initialEmail.attachments && initialEmail.attachments.length > 0) { diff --git a/components/email/EmailContentDisplay.tsx b/components/email/EmailContentDisplay.tsx index 2ac38800..a856f186 100644 --- a/components/email/EmailContentDisplay.tsx +++ b/components/email/EmailContentDisplay.tsx @@ -1,7 +1,7 @@ 'use client'; import React, { useMemo, CSSProperties } from 'react'; -import { renderEmailContent, normalizeEmailContent } from '@/lib/utils/email-utils'; +import { renderEmailContent } from '@/lib/utils/email-utils'; import { EmailContent } from '@/types/email'; interface EmailContentDisplayProps { @@ -44,13 +44,42 @@ const EmailContentDisplay: React.FC = ({ return content as EmailContent; } - // Special case for simple string content - if (typeof content === 'string') { - return normalizeEmailContent({ content }); + // Special case for email message with content property + if (content && typeof content === 'object' && content.content && + typeof content.content === 'object' && + 'text' in content.content && + 'isHtml' in content.content) { + return content.content as EmailContent; } - // Otherwise normalize it - return normalizeEmailContent(content); + // Special case for simple string content + if (typeof content === 'string') { + return { + text: content, + isHtml: content.trim().startsWith('<'), + direction: 'ltr' + } as EmailContent; + } + + // For HTML/text properties + if (content && typeof content === 'object') { + if (content.html || content.text) { + return { + html: content.html, + text: content.text || '', + isHtml: !!content.html, + direction: 'ltr' + } as EmailContent; + } + } + + // Fallback + console.warn('EmailContentDisplay: Unable to properly normalize content format'); + return { + text: 'Content format not supported', + isHtml: false, + direction: 'ltr' + } as EmailContent; } catch (error) { console.error('Error normalizing content in EmailContentDisplay:', error); return { @@ -114,10 +143,10 @@ const EmailContentDisplay: React.FC = ({ {typeof content === 'object' && (

Keys: {Object.keys(content).join(', ')}

)} -

Normalized: {normalizedContent.isHtml ? 'HTML' : 'Text'}

-

Direction: {normalizedContent.direction}

-

Has HTML: {!!normalizedContent.html}

-

Text Length: {normalizedContent.text?.length || 0}

+

Normalized: {normalizedContent?.isHtml ? 'HTML' : 'Text'}

+

Direction: {normalizedContent?.direction}

+

Has HTML: {!!normalizedContent?.html}

+

Text Length: {normalizedContent?.text?.length || 0}

)} diff --git a/components/email/EmailPreview.tsx b/components/email/EmailPreview.tsx index 20d6b778..920dc009 100644 --- a/components/email/EmailPreview.tsx +++ b/components/email/EmailPreview.tsx @@ -191,7 +191,7 @@ export default function EmailPreview({ email, loading = false, onReply }: EmailP {/* Attachments list */} - {hasAttachments && ( + {hasAttachments && standardizedEmail.attachments && (

Attachments ({standardizedEmail.attachments.length})