courrier preview
This commit is contained in:
parent
c501784bbe
commit
2113c27a68
@ -2,15 +2,14 @@
|
|||||||
|
|
||||||
import { useState, useEffect } from 'react';
|
import { useState, useEffect } from 'react';
|
||||||
import ComposeEmail from './ComposeEmail';
|
import ComposeEmail from './ComposeEmail';
|
||||||
import { EmailMessage as NewEmailMessage } from '@/types/email';
|
import { EmailMessage } from '@/types/email';
|
||||||
import {
|
import {
|
||||||
EmailMessage as OldEmailMessage,
|
formatReplyEmail,
|
||||||
formatReplyEmail as oldFormatReplyEmail,
|
formatForwardedEmail
|
||||||
formatForwardedEmail as oldFormatForwardedEmail
|
} from '@/lib/utils/email-utils';
|
||||||
} from '@/lib/utils/email-formatter';
|
|
||||||
|
|
||||||
interface ComposeEmailAdapterProps {
|
interface ComposeEmailAdapterProps {
|
||||||
initialEmail?: NewEmailMessage | null;
|
initialEmail?: EmailMessage | null;
|
||||||
type?: 'new' | 'reply' | 'reply-all' | 'forward';
|
type?: 'new' | 'reply' | 'reply-all' | 'forward';
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
onSend: (emailData: {
|
onSend: (emailData: {
|
||||||
@ -38,7 +37,7 @@ export default function ComposeEmailAdapter({
|
|||||||
onSend
|
onSend
|
||||||
}: ComposeEmailAdapterProps) {
|
}: ComposeEmailAdapterProps) {
|
||||||
// Convert the new EmailMessage format to the old format
|
// Convert the new EmailMessage format to the old format
|
||||||
const [adaptedEmail, setAdaptedEmail] = useState<OldEmailMessage | null>(null);
|
const [adaptedEmail, setAdaptedEmail] = useState<any | null>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!initialEmail) {
|
if (!initialEmail) {
|
||||||
@ -47,45 +46,77 @@ export default function ComposeEmailAdapter({
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Convert the new EmailMessage to the old format
|
console.log('ComposeEmailAdapter: Processing email for', type, initialEmail);
|
||||||
const oldFormat: OldEmailMessage = {
|
|
||||||
|
// 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,
|
id: initialEmail.id,
|
||||||
messageId: initialEmail.messageId,
|
subject: formatted.subject,
|
||||||
subject: initialEmail.subject,
|
// Format to/cc as expected by ComposeEmail
|
||||||
from: initialEmail.from,
|
to: formatted.to || initialEmail.from, // Will set recipient to original sender
|
||||||
to: initialEmail.to,
|
cc: formatted.cc,
|
||||||
cc: initialEmail.cc,
|
// Include the original content for quoting
|
||||||
bcc: initialEmail.bcc,
|
content: formatted.content?.html || formatted.content?.text || '',
|
||||||
date: initialEmail.date,
|
html: formatted.content?.html,
|
||||||
// Convert new flags object to old format string array
|
text: formatted.content?.text,
|
||||||
flags: initialEmail.flags ? {
|
// Copy over attachments if needed
|
||||||
seen: initialEmail.flags.seen || false,
|
attachments: initialEmail.attachments || []
|
||||||
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: Converted new format to old format', oldFormat);
|
setAdaptedEmail(adapted);
|
||||||
setAdaptedEmail(oldFormat);
|
}
|
||||||
|
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) {
|
} catch (error) {
|
||||||
console.error('Error adapting email for ComposeEmail:', 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 still adapting, show loading
|
||||||
if (initialEmail && !adaptedEmail) {
|
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));
|
console.log('useEmailFetch: Transformed email:', JSON.stringify(transformedEmail, null, 2));
|
||||||
|
|
||||||
setState({ email: transformedEmail, loading: false, error: null });
|
setState({ email: transformedEmail, loading: false, error: null });
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user