'use client'; import { useState, useEffect } from 'react'; import DOMPurify from 'isomorphic-dompurify'; import { EmailMessage } from '@/lib/services/email-service'; import { Loader2, Paperclip, Download } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { cleanHtml } from '@/lib/mail-parser-wrapper'; interface EmailPreviewProps { email: EmailMessage | null; loading?: boolean; onReply?: (type: 'reply' | 'reply-all' | 'forward') => void; } export default function EmailPreview({ email, loading = false, onReply }: EmailPreviewProps) { const [contentLoading, setContentLoading] = useState(false); // Handle sanitizing and rendering HTML content const renderContent = () => { if (!email?.content) return

No content available

; try { // Use DOMPurify directly with enhanced sanitization options const sanitizedContent = DOMPurify.sanitize(email.content, { ADD_TAGS: ['style', 'meta', 'link', 'table', 'thead', 'tbody', 'tr', 'td', 'th', 'hr', 'font', 'div', 'span', 'a', 'img', 'b', 'strong', 'i', 'em', 'u', 'br', 'p', 'ul', 'ol', 'li', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'pre', 'code', 'center', 'section', 'header', 'footer', 'article', 'nav', 'keyframes'], ADD_ATTR: ['*', 'colspan', 'rowspan', 'cellpadding', 'cellspacing', 'border', 'bgcolor', 'width', 'height', 'align', 'valign', 'class', 'id', 'style', 'color', 'face', 'size', 'background', 'src', 'href', 'target', 'rel', 'alt', 'title', 'name', 'animation', 'animation-name', 'animation-duration', 'animation-fill-mode'], ALLOW_UNKNOWN_PROTOCOLS: true, WHOLE_DOCUMENT: true, KEEP_CONTENT: true, RETURN_DOM: false, FORBID_TAGS: ['script', 'iframe', 'object', 'embed', 'form', 'input', 'button', 'select', 'option', 'textarea', 'canvas', 'video', 'audio'], FORBID_ATTR: ['onerror', 'onload', 'onclick', 'onmouseover', 'onmouseout', 'onchange', 'onsubmit'], USE_PROFILES: { html: true, svg: false, svgFilters: false, mathMl: false }, FORCE_BODY: true }); return (
); } catch (error) { console.error('Error rendering email content:', error); return

Error displaying email content

; } }; // Format the date const formatDate = (date: Date | string) => { if (!date) return ''; const dateObj = date instanceof Date ? date : new Date(date); return dateObj.toLocaleString('en-US', { weekday: 'short', year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' }); }; // Format email addresses const formatEmailAddresses = (addresses: Array<{name: string, address: string}> | undefined) => { if (!addresses || addresses.length === 0) return ''; return addresses.map(addr => addr.name && addr.name !== addr.address ? `${addr.name} <${addr.address}>` : addr.address ).join(', '); }; if (loading || contentLoading) { return (

Loading email content...

); } if (!email) { return (

Select an email to view

); } return (
{/* Email header */}

{email.subject}

From: {formatEmailAddresses(email.from)}
{formatDate(email.date)}
{email.to && email.to.length > 0 && (
To: {formatEmailAddresses(email.to)}
)} {email.cc && email.cc.length > 0 && (
Cc: {formatEmailAddresses(email.cc)}
)}
{/* Action buttons */} {onReply && (
)} {/* Attachments */} {email.attachments && email.attachments.length > 0 && (
Attachments:
{email.attachments.map((attachment, index) => ( {attachment.filename} ({Math.round(attachment.size / 1024)}KB) ))}
)}
{/* Email content */}
{renderContent()}
); }