'use client'; import React, { useEffect, useState, useRef } from 'react'; import { X, Paperclip, ChevronDown, ChevronUp, SendHorizontal, Loader2 } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Textarea } from '@/components/ui/textarea'; import { Label } from '@/components/ui/label'; import DOMPurify from 'isomorphic-dompurify'; // Add a CSS style block to handle the direction properly const forceLTRStyles = ` .force-ltr { direction: ltr !important; text-align: left !important; unicode-bidi: isolate !important; writing-mode: horizontal-tb !important; } .force-ltr * { direction: ltr !important; text-align: left !important; unicode-bidi: isolate !important; } [dir="rtl"] .force-ltr, [dir="rtl"] .force-ltr * { direction: ltr !important; text-align: left !important; } .email-content { direction: ltr !important; text-align: left !important; } .email-content * { direction: ltr !important; text-align: left !important; } `; // Nous simplifions l'interface car nous n'utilisons plus ces props interface EmailObject { id?: string; from?: string; fromName?: string; to?: string; subject?: string; content?: string; html?: string; text?: string; body?: string; date?: string; read?: boolean; starred?: boolean; attachments?: { name: string; url: string }[]; folder?: string; cc?: string; bcc?: string; } interface ComposeEmailProps { 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; // Nous laissons ces props pour la rétrocompatibilité mais nous ne les utilisons plus replyTo?: EmailObject | null; forwardFrom?: EmailObject | null; } export default function ComposeEmail({ showCompose, setShowCompose, composeTo, setComposeTo, composeCc, setComposeCc, composeBcc, setComposeBcc, composeSubject, setComposeSubject, composeBody, setComposeBody, showCc, setShowCc, showBcc, setShowBcc, attachments, setAttachments, handleSend, originalEmail, // replyTo et forwardFrom ne sont plus utilisés replyTo, forwardFrom, onSend, onCancel }: ComposeEmailProps) { const [isSending, setIsSending] = useState(false); const fileInputRef = useRef(null); const contentEditableRef = useRef(null); // Si nous avons un contenu préformaté, nous utilisons toujours l'éditeur riche const [useRichEditor, setUseRichEditor] = useState(!!composeBody); const [userMessage, setUserMessage] = useState(''); // Nous extrarons la partie de contenu citée du corps de l'email préformaté const [quotedContent, setQuotedContent] = useState(''); const [hasStartedTyping, setHasStartedTyping] = useState(false); useEffect(() => { // Utiliser l'éditeur riche si nous avons du contenu HTML if (composeBody && composeBody.includes('<')) { setUseRichEditor(true); // Séparer le contenu de l'utilisateur (vide) et le contenu cité (préformaté) setUserMessage(''); setQuotedContent(composeBody); } }, [composeBody]); // Nous n'avons plus besoin de ces effets qui initialisaient replyTo et forwardFrom // useEffect(() => { // if (replyTo) { // initializeReplyEmail(replyTo); // } // }, [replyTo, setComposeTo, setComposeSubject, setComposeBody]); // // useEffect(() => { // if (forwardFrom) { // initializeForwardedEmail(forwardFrom); // } // }, [forwardFrom]); // Nous n'avons plus besoin de ces fonctions qui initialisaient le contenu // via formatEmailForReply et formatEmailForForward // const initializeForwardedEmail = async (email: any) => {...}; // const initializeReplyEmail = async (email: any, replyType: 'reply' | 'replyAll' = 'reply') => {...}; // Handle file attachment selection const handleFileAttachment = (e: React.ChangeEvent) => { if (e.target.files) { const newFiles = Array.from(e.target.files); setAttachments([...attachments, ...newFiles]); } }; // Handle contentEditable input changes const handleUserMessageChange = () => { if (contentEditableRef.current) { const content = contentEditableRef.current.innerHTML; // Check if this is the initial state or if the user has actually typed something if (content && content !== '

Write your message here...

') { setHasStartedTyping(true); } setUserMessage(content); // Combine user message with quoted content for the full email body const combined = `${content}${quotedContent ? `
${quotedContent}
` : ''}`; setComposeBody(combined); } }; // Handle sending email with combined content const handleSendWithCombinedContent = async () => { if (isSending) return; try { setIsSending(true); // For rich editor, combine user message with quoted content if (useRichEditor) { // Wrap the content with proper direction styles const userContent = userMessage ? `
${userMessage}
` : ''; const quotedWithStyles = quotedContent ? `
${quotedContent}
` : ''; const combinedContent = `${userContent}${quotedWithStyles}`; setComposeBody(combinedContent); // Wait for state update to complete await new Promise(resolve => setTimeout(resolve, 0)); } // Call the provided onSend function await onSend({ to: composeTo, cc: composeCc, bcc: composeBcc, subject: composeSubject, body: composeBody, attachments: attachments }); // Reset the compose state setShowCompose(false); setComposeTo(''); setComposeCc(''); setComposeBcc(''); setComposeSubject(''); setComposeBody(''); setShowCc(false); setShowBcc(false); setAttachments([]); setUserMessage(''); setQuotedContent(''); } catch (error) { console.error('Failed to send email:', error); } finally { setIsSending(false); } }; return ( <> {/* Compose Email Modal */} {showCompose && (
{/* Add global styles for email direction */}