104 lines
3.1 KiB
TypeScript
104 lines
3.1 KiB
TypeScript
'use client';
|
|
|
|
import { useState, useEffect } from 'react';
|
|
import ComposeEmail from './ComposeEmail';
|
|
import { EmailMessage as NewEmailMessage } from '@/types/email';
|
|
import {
|
|
EmailMessage as OldEmailMessage,
|
|
formatReplyEmail as oldFormatReplyEmail,
|
|
formatForwardedEmail as oldFormatForwardedEmail
|
|
} from '@/lib/utils/email-formatter';
|
|
|
|
interface ComposeEmailAdapterProps {
|
|
initialEmail?: NewEmailMessage | null;
|
|
type?: 'new' | 'reply' | 'reply-all' | 'forward';
|
|
onClose: () => void;
|
|
onSend: (emailData: {
|
|
to: string;
|
|
cc?: string;
|
|
bcc?: string;
|
|
subject: string;
|
|
body: string;
|
|
attachments?: Array<{
|
|
name: string;
|
|
content: string;
|
|
type: string;
|
|
}>;
|
|
}) => Promise<void>;
|
|
}
|
|
|
|
/**
|
|
* Adapter component that converts between the new EmailMessage format
|
|
* and the format expected by the legacy ComposeEmail component
|
|
*/
|
|
export default function ComposeEmailAdapter({
|
|
initialEmail,
|
|
type = 'new',
|
|
onClose,
|
|
onSend
|
|
}: ComposeEmailAdapterProps) {
|
|
// Convert the new EmailMessage format to the old format
|
|
const [adaptedEmail, setAdaptedEmail] = useState<OldEmailMessage | null>(null);
|
|
|
|
useEffect(() => {
|
|
if (!initialEmail) {
|
|
setAdaptedEmail(null);
|
|
return;
|
|
}
|
|
|
|
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: Converted new format to old format', oldFormat);
|
|
setAdaptedEmail(oldFormat);
|
|
} catch (error) {
|
|
console.error('Error adapting email for ComposeEmail:', error);
|
|
setAdaptedEmail(null);
|
|
}
|
|
}, [initialEmail]);
|
|
|
|
// If still adapting, show loading
|
|
if (initialEmail && !adaptedEmail) {
|
|
return <div>Loading email...</div>;
|
|
}
|
|
|
|
// Pass the adapted email to the original ComposeEmail component
|
|
return (
|
|
<ComposeEmail
|
|
initialEmail={adaptedEmail}
|
|
type={type}
|
|
onClose={onClose}
|
|
onSend={onSend}
|
|
/>
|
|
);
|
|
}
|