'use client'; import { useState, useRef, useEffect, useMemo } from 'react'; import { Loader2, Paperclip, User } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { ScrollArea } from '@/components/ui/scroll-area'; import { formatReplyEmail, formatForwardedEmail, formatEmailForReplyOrForward, EmailMessage as FormatterEmailMessage, sanitizeHtml } from '@/lib/utils/email-formatter'; import { Avatar, AvatarFallback } from '@/components/ui/avatar'; import { AvatarImage } from '@/components/ui/avatar'; import { Card } from '@/components/ui/card'; import { cn } from '@/lib/utils'; import { CalendarIcon, PaperclipIcon } from 'lucide-react'; import Link from 'next/link'; interface EmailAddress { name: string; address: string; } interface EmailMessage { id: string; messageId?: string; subject: string; from: EmailAddress[]; to: EmailAddress[]; cc?: EmailAddress[]; bcc?: EmailAddress[]; date: Date | string; flags?: { seen: boolean; flagged: boolean; answered: boolean; deleted: boolean; draft: boolean; }; preview?: string; content?: string; html?: string; text?: string; formattedContent?: string; hasAttachments?: boolean; attachments?: Array<{ filename: string; contentType: string; size: number; path?: string; content?: string; }>; folder?: string; size?: number; contentFetched?: boolean; } interface EmailPreviewProps { email: EmailMessage | null; loading?: boolean; onReply?: (type: 'reply' | 'reply-all' | 'forward') => void; } export default function EmailPreview({ email, loading = false, onReply }: EmailPreviewProps) { // Add editorRef to match ComposeEmail exactly const editorRef = useRef(null); // Format the date const formatDate = (date: Date | string) => { if (!date) return ''; const dateObj = date instanceof Date ? date : new Date(date); return dateObj.toLocaleString('en-US', { weekday: 'short', year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' }); }; // Format email addresses const formatEmailAddresses = (addresses: Array<{name: string, address: string}> | undefined) => { if (!addresses || addresses.length === 0) return ''; return addresses.map(addr => addr.name && addr.name !== addr.address ? `${addr.name} <${addr.address}>` : addr.address ).join(', '); }; // Get sender initials for avatar const getSenderInitials = (name: string) => { if (!name) return ''; return name .split(" ") .map((n) => n?.[0] || '') .join("") .toUpperCase() .slice(0, 2); }; // Format the email content using the same formatter as ComposeEmail const formattedContent = useMemo(() => { if (!email) return ''; try { // Convert to the formatter message format - same as what ComposeEmail does const formatterEmail: FormatterEmailMessage = { id: email.id, messageId: email.messageId, subject: email.subject, from: email.from || [], to: email.to || [], cc: email.cc || [], bcc: email.bcc || [], date: email.date instanceof Date ? email.date : new Date(email.date), content: email.content || '', html: email.html, text: email.text, hasAttachments: email.hasAttachments || false }; // Get the formatted content - if already formatted content is provided, use that instead if (email.formattedContent) { return email.formattedContent; } // Otherwise sanitize the content for display return sanitizeHtml(email.content || email.html || email.text || ''); } catch (error) { console.error('Error formatting email content:', error); return email.content || email.html || email.text || ''; } }, [email]); // Display loading state if (loading) { return (

Loading email...

); } // No email selected if (!email) { return (

Select an email to view

); } const sender = email.from && email.from.length > 0 ? email.from[0] : undefined; return ( {/* Email header */}

{email.subject}

{getSenderInitials(sender?.name || '')}
{sender?.name || sender?.address}
{formatDate(email.date)}
To: {formatEmailAddresses(email.to)}
{email.cc && email.cc.length > 0 && (
Cc: {formatEmailAddresses(email.cc)}
)}
{/* Action buttons */} {onReply && (
)}
{/* Attachments */} {email.attachments && email.attachments.length > 0 && (
Attachments
{email.attachments.map((attachment, index) => ( {attachment.filename} ({Math.round(attachment.size / 1024)}KB) ))}
)}
{/* Email content - using the preformatted content */}
); }