'use client'; import React, { useMemo, CSSProperties } from 'react'; import { renderEmailContent, normalizeEmailContent } from '@/lib/utils/email-utils'; import { EmailContent } from '@/types/email'; interface EmailContentDisplayProps { content: EmailContent | any; 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 }) => { // Normalize the content to our standard format if needed const normalizedContent = useMemo(() => { try { // Handle different input types if (!content) { return { html: undefined, text: 'No content available', isHtml: false, direction: 'ltr' } as EmailContent; } // If content is already in our EmailContent format if (content && typeof content === 'object' && 'text' in content && 'isHtml' in content) { return content as EmailContent; } // Special case for simple string content if (typeof content === 'string') { return normalizeEmailContent({ content }); } // Otherwise normalize it return normalizeEmailContent(content); } catch (error) { console.error('Error normalizing content in EmailContentDisplay:', error); return { html: undefined, text: `Error processing email content: ${error instanceof Error ? error.message : 'Unknown error'}`, isHtml: false, direction: 'ltr' } as EmailContent; } }, [content]); // Render the normalized content const htmlContent = useMemo(() => { if (!normalizedContent) return ''; try { // Override content type if specified let contentToRender: EmailContent = { ...normalizedContent }; 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 in EmailContentDisplay:', error); return `
Error rendering email content: ${error instanceof Error ? error.message : 'Unknown error'}
`; } }, [normalizedContent, type]); // Apply quoted text styling if needed const containerStyle: CSSProperties = showQuotedText ? {} : { maxHeight: '400px', overflowY: 'auto' }; return (
{/* Debug output if enabled */} {debug && (

Content Type: {typeof content}

{typeof content === 'object' && (

Keys: {Object.keys(content).join(', ')}

)}

Normalized: {normalizedContent.isHtml ? 'HTML' : 'Text'}

Direction: {normalizedContent.direction}

Has HTML: {!!normalizedContent.html}

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

)}
); }; export default EmailContentDisplay;