MessageList component renders a virtualized, scrollable list of messages using react-virtuoso. It automatically groups messages by date, shows avatars for incoming messages, and displays typing indicators at the bottom.
Location
components/chat/message-list.tsx
Props
Unique identifier for the chat conversation
Array of message objects to display
Initials to display in contact avatars
ID of the current user (to determine incoming vs outgoing messages)
Whether to show the typing indicator
Custom label for the typing indicator (defaults to “typing”)
Callback when a user clicks on a media attachment
Features
Virtualization with react-virtuoso
The component usesreact-virtuoso for efficient rendering of long message histories. Only visible messages are rendered in the DOM, enabling smooth scrolling even with thousands of messages.
components/chat/message-list.tsx
Date dividers
Messages are automatically grouped by date with divider labels:components/chat/message-list.tsx
Timeline building
ThebuildTimeline function creates a timeline of items including both messages and date dividers:
components/chat/message-list.tsx
Avatar display logic
Avatars are shown for incoming messages when the previous message was outgoing (creating visual grouping by sender):components/chat/message-list.tsx
Automatic scroll to bottom
When the chat changes, the list automatically scrolls to the most recent message:components/chat/message-list.tsx
Typing indicator footer
The component includes a custom footer that displays a typing indicator whenisTyping is true:
components/chat/message-list.tsx
Usage
Performance considerations
- Virtualization: Only renders visible items, making it efficient for thousands of messages
- Memoization: The timeline is memoized with
useMemoto avoid rebuilding on every render - Mounted check: Uses
isMountedstate to prevent hydration mismatches in SSR
Related components
- MessageBubble - Individual message component
- TypingIndicator - Animated typing indicator
- ChatPanel - Parent component that provides messages