import { Component } from 'react'; import { IMessage } from '../reducer'; export interface IProps { /** * The messages array to render. */ messages: IMessage[]; } /** * Abstract component to display a list of chat messages, grouped by sender. * * @augments PureComponent */ export default class AbstractMessageContainer

extends Component { static defaultProps = { messages: [] as IMessage[] }; /** * Iterates over all the messages and creates nested arrays which hold * consecutive messages sent by the same participant. * * @private * @returns {Array>} */ _getMessagesGroupedBySender() { const messagesCount = this.props.messages.length; const groups: IMessage[][] = []; let currentGrouping: IMessage[] = []; let currentGroupParticipantId; for (let i = 0; i < messagesCount; i++) { const message = this.props.messages[i]; if (message.id === currentGroupParticipantId) { currentGrouping.push(message); } else { currentGrouping.length && groups.push(currentGrouping); currentGrouping = [ message ]; currentGroupParticipantId = message.id; } } currentGrouping.length && groups.push(currentGrouping); return groups; } }