2019-05-04 21:37:30 +00:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import { PureComponent } from 'react';
|
|
|
|
|
|
|
|
export type Props = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The messages array to render.
|
|
|
|
*/
|
|
|
|
messages: Array<Object>
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract component to display a list of chat messages, grouped by sender.
|
|
|
|
*
|
|
|
|
* @extends PureComponent
|
|
|
|
*/
|
2019-11-21 14:26:57 +00:00
|
|
|
export default class AbstractMessageContainer<P: Props> extends PureComponent<P> {
|
2019-05-04 21:37:30 +00:00
|
|
|
static defaultProps = {
|
|
|
|
messages: []
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterates over all the messages and creates nested arrays which hold
|
|
|
|
* consecutive messages sent by the same participant.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {Array<Array<Object>>}
|
|
|
|
*/
|
|
|
|
_getMessagesGroupedBySender() {
|
|
|
|
const messagesCount = this.props.messages.length;
|
|
|
|
const groups = [];
|
|
|
|
let currentGrouping = [];
|
|
|
|
let currentGroupParticipantId;
|
|
|
|
|
|
|
|
for (let i = 0; i < messagesCount; i++) {
|
|
|
|
const message = this.props.messages[i];
|
|
|
|
|
|
|
|
if (message.id === currentGroupParticipantId) {
|
|
|
|
currentGrouping.push(message);
|
|
|
|
} else {
|
2019-05-06 18:21:41 +00:00
|
|
|
currentGrouping.length && groups.push(currentGrouping);
|
2019-05-04 21:37:30 +00:00
|
|
|
|
|
|
|
currentGrouping = [ message ];
|
|
|
|
currentGroupParticipantId = message.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:26:57 +00:00
|
|
|
currentGrouping.length && groups.push(currentGrouping);
|
2019-05-04 21:37:30 +00:00
|
|
|
|
|
|
|
return groups;
|
|
|
|
}
|
|
|
|
}
|