import React, { useState, useMemo } from 'react'; import { Copy, Check, Mail, MessageSquare, Code2, Eye, ShieldCheck, } from 'lucide-react'; import { NotificationPlatform } from '../../types'; import { parseMarkdownToHtml, isHtmlContent, stripMarkdown } from '../../lib/markdownUtils'; export interface MessageViewerProps { content: string; platform?: NotificationPlatform | number | string; subject?: string; className?: string; showFormatHeader?: boolean; maxHeightClass?: string; } export const MessageViewer: React.FC = ({ content, platform, subject, className = '', showFormatHeader = true, maxHeightClass = 'max-h-[60vh]', }) => { const [copied, setCopied] = useState(false); const [showRaw, setShowRaw] = useState(false); // Determine whether this message represents an Email (HTML) or In-App (Markup) const isEmail = useMemo(() => { if ( platform === NotificationPlatform.Email || platform === 1 || platform === '1' || String(platform).toLowerCase() === 'email' ) { return true; } // If platform is not explicitly InApp, detect if content is structured HTML if ( platform !== NotificationPlatform.InApp && platform !== 5 && platform !== '5' && String(platform).toLowerCase() !== 'inapp' && isHtmlContent(content) ) { return true; } return false; }, [platform, content]); const isInApp = useMemo(() => { if ( platform === NotificationPlatform.InApp || platform === 5 || platform === '5' || String(platform).toLowerCase() === 'inapp' ) { return true; } return !isEmail; }, [platform, isEmail]); // Convert the message to its respective format: // - Email: HTML rendered // - In-App: Markup (Markdown) parsed to HTML const renderedHtml = useMemo(() => { if (!content) return '

No message content

'; // Format 1: Email => HTML if (isEmail) { if (isHtmlContent(content)) { return content.replace(/)<[^<]*)*<\/script>/gi, ''); } return parseMarkdownToHtml(content); } // Format 2: In-App => Markup (Markdown) return parseMarkdownToHtml(content); }, [content, isEmail]); const handleCopy = () => { const textToCopy = showRaw ? content : stripMarkdown(content) || content; navigator.clipboard.writeText(textToCopy); setCopied(true); setTimeout(() => setCopied(false), 2000); }; return (
{/* Format Header & Controls Bar */} {showFormatHeader && (
{isEmail ? ( Email (HTML) ) : ( In-App (Markup) )} {isEmail ? 'Rendered as HTML Email' : 'Converted from Markup to Formatted View'}
{/* View Mode Toggle: Formatted vs Raw */} {/* Copy Button */}
)} {/* Message Body Content - with explicit max height, max width, word wrapping, and automatic scrollbars */}
{showRaw ? (
            {content}
          
) : (
)}
{/* Footer Info Stamp */}
PFM Communications • {isEmail ? 'Email HTML Format' : 'In-App Markup Format'}
{stripMarkdown(content).length} characters
); };