'use client'; import { useRef, useState } from 'react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Card, CardContent, CardHeader, CardTitle, CardFooter } from '@/components/ui/card'; import { X, Paperclip, ChevronDown, ChevronUp, SendHorizontal } from 'lucide-react'; import { Email } from '@/app/courrier/page'; 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; onBodyChange?: (body: string) => void; initialTo?: string; initialSubject?: string; initialBody?: string; initialCc?: string; initialBcc?: string; replyTo?: Email | null; forwardFrom?: Email | 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, onSend, onCancel, onBodyChange, initialTo, initialSubject, initialBody, initialCc, initialBcc, replyTo, forwardFrom }: ComposeEmailProps) { const [sending, setSending] = useState(false); const editorRef = useRef(null); const fileInputRef = useRef(null); if (!showCompose) return null; const handleInput = (e: React.FormEvent) => { const content = e.currentTarget.innerHTML; setComposeBody(content); if (onBodyChange) { onBodyChange(content); } }; const handleSendEmail = async () => { if (!composeTo.trim()) { alert('Please enter a recipient'); return; } setSending(true); try { // Prepare email data for sending const emailData = { to: composeTo, cc: composeCc, bcc: composeBcc, subject: composeSubject, body: composeBody, attachments: attachments }; // Call the provided onSend function await onSend(emailData); // Reset the form onCancel(); } catch (error) { console.error('Error sending email:', error); alert('Failed to send email. Please try again.'); } finally { setSending(false); } }; return (
{replyTo ? 'Reply' : forwardFrom ? 'Forward' : 'New Message'}
To: setComposeTo(e.target.value)} placeholder="recipient@example.com" className="border-0 focus-visible:ring-0" />
{showCc && (
Cc: setComposeCc(e.target.value)} placeholder="cc@example.com" className="border-0 focus-visible:ring-0" />
)} {showBcc && (
Bcc: setComposeBcc(e.target.value)} placeholder="bcc@example.com" className="border-0 focus-visible:ring-0" />
)}
Subject: setComposeSubject(e.target.value)} placeholder="Subject" className="border-0 focus-visible:ring-0" />
{!showCc && ( )} {!showBcc && ( )}
{attachments.length > 0 && (

Attachments

{attachments.map((attachment, index) => (
{attachment.name}
))}
)}
{ if (e.target.files?.length) { const newAttachments = Array.from(e.target.files).map(file => ({ name: file.name, type: file.type, content: URL.createObjectURL(file), file })); setAttachments([...attachments, ...newAttachments]); } }} multiple />
); }