'use client'; import { useRef, useEffect, useState } from 'react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Paperclip, X } from 'lucide-react'; import { Textarea } from '@/components/ui/textarea'; import { decodeComposeContent, encodeComposeContent } from '@/lib/compose-mime-decoder'; 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'; }; } export default function ComposeEmail({ showCompose, setShowCompose, composeTo, setComposeTo, composeCc, setComposeCc, composeBcc, setComposeBcc, composeSubject, setComposeSubject, composeBody, setComposeBody, showCc, setShowCc, showBcc, setShowBcc, attachments, setAttachments, handleSend, originalEmail }: ComposeEmailProps) { const composeBodyRef = useRef(null); const newReplyRef = useRef(null); const [showOriginalContent, setShowOriginalContent] = useState(true); useEffect(() => { if (composeBodyRef.current) { const decodedContent = decodeComposeContent(composeBody); // Create the clear structure with proper content composeBodyRef.current.innerHTML = `
`; // Place cursor in the new reply area const newReplyArea = composeBodyRef.current.querySelector('#new-reply-area'); if (newReplyArea) { const range = document.createRange(); const sel = window.getSelection(); range.setStart(newReplyArea, 0); range.collapse(true); sel?.removeAllRanges(); sel?.addRange(range); } } }, [composeBody]); // Enhanced input handler to prevent text reversal const handleInput = (e: React.FormEvent) => { if (composeBodyRef.current) { // Get the current HTML content const fullContent = e.currentTarget.innerHTML; // Split at the divider to separate new reply from original content const parts = fullContent.split('