'use client'; import React, { useState, useEffect } from 'react'; import { Loader2, Paperclip, FileDown, Download } from 'lucide-react'; import { sanitizeHtml } from '@/lib/utils/email-formatter'; import { Button } from '@/components/ui/button'; import { Email } from '@/hooks/use-courrier'; import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'; import { ScrollArea } from '@/components/ui/scroll-area'; import { Avatar, AvatarFallback } from '@/components/ui/avatar'; interface EmailContentProps { email: Email; } export default function EmailContent({ email }: EmailContentProps) { const [content, setContent] = useState(null); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); useEffect(() => { if (!email) return; const renderContent = async () => { setIsLoading(true); setError(null); try { if (!email.content || email.content.length === 0) { setContent(
Email content is empty
); return; } // Use the sanitizer from the centralized formatter const sanitizedHtml = sanitizeHtml(email.content); // Look for specific markers that indicate this is a forwarded or replied email const isForwarded = sanitizedHtml.includes('---------- Forwarded message ---------'); const isReply = sanitizedHtml.includes('class="reply-body"') || sanitizedHtml.includes('blockquote style="margin: 0; padding: 10px 0 10px 15px; border-left:'); // For forwarded or replied emails, ensure we keep the exact structure if (isForwarded || isReply) { setContent(
); } else { // For regular emails, wrap in the same structure used in the compose editor setContent(
); } } catch (err) { console.error('Error rendering email content:', err); setError('Error rendering email content. Please try again.'); setContent(null); } finally { setIsLoading(false); } }; renderContent(); }, [email]); // Render attachments if they exist const renderAttachments = () => { if (!email?.attachments || email.attachments.length === 0) { return null; } return (

Attachments

{email.attachments.map((attachment, index) => (
{attachment.filename}
))}
); }; if (isLoading) { return (
); } if (error) { return ( Error {error} ); } // Format the date for display const formatDate = (dateObj: Date) => { const now = new Date(); const today = new Date(now.getFullYear(), now.getMonth(), now.getDate()); const yesterday = new Date(today); yesterday.setDate(yesterday.getDate() - 1); const date = new Date(dateObj); const formattedTime = date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); if (date >= today) { return formattedTime; } else if (date >= yesterday) { return `Yesterday, ${formattedTime}`; } else { return date.toLocaleDateString([], { month: 'short', day: 'numeric' }); } }; return (
{email.from?.[0]?.name?.[0] || email.from?.[0]?.address?.[0] || '?'}

{email.from?.[0]?.name || email.from?.[0]?.address || 'Unknown'}

to {email.to?.[0]?.address || 'you'}

{formatDate(new Date(email.date))}
{content} {renderAttachments()}
); }