import React, { useState, useMemo } from 'react'; import { Copy, Check } from 'lucide-react'; import { NotificationPlatform } from '../../types'; import { parseMarkdownToHtml, isHtmlContent, stripMarkdown } from '../../lib/markdownUtils'; export interface MessageViewerProps { content?: string; message?: string; platform?: NotificationPlatform | number | string; subject?: string; className?: string; showFormatHeader?: boolean; maxHeightClass?: string; variant?: string; createdDate?: string; } export const MessageViewer: React.FC = ({ content, message, platform, subject, className = '', maxHeightClass = 'max-h-[65vh]', }) => { const rawContent = (content !== undefined && content !== null ? content : message) ?? ''; const [copied, setCopied] = useState(false); // Determine whether this message represents an Email (HTML) or In-App (Markdown) const isEmail = useMemo(() => { if ( platform === NotificationPlatform.Email || platform === 1 || platform === '1' || String(platform).toLowerCase() === 'email' ) { return true; } if ( platform !== NotificationPlatform.InApp && platform !== 5 && platform !== '5' && String(platform).toLowerCase() !== 'inapp' && isHtmlContent(rawContent) ) { return true; } return false; }, [platform, rawContent]); // Requirement #1: In-App message contents encoded as markdown, rendered for user; Email in HTML // Requirement #8: User is none the wiser of raw form or format in any Messages UI const renderedHtml = useMemo(() => { if (!rawContent || !rawContent.trim()) { return '

No message content

'; } if (isEmail) { if (isHtmlContent(rawContent)) { return rawContent.replace(/)<[^<]*)*<\/script>/gi, ''); } return parseMarkdownToHtml(rawContent); } // In-App: parsed from Markdown to styled HTML return parseMarkdownToHtml(rawContent); }, [rawContent, isEmail]); const handleCopy = () => { const textToCopy = stripMarkdown(rawContent) || rawContent; navigator.clipboard.writeText(textToCopy); setCopied(true); setTimeout(() => setCopied(false), 2000); }; return (
{/* Top action toolbar - clean without technical format metadata */}
{subject || 'Message Content'} {/* Copy Clean Text */}
{/* Rendered Body - Prose & Markdown styling */}
); };