courrier preview
This commit is contained in:
parent
c501784bbe
commit
2113c27a68
@ -2,15 +2,14 @@
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
import ComposeEmail from './ComposeEmail';
|
||||
import { EmailMessage as NewEmailMessage } from '@/types/email';
|
||||
import { EmailMessage } from '@/types/email';
|
||||
import {
|
||||
EmailMessage as OldEmailMessage,
|
||||
formatReplyEmail as oldFormatReplyEmail,
|
||||
formatForwardedEmail as oldFormatForwardedEmail
|
||||
} from '@/lib/utils/email-formatter';
|
||||
formatReplyEmail,
|
||||
formatForwardedEmail
|
||||
} from '@/lib/utils/email-utils';
|
||||
|
||||
interface ComposeEmailAdapterProps {
|
||||
initialEmail?: NewEmailMessage | null;
|
||||
initialEmail?: EmailMessage | null;
|
||||
type?: 'new' | 'reply' | 'reply-all' | 'forward';
|
||||
onClose: () => void;
|
||||
onSend: (emailData: {
|
||||
@ -38,7 +37,7 @@ export default function ComposeEmailAdapter({
|
||||
onSend
|
||||
}: ComposeEmailAdapterProps) {
|
||||
// Convert the new EmailMessage format to the old format
|
||||
const [adaptedEmail, setAdaptedEmail] = useState<OldEmailMessage | null>(null);
|
||||
const [adaptedEmail, setAdaptedEmail] = useState<any | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!initialEmail) {
|
||||
@ -47,45 +46,77 @@ export default function ComposeEmailAdapter({
|
||||
}
|
||||
|
||||
try {
|
||||
// Convert the new EmailMessage to the old format
|
||||
const oldFormat: OldEmailMessage = {
|
||||
id: initialEmail.id,
|
||||
messageId: initialEmail.messageId,
|
||||
subject: initialEmail.subject,
|
||||
from: initialEmail.from,
|
||||
to: initialEmail.to,
|
||||
cc: initialEmail.cc,
|
||||
bcc: initialEmail.bcc,
|
||||
date: initialEmail.date,
|
||||
// Convert new flags object to old format string array
|
||||
flags: initialEmail.flags ? {
|
||||
seen: initialEmail.flags.seen || false,
|
||||
flagged: initialEmail.flags.flagged || false,
|
||||
answered: initialEmail.flags.answered || false,
|
||||
deleted: initialEmail.flags.deleted || false,
|
||||
draft: initialEmail.flags.draft || false
|
||||
} : undefined,
|
||||
// Convert new content format to old format
|
||||
content: initialEmail.content.isHtml && initialEmail.content.html
|
||||
? initialEmail.content.html
|
||||
: initialEmail.content.text,
|
||||
html: initialEmail.content.isHtml ? initialEmail.content.html : undefined,
|
||||
text: initialEmail.content.text,
|
||||
attachments: initialEmail.attachments.map(att => ({
|
||||
filename: att.filename,
|
||||
contentType: att.contentType,
|
||||
content: att.content,
|
||||
size: att.size || 0
|
||||
}))
|
||||
};
|
||||
console.log('ComposeEmailAdapter: Processing email for', type, initialEmail);
|
||||
|
||||
console.log('ComposeEmailAdapter: Converted new format to old format', oldFormat);
|
||||
setAdaptedEmail(oldFormat);
|
||||
// For reply or forward, use the email formatting utilities
|
||||
if (type === 'reply' || type === 'reply-all') {
|
||||
const formatted = formatReplyEmail(initialEmail, type);
|
||||
console.log('ComposeEmailAdapter: Formatted reply', formatted);
|
||||
|
||||
// Create an object suitable for the legacy ComposeEmail component
|
||||
const adapted = {
|
||||
id: initialEmail.id,
|
||||
subject: formatted.subject,
|
||||
// Format to/cc as expected by ComposeEmail
|
||||
to: formatted.to || initialEmail.from, // Will set recipient to original sender
|
||||
cc: formatted.cc,
|
||||
// Include the original content for quoting
|
||||
content: formatted.content?.html || formatted.content?.text || '',
|
||||
html: formatted.content?.html,
|
||||
text: formatted.content?.text,
|
||||
// Copy over attachments if needed
|
||||
attachments: initialEmail.attachments || []
|
||||
};
|
||||
|
||||
setAdaptedEmail(adapted);
|
||||
}
|
||||
else if (type === 'forward') {
|
||||
const formatted = formatForwardedEmail(initialEmail);
|
||||
console.log('ComposeEmailAdapter: Formatted forward', formatted);
|
||||
|
||||
// Create an object suitable for the legacy ComposeEmail component
|
||||
const adapted = {
|
||||
id: initialEmail.id,
|
||||
subject: formatted.subject,
|
||||
// Forward doesn't pre-fill recipients
|
||||
to: '',
|
||||
cc: '',
|
||||
// Include the forwarded content
|
||||
content: formatted.content?.html || formatted.content?.text || '',
|
||||
html: formatted.content?.html,
|
||||
text: formatted.content?.text,
|
||||
// Copy over attachments
|
||||
attachments: initialEmail.attachments || []
|
||||
};
|
||||
|
||||
setAdaptedEmail(adapted);
|
||||
}
|
||||
else {
|
||||
// For new emails, just create a minimal template
|
||||
setAdaptedEmail({
|
||||
subject: '',
|
||||
to: '',
|
||||
cc: '',
|
||||
content: '',
|
||||
html: '',
|
||||
text: '',
|
||||
attachments: []
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error adapting email for ComposeEmail:', error);
|
||||
setAdaptedEmail(null);
|
||||
// Provide a fallback minimal email
|
||||
setAdaptedEmail({
|
||||
subject: initialEmail?.subject || '',
|
||||
to: initialEmail?.from || '',
|
||||
cc: '',
|
||||
content: '',
|
||||
html: '',
|
||||
text: '',
|
||||
attachments: []
|
||||
});
|
||||
}
|
||||
}, [initialEmail]);
|
||||
}, [initialEmail, type]);
|
||||
|
||||
// If still adapting, show loading
|
||||
if (initialEmail && !adaptedEmail) {
|
||||
|
||||
@ -89,6 +89,34 @@ export function useEmailFetch({ onEmailLoaded, onError }: UseEmailFetchProps = {
|
||||
}
|
||||
};
|
||||
|
||||
// If data contains content as a string or object without isHtml property,
|
||||
// normalize it to conform to our EmailContent interface
|
||||
if (typeof transformedEmail.content === 'string') {
|
||||
// If content is a string, normalize it to the proper structure
|
||||
const contentStr = transformedEmail.content;
|
||||
transformedEmail.content = {
|
||||
text: contentStr,
|
||||
html: contentStr.startsWith('<') ? contentStr : undefined,
|
||||
isHtml: contentStr.startsWith('<'),
|
||||
direction: 'ltr'
|
||||
};
|
||||
console.log('useEmailFetch: Normalized string content to object');
|
||||
} else if (transformedEmail.content && typeof transformedEmail.content === 'object') {
|
||||
// Ensure the content object has all required fields
|
||||
if (!('isHtml' in transformedEmail.content)) {
|
||||
transformedEmail.content.isHtml = !!transformedEmail.content.html;
|
||||
console.log('useEmailFetch: Added missing isHtml field to content');
|
||||
}
|
||||
if (!('direction' in transformedEmail.content)) {
|
||||
transformedEmail.content.direction = 'ltr';
|
||||
console.log('useEmailFetch: Added missing direction field to content');
|
||||
}
|
||||
if (!('text' in transformedEmail.content) && !transformedEmail.content.text) {
|
||||
transformedEmail.content.text = '';
|
||||
console.log('useEmailFetch: Added missing text field to content');
|
||||
}
|
||||
}
|
||||
|
||||
console.log('useEmailFetch: Transformed email:', JSON.stringify(transformedEmail, null, 2));
|
||||
|
||||
setState({ email: transformedEmail, loading: false, error: null });
|
||||
|
||||
Loading…
Reference in New Issue
Block a user