/** * MessageList — scrollable message thread with auto-scroll. */ import { useRef, useEffect } from '@wordpress/element'; import MessageBubble from './MessageBubble'; const MessageList = ( { messages, isTyping, readReceiptId } ) => { const listRef = useRef( null ); const isNearBottomRef = useRef( true ); const prevCountRef = useRef( 0 ); const handleScroll = () => { const el = listRef.current; if ( ! el ) return; const distanceFromBottom = el.scrollHeight - el.scrollTop - el.clientHeight; isNearBottomRef.current = distanceFromBottom < 80; }; useEffect( () => { if ( ! listRef.current ) return; const grew = messages.length > prevCountRef.current; const isFirstLoad = prevCountRef.current === 0; prevCountRef.current = messages.length; // Scroll on first load, when typing, or on new messages only while the // visitor is near the bottom — so scrolling up to read is not interrupted. if ( isFirstLoad || isTyping || ( grew && isNearBottomRef.current ) ) { listRef.current.scrollTop = listRef.current.scrollHeight; } }, [ messages, isTyping ] ); return (
{ messages.map( ( msg ) => ( ) ) } { isTyping && (
) }
); }; export default MessageList;