'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-adapters'; 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 { // Log input email details for debugging console.log('EmailPreview: Input email type:', typeof email); console.log('EmailPreview: Input email properties:', Object.keys(email)); if (email.content) { console.log('EmailPreview: Content type:', typeof email.content); if (typeof email.content === 'object') { console.log('EmailPreview: Content properties:', Object.keys(email.content)); } else { console.log('EmailPreview: Content first 100 chars:', email.content.substring(0, 100)); } } // 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'); const adapted = adaptLegacyEmail(email); // Log the adapted email structure for debugging console.log('EmailPreview: Adapted email:', { id: adapted.id, subject: adapted.subject, from: adapted.from, content: adapted.content ? { isHtml: adapted.content.isHtml, direction: adapted.content.direction, textLength: adapted.content.text?.length, htmlExists: !!adapted.content.html } : 'No content' }); return adapted; } catch (error) { console.error('Error adapting email:', error); // Instead of returning null, try to create a minimal valid email to display the error return { id: email?.id || 'error', subject: email?.subject || 'Error processing email', from: email?.from || '', to: email?.to || '', date: email?.date || new Date().toISOString(), flags: [], content: { text: `Error processing email: ${error instanceof Error ? error.message : 'Unknown error'}`, isHtml: false, direction: 'ltr' } } as EmailMessage; } }, [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); // Extract sender from string (name ) const senderInfo = standardizedEmail.from.match(/^(?:"?([^"]*)"?\s)?]+@[^\s>]+)>?$/); const senderName = senderInfo ? senderInfo[1] || senderInfo[2] : standardizedEmail.from; const senderEmail = senderInfo ? senderInfo[2] : standardizedEmail.from; // Check for attachments const hasAttachments = standardizedEmail.attachments && standardizedEmail.attachments.length > 0; return ( {/* Email header */}

{standardizedEmail.subject}

{getSenderInitials(senderName)}
{senderName}
{formatEmailDate(standardizedEmail.date)}
To: {standardizedEmail.to}
{standardizedEmail.cc && (
Cc: {standardizedEmail.cc}
)}
{/* Action buttons */} {onReply && (
)}
{/* Attachments list */} {hasAttachments && standardizedEmail.attachments && (

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

)}
)}
); }