/** * MessageBubble — individual message display. */ import { renderMarkdown } from '../../utils/renderMessage'; import sanitizeHtml from '../../utils/sanitizeHtml'; import ProductCard from './ProductCard'; /** * Parse a created_at timestamp. Server sends MySQL UTC datetime * ("2026-03-02 06:51:34") while optimistic messages use ISO. */ const formatTime = ( dt ) => { if ( ! dt ) return ''; // Already ISO with timezone → use as-is; otherwise append Z for UTC. const d = ( dt.includes( 'T' ) || dt.endsWith( 'Z' ) ) ? new Date( dt ) : new Date( dt.replace( ' ', 'T' ) + 'Z' ); if ( isNaN( d.getTime() ) ) return ''; return d.toLocaleTimeString( [], { hour: '2-digit', minute: '2-digit' } ); }; const MessageBubble = ( { message, readReceiptId } ) => { const { sender_type, content, created_at } = message; const isVisitor = sender_type === 'visitor'; const isSystem = sender_type === 'system'; // Product recommendation cards: live responses carry `actions` on the // message itself; messages restored from history/polling carry them in // persisted `metadata.actions`. const actions = message.actions || message.metadata?.actions || []; const products = Array.isArray( actions ) ? actions.filter( ( a ) => a && a.type === 'product' && a.title ) : []; if ( isSystem ) { return (
{ content }
); } // Read receipt: show double-check if visitor message ID ≤ readReceiptId. const isRead = isVisitor && readReceiptId > 0 && Number( message.id ) <= readReceiptId; return (
{ ! isVisitor && (
) }
{ ! isVisitor && products.length > 0 && (
{ products.map( ( product ) => ( ) ) }
) }
{ created_at && ( { formatTime( created_at ) } ) } { isVisitor && ( { isRead ? ( /* Double checkmark — read */ ) : ( /* Single checkmark — sent */ ) } ) }
); }; export default MessageBubble;