From e99d08fccfa79530ae62cb441c910e761144cf40 Mon Sep 17 00:00:00 2001 From: alma Date: Mon, 28 Apr 2025 10:52:53 +0200 Subject: [PATCH] courrier multi account restore compose --- components/email/ComposeEmail.tsx | 683 +++++------------------------- 1 file changed, 112 insertions(+), 571 deletions(-) diff --git a/components/email/ComposeEmail.tsx b/components/email/ComposeEmail.tsx index 5af7cc38..542cf5bd 100644 --- a/components/email/ComposeEmail.tsx +++ b/components/email/ComposeEmail.tsx @@ -39,37 +39,6 @@ import { * for consistent handling of email content and text direction. */ -// Define interface for the legacy props -interface LegacyComposeEmailProps { - showCompose: boolean; - setShowCompose: (show: boolean) => void; - composeTo: string; - setComposeTo: (to: string) => void; - composeCc: string; - setComposeCc: (cc: string) => void; - composeBcc: string; - setComposeBcc: (bcc: string) => void; - composeSubject: string; - setComposeSubject: (subject: string) => void; - composeBody: string; - setComposeBody: (body: string) => void; - showCc: boolean; - setShowCc: (show: boolean) => void; - showBcc: boolean; - setShowBcc: (show: boolean) => void; - attachments: any[]; - setAttachments: (attachments: any[]) => void; - handleSend: () => Promise; - originalEmail?: { - content: string; - type: 'reply' | 'reply-all' | 'forward'; - }; - onSend: (email: any) => Promise; - onCancel: () => void; - replyTo?: any | null; - forwardFrom?: any | null; -} - // Define interface for the modern props interface ComposeEmailProps { initialEmail?: EmailMessage | null; @@ -89,55 +58,7 @@ interface ComposeEmailProps { }) => Promise; } -// Union type for handling both types of props -type ComposeEmailAllProps = ComposeEmailProps | LegacyComposeEmailProps; - -// Type guard to check if props are legacy -function isLegacyProps( - props: ComposeEmailAllProps -): props is LegacyComposeEmailProps { - return 'showCompose' in props; -} - -// Helper function to adapt EmailMessage to QuotedEmailContent props format -function EmailMessageToQuotedContentAdapter({ - email, - type -}: { - email: EmailMessage, - type: 'reply' | 'reply-all' | 'forward' -}) { - // Get the email content - const content = email.content || email.html || email.text || ''; - - // Get the sender - const sender = email.from && email.from.length > 0 - ? { - name: email.from[0].name, - email: email.from[0].address - } - : { email: 'unknown@example.com' }; - - // Map the type to what QuotedEmailContent expects - const mappedType = type === 'reply-all' ? 'reply' : type; - - return ( - - ); -} - -export default function ComposeEmail(props: ComposeEmailAllProps) { - // Handle legacy props by adapting them to new component - if (isLegacyProps(props)) { - return ; - } - - // Continue with modern implementation for new props +export default function ComposeEmail(props: ComposeEmailProps) { const { initialEmail, type = 'new', onClose, onSend } = props; // Email form state @@ -359,132 +280,118 @@ export default function ComposeEmail(props: ComposeEmailAllProps) { }, []); return ( -
-
- {/* Modal Header */} -
-

- {type === 'reply' ? 'Reply' : type === 'forward' ? 'Forward' : type === 'reply-all' ? 'Reply All' : 'New Message'} -

- -
- - {/* Modal Body */} -
-
- {/* To Field */} -
- - setTo(e.target.value)} - placeholder="recipient@example.com" - className="w-full mt-1 bg-white border-gray-300 text-gray-900" - /> -
- - {/* CC/BCC Toggle Buttons */} -
- - -
- - {/* CC Field */} - {showCc && ( -
- - setCc(e.target.value)} - placeholder="cc@example.com" - className="w-full mt-1 bg-white border-gray-300 text-gray-900" - /> -
- )} - - {/* BCC Field */} - {showBcc && ( -
- - setBcc(e.target.value)} - placeholder="bcc@example.com" - className="w-full mt-1 bg-white border-gray-300 text-gray-900" - /> -
- )} - - {/* Subject Field */} -
- - setSubject(e.target.value)} - placeholder="Enter subject" - className="w-full mt-1 bg-white border-gray-300 text-gray-900" - /> -
- - {/* Message Body */} -
- -
- -
-
- - {/* Attachments */} - {attachments.length > 0 && ( -
-

Attachments

-
- {attachments.map((file, index) => ( -
- {file.name} - -
- ))} -
-
- )} +
+ +
+
+ {/* To Field */} +
+ + setTo(e.target.value)} + placeholder="recipient@example.com" + className="w-full mt-1 bg-white border-gray-300 text-gray-900" + />
+ + {/* CC/BCC Toggle Buttons */} +
+ + +
+ + {/* CC Field */} + {showCc && ( +
+ + setCc(e.target.value)} + placeholder="cc@example.com" + className="w-full mt-1 bg-white border-gray-300 text-gray-900" + /> +
+ )} + + {/* BCC Field */} + {showBcc && ( +
+ + setBcc(e.target.value)} + placeholder="bcc@example.com" + className="w-full mt-1 bg-white border-gray-300 text-gray-900" + /> +
+ )} + + {/* Subject Field */} +
+ + setSubject(e.target.value)} + placeholder="Enter subject" + className="w-full mt-1 bg-white border-gray-300 text-gray-900" + /> +
+ + {/* Message Body */} +
+ +
+ +
+
+ + {/* Attachments */} + {attachments.length > 0 && ( +
+

Attachments

+
+ {attachments.map((file, index) => ( +
+ {file.name} + +
+ ))} +
+
+ )}
{/* Modal Footer */} @@ -548,370 +455,4 @@ export default function ComposeEmail(props: ComposeEmailAllProps) {
); -} - -// Legacy adapter to maintain backward compatibility -function LegacyAdapter({ - showCompose, - setShowCompose, - composeTo, - setComposeTo, - composeCc, - setComposeCc, - composeBcc, - setComposeBcc, - composeSubject, - setComposeSubject, - composeBody, - setComposeBody, - showCc, - setShowCc, - showBcc, - setShowBcc, - attachments, - setAttachments, - handleSend, - originalEmail, - onSend, - onCancel, - replyTo, - forwardFrom -}: LegacyComposeEmailProps) { - const [sending, setSending] = useState(false); - - // Determine the type based on legacy props - const determineType = (): 'new' | 'reply' | 'reply-all' | 'forward' => { - if (originalEmail?.type === 'forward') return 'forward'; - if (originalEmail?.type === 'reply-all') return 'reply-all'; - if (originalEmail?.type === 'reply') return 'reply'; - if (replyTo) return 'reply'; - if (forwardFrom) return 'forward'; - return 'new'; - }; - - // Format legacy content on mount if necessary - useEffect(() => { - // Only format if we have original email and no content was set yet - if ((originalEmail || replyTo || forwardFrom) && - (!composeBody || composeBody === '

