'use client'; import { useRef } 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 } from '@/types/email'; import { formatEmailDate } from '@/lib/utils/email-utils'; import EmailContentDisplay from './EmailContentDisplay'; interface EmailPreviewProps { email: EmailMessage | null; loading?: boolean; onReply?: (type: 'reply' | 'reply-all' | 'forward') => void; } export default function EmailPreview({ email, loading = false, onReply }: EmailPreviewProps) { const editorRef = useRef(null); // Display loading state if (loading) { return (

Loading email...

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

Select an email to view

); } // Extract sender name from email.from (which is a string in our standardized format) let senderName = ''; let senderEmail = ''; if (email.from) { // If it's a string, try to extract name and email with regex const senderInfo = email.from.match(/^(?:"?([^"]*)"?\s)?]+@[^\s>]+)>?$/); senderName = senderInfo ? senderInfo[1] || senderInfo[2] : email.from; senderEmail = senderInfo ? senderInfo[2] : email.from; } // Check for attachments const hasAttachments = email.attachments && email.attachments.length > 0; // Get sender initials for avatar const getSenderInitials = (name: string) => { if (!name) return ''; return name .split(" ") .map((n) => n?.[0] || '') .join("") .toUpperCase() .slice(0, 2); }; return ( {/* Email header */}

{email.subject}

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

Attachments ({email.attachments.length})

{email.attachments.map((attachment, index) => (
{attachment.filename}
))}
)}
{/* Email body */}
{/* Debugging info - simplified */} {process.env.NODE_ENV === 'development' && (
Email Debug Info

Email ID: {email.id}

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

Text Direction: {email.content.direction}

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

)}
); }