'use client'; import { useRef, useMemo } from 'react'; import { Loader2 } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { ScrollArea } from '@/components/ui/scroll-area'; import { Avatar, AvatarFallback } from '@/components/ui/avatar'; import { Card } from '@/components/ui/card'; import { EmailMessage, EmailAddress } from '@/types/email'; import { formatEmailAddresses, formatEmailDate } from '@/lib/utils/email-utils'; import { adaptLegacyEmail } from '@/lib/utils/email-adapter'; import EmailContentDisplay from './EmailContentDisplay'; interface EmailPreviewProps { email: EmailMessage | any; 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); // Convert legacy email to standardized format if needed const standardizedEmail = useMemo(() => { if (!email) return null; try { // Check if the email is already in the standardized format if ( email.content && typeof email.content === 'object' && 'isHtml' in email.content && 'text' in email.content ) { console.log('EmailPreview: Email is already in standardized format'); return email as EmailMessage; } // Otherwise, adapt it console.log('EmailPreview: Adapting legacy email format'); return adaptLegacyEmail(email); } catch (error) { console.error('Error adapting email:', error); return null; } }, [email]); // Get sender initials for avatar const getSenderInitials = (name: string) => { if (!name) return ''; return name .split(" ") .map((n) => n?.[0] || '') .join("") .toUpperCase() .slice(0, 2); }; // Display loading state if (loading) { return (

Loading email...

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

Select an email to view

); } // Debug output for content structure console.log('EmailPreview: Standardized Email Content:', standardizedEmail.content); const sender = standardizedEmail.from && standardizedEmail.from.length > 0 ? standardizedEmail.from[0] : undefined; // Check for attachments const hasAttachments = standardizedEmail.attachments && standardizedEmail.attachments.length > 0; return ( {/* Email header */}

{standardizedEmail.subject}

{getSenderInitials(sender?.name || '')}
{sender?.name || sender?.address}
{formatEmailDate(standardizedEmail.date)}
To: {formatEmailAddresses(standardizedEmail.to)}
{standardizedEmail.cc && standardizedEmail.cc.length > 0 && (
Cc: {formatEmailAddresses(standardizedEmail.cc)}
)}
{/* Action buttons */} {onReply && (
)}
{/* Attachments list */} {hasAttachments && (

Attachments ({standardizedEmail.attachments.length})

{standardizedEmail.attachments.map((attachment, index) => (
{attachment.filename}
))}
)}
{/* Email body */}
{/* Render the email content using the new standardized component */}
{/* Always show debugging info in development mode */} {process.env.NODE_ENV === 'development' && (
Email Debug Info

Email ID: {standardizedEmail.id}

Content Type: {standardizedEmail.content.isHtml ? 'HTML' : 'Plain Text'}

Text Direction: {standardizedEmail.content.direction || 'ltr'}

Content Size: HTML: {standardizedEmail.content.html?.length || 0} chars, Text: {standardizedEmail.content.text?.length || 0} chars

Content Structure: {JSON.stringify(standardizedEmail.content, null, 2)}


Original Email Type: {typeof email}

Original Content Type: {typeof email.content}

{email && typeof email.content === 'object' && (

Original Content Keys: {Object.keys(email.content).join(', ')}

)} {email && email.html && (

Has HTML property: {email.html.length} chars

)} {email && email.text && (

Has Text property: {email.text.length} chars

)}
)}
); }