2022-08-26 06:21:41 +00:00
|
|
|
import { Component } from 'react';
|
2019-05-04 21:37:30 +00:00
|
|
|
|
2022-08-26 06:21:41 +00:00
|
|
|
import { IMessage } from '../reducer';
|
2019-05-04 21:37:30 +00:00
|
|
|
|
2022-10-20 09:11:27 +00:00
|
|
|
export interface IProps {
|
2019-05-04 21:37:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The messages array to render.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
messages: IMessage[];
|
2019-05-04 21:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract component to display a list of chat messages, grouped by sender.
|
|
|
|
*
|
2021-11-04 21:10:43 +00:00
|
|
|
* @augments PureComponent
|
2019-05-04 21:37:30 +00:00
|
|
|
*/
|
2022-10-20 09:11:27 +00:00
|
|
|
export default class AbstractMessageContainer<P extends IProps, S> extends Component<P, S> {
|
2019-05-04 21:37:30 +00:00
|
|
|
static defaultProps = {
|
2022-08-26 06:21:41 +00:00
|
|
|
messages: [] as IMessage[]
|
2019-05-04 21:37:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2022-08-26 06:21:41 +00:00
|
|
|
const groups: IMessage[][] = [];
|
|
|
|
let currentGrouping: IMessage[] = [];
|
2019-05-04 21:37:30 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|