' || composeBody === '
')) { - - const type = determineType(); - - if (type === 'reply' || type === 'reply-all') { - // For reply, format with sender info and original content - const content = originalEmail?.content || ''; - const sender = replyTo?.name || replyTo?.email || 'Unknown sender'; - const date = new Date().toLocaleString('en-US', { - weekday: 'short', - year: 'numeric', - month: 'short', - day: 'numeric', - hour: '2-digit', - minute: '2-digit' - }); - - const replyContent = ` -

-

-

-

-
On ${date}, ${sender} wrote:
-
-
- ${content} -
-
- `; - - setComposeBody(replyContent); - } - else if (type === 'forward') { - // For forward, format with original message details - const content = originalEmail?.content || ''; - const fromString = forwardFrom?.name || forwardFrom?.email || 'Unknown'; - const toString = 'Recipients'; - const date = new Date().toLocaleString('en-US', { - weekday: 'short', - year: 'numeric', - month: 'short', - day: 'numeric', - hour: '2-digit', - minute: '2-digit' - }); - - const forwardContent = ` -

-

-

-

-
-
-
-
---------- Forwarded message ---------
-
From: ${fromString}
-
Date: ${date}
-
Subject: ${composeSubject || ''}
-
To: ${toString}
-
- -
-
- `; - - setComposeBody(forwardContent); - } - } - }, [originalEmail, replyTo, forwardFrom, composeBody, determineType, composeSubject]); - - // Converts attachments to the expected format - const convertAttachments = () => { - return attachments.map(att => ({ - name: att.name || att.filename || 'attachment', - content: att.content || '', - type: att.type || att.contentType || 'application/octet-stream' - })); - }; - - // Handle sending in the legacy format - const handleLegacySend = async () => { - setSending(true); - - try { - if (onSend) { - // New API - await onSend({ - to: composeTo, - cc: composeCc, - bcc: composeBcc, - subject: composeSubject, - body: composeBody, - attachments: convertAttachments() - }); - } else if (handleSend) { - // Old API - await handleSend(); - } - - // Close compose window - setShowCompose(false); - } catch (error) { - console.error('Error sending email:', error); - alert('Failed to send email. Please try again.'); - } finally { - setSending(false); - } - }; - - // Handle file selection for legacy interface - const handleFileSelection = (files: FileList) => { - const newAttachments = Array.from(files).map(file => ({ - name: file.name, - type: file.type, - content: URL.createObjectURL(file), - size: file.size - })); - - setAttachments([...attachments, ...newAttachments]); - }; - - if (!showCompose) return null; - - return ( -
-
- {/* Modal Header */} -
-

- {determineType() === 'reply' ? 'Reply' : determineType() === 'forward' ? 'Forward' : determineType() === 'reply-all' ? 'Reply All' : 'New Message'} -

- -
- - {/* Modal Body */} -
-
- {/* To Field */} -
- - setComposeTo(e.target.value)} - placeholder="recipient@example.com" - className="w-full mt-1 bg-white border-gray-300 text-gray-900" - /> -
- - {/* CC/BCC Toggle Buttons */} -
- - -
- - {/* CC Field */} - {showCc && ( -
- - setComposeCc(e.target.value)} - placeholder="cc@example.com" - className="w-full mt-1 bg-white border-gray-300 text-gray-900" - /> -
- )} - - {/* BCC Field */} - {showBcc && ( -
- - setComposeBcc(e.target.value)} - placeholder="bcc@example.com" - className="w-full mt-1 bg-white border-gray-300 text-gray-900" - /> -
- )} - - {/* Subject Field */} -
- - setComposeSubject(e.target.value)} - placeholder="Enter subject" - className="w-full mt-1 bg-white border-gray-300 text-gray-900" - /> -
- - {/* Message Body */} -
- -
- -
-
- - {/* Attachments */} - {attachments.length > 0 && ( -
-

Attachments

-
- {attachments.map((file, index) => ( -
- {file.name || file.filename} - -
- ))} -
-
- )} -
-
- - {/* Modal Footer */} -
-
- {/* File Input for Attachments */} - { - if (e.target.files && e.target.files.length > 0) { - handleFileSelection(e.target.files); - } - }} - /> - - {sending && Preparing attachment...} -
-
- - -
-
-
-
- ); } \ No newline at end of file