'use client'; import React, { useMemo, CSSProperties } from 'react'; import { renderEmailContent } from '@/lib/utils/email-utils'; import { EmailContent } from '@/types/email'; interface EmailContentDisplayProps { content: EmailContent; 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 */ const EmailContentDisplay: React.FC = ({ content, className = '', showQuotedText = true, type = 'auto', debug = false }) => { // Ensure we have valid content to work with const safeContent = useMemo(() => { if (!content) { return { html: undefined, text: 'No content available', isHtml: false, direction: 'ltr' } as EmailContent; } return content; }, [content]); // Render the content with proper formatting const htmlContent = useMemo(() => { if (!safeContent) return ''; try { // Override content type if specified let contentToRender: EmailContent = { ...safeContent }; if (type === 'html' && !contentToRender.isHtml) { // Force HTML rendering for text content contentToRender = { ...contentToRender, isHtml: true, html: `

${contentToRender.text.replace(/\n/g, '
')}

` }; } else if (type === 'text' && contentToRender.isHtml) { // Force text rendering contentToRender = { ...contentToRender, isHtml: false }; } return renderEmailContent(contentToRender); } catch (error) { console.error('Error rendering content:', error); return `
Error rendering email content: ${error instanceof Error ? error.message : 'Unknown error'}
`; } }, [safeContent, type]); // Apply quoted text styling if needed const containerStyle: CSSProperties = showQuotedText ? {} : { maxHeight: '400px', overflowY: 'auto' }; return (
{/* Debug output if enabled */} {debug && (

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

Direction: {safeContent.direction}

Has HTML: {!!safeContent.html}

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

)}
); }; export default EmailContentDisplay;