'use client'; import React, { useState } from 'react'; import { Loader2, Paperclip, Download } from 'lucide-react'; import { sanitizeHtml } from '@/lib/utils/dom-purify-config'; import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'; import { Avatar, AvatarFallback } from '@/components/ui/avatar'; interface EmailAddress { name: string; address: string; } interface Email { id: string; subject: string; from: EmailAddress[]; to: EmailAddress[]; cc?: EmailAddress[]; bcc?: EmailAddress[]; date: Date | string; content?: string; html?: string; text?: string; hasAttachments?: boolean; attachments?: Array<{ filename: string; contentType: string; size: number; path?: string; content?: string; }>; } interface EmailContentProps { email: Email; } export default function EmailContent({ email }: EmailContentProps) { const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); // 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))}
{email.content ? (
) : (

No content available

)} {renderAttachments()}
); }