'use client'; import React, { useMemo } from 'react'; import { EmailContent } from '@/types/email'; import { detectTextDirection, applyTextDirection } from '@/lib/utils/text-direction'; import { sanitizeHtml } from '@/lib/utils/dom-sanitizer'; interface EmailContentDisplayProps { content: EmailContent | null | undefined; className?: string; showQuotedText?: boolean; type?: 'html' | 'text' | 'auto'; debug?: boolean; } /** * Unified component for displaying email content in a consistent way * This handles both HTML and plain text content with proper styling and RTL support */ const EmailContentDisplay: React.FC = ({ content, className = '', showQuotedText = true, type = 'auto', debug = false }) => { // Create a safe content object with fallback values for missing properties const safeContent = useMemo(() => { if (!content) { return { text: '', html: undefined, isHtml: false, direction: 'ltr' as const }; } return { text: content.text || '', html: content.html, isHtml: content.isHtml, // If direction is missing, detect it from the text content direction: content.direction || detectTextDirection(content.text) }; }, [content]); // Determine what content to display based on type preference and available content const htmlToDisplay = useMemo(() => { // If no content is available, show a placeholder if (!safeContent.text && !safeContent.html) { return '
No content available
'; } // If type is explicitly set to text, or we don't have HTML and auto mode if (type === 'text' || (type === 'auto' && !safeContent.isHtml)) { // Format plain text with line breaks for HTML display if (safeContent.text) { const formattedText = safeContent.text .replace(/&/g, '&') .replace(//g, '>') .replace(/\n/g, '
'); return formattedText; } } // Otherwise use HTML content if available if (safeContent.isHtml && safeContent.html) { return safeContent.html; } // Fallback to text content if there's no HTML if (safeContent.text) { const formattedText = safeContent.text .replace(/&/g, '&') .replace(//g, '>') .replace(/\n/g, '
'); return formattedText; } return '
No content available
'; }, [safeContent, type]); // Handle quoted text display const processedHTML = useMemo(() => { if (!showQuotedText) { // This is simplified - a more robust approach would parse and handle // quoted sections more intelligently return htmlToDisplay.replace(/]*>[\s\S]*?<\/blockquote>/gi, '
[Quoted text hidden]
'); } return htmlToDisplay; }, [htmlToDisplay, showQuotedText]); // Sanitize HTML content and apply proper direction const sanitizedHTML = useMemo(() => { const clean = sanitizeHtml(processedHTML); // Apply text direction consistently using our utility return applyTextDirection(clean, safeContent.text); }, [processedHTML, safeContent.text]); return (
{/* Debug output if enabled */} {debug && (

Content Type: {safeContent.isHtml ? 'HTML' : 'Text'}

Direction: {safeContent.direction}

HTML Length: {safeContent.html?.length || 0}

Text Length: {safeContent.text?.length || 0}

)}
); }; export default EmailContentDisplay;