'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 { formatEmailContent } from '@/lib/utils/email-content'; 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'; import DOMPurify from 'dompurify'; interface EmailAddress { name: string; address: string; } interface EmailAttachment { filename: string; contentType: string; size: number; path?: string; content?: string; } interface EmailMessage { id: string; uid: number; from: EmailAddress[]; to: EmailAddress[]; cc?: EmailAddress[]; bcc?: EmailAddress[]; subject: string; date: string; flags: string[]; attachments: EmailAttachment[]; content?: string | { text?: string; html?: string; }; html?: string; text?: string; formattedContent?: string; } 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 const formattedContent = useMemo(() => { if (!email) { return ''; } // Use the improved, standardized email content formatter return formatEmailContent(email); }, [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; // Update the array access to use proper type checking const hasAttachments = email.attachments && email.attachments.length > 0; 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 */} {hasAttachments && (
Attachments
{email.attachments.map((attachment, index) => ( {attachment.filename} ({Math.round(attachment.size / 1024)}KB) ))}
)}
{/* Email content */}
); }