'use client'; import React, { useEffect, useState } from 'react'; import DOMPurify from 'dompurify'; import { formatEmailContent } from '@/lib/utils/email-content'; import { sanitizeHtml } from '@/lib/utils/email-formatter'; /** * Interface for email content types */ interface ProcessedContent { html: string; text: string; isHtml: boolean; } /** * Interface for component props */ interface EmailContentDisplayProps { content: string; type?: 'html' | 'text' | 'auto'; isRawEmail?: boolean; className?: string; } /** * Parse raw email content into HTML and text parts * This is a helper function used when processing raw email formats */ function parseRawEmail(rawContent: string): { html: string; text: string } { // Simple parser for demonstration - in production, use a proper MIME parser const hasHtmlPart = rawContent.includes('/i) || rawContent.match(//i); if (htmlMatch) { htmlPart = htmlMatch[0]; } else { // Fallback extraction const parts = rawContent.split(/(?:--boundary|\r\n\r\n)/); htmlPart = parts.find(part => part.includes('Content-Type: text/html') || part.includes(']+>/g, ' ').replace(/\s+/g, ' ').trim() }; } return { html: '', text: rawContent }; } /** * EmailContentDisplay component - displays formatted email content * with proper security, styling and support for different email formats */ const EmailContentDisplay: React.FC = ({ content, type = 'auto', isRawEmail = false, className = '' }) => { const [processedContent, setProcessedContent] = useState({ html: '', text: '', isHtml: false }); // Process the email content when it changes useEffect(() => { if (!content) { setProcessedContent({ html: '', text: '', isHtml: false }); return; } try { if (isRawEmail) { // Parse raw email content const parsed = parseRawEmail(content); // Check which content to use based on type and availability const useHtml = (type === 'html' || (type === 'auto' && parsed.html)) && !!parsed.html; if (useHtml) { // Use the enhanced sanitizeHtml function from email-formatter const sanitizedHtml = sanitizeHtml(parsed.html); setProcessedContent({ html: sanitizedHtml, text: parsed.text, isHtml: true }); } else { // Format plain text properly const formattedText = formatEmailContent({ text: parsed.text }); setProcessedContent({ html: formattedText, text: parsed.text, isHtml: false }); } } else { // Treat as direct content (not raw email) const isHtmlContent = type === 'html' || ( type === 'auto' && ( content.includes('') || content.includes(']+>/g, ' ').replace(/\s+/g, ' ').trim(), isHtml: true }); } else { // Format plain text properly using formatEmailContent const formattedText = formatEmailContent({ text: content }); setProcessedContent({ html: formattedText, text: content, isHtml: false }); } } } catch (err) { console.error('Error processing email content:', err); // Fallback to plain text with basic formatting setProcessedContent({ html: `
${content.replace(//g, '>').replace(/\n/g, '
')}
`, text: content, isHtml: false }); } }, [content, type, isRawEmail]); return (
); }; export default EmailContentDisplay;