2019-05-04 21:22:52 +00:00
|
|
|
// @flow
|
|
|
|
|
2019-05-04 21:37:30 +00:00
|
|
|
import React from 'react';
|
2019-05-04 21:22:52 +00:00
|
|
|
|
2019-10-15 14:08:23 +00:00
|
|
|
import { MESSAGE_TYPE_REMOTE } from '../../constants';
|
2019-05-04 21:37:30 +00:00
|
|
|
import AbstractMessageContainer, { type Props }
|
|
|
|
from '../AbstractMessageContainer';
|
2019-05-04 21:22:52 +00:00
|
|
|
|
2019-05-04 21:37:30 +00:00
|
|
|
import ChatMessageGroup from './ChatMessageGroup';
|
2019-05-04 21:22:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays all received chat messages, grouped by sender.
|
|
|
|
*
|
2019-05-04 21:37:30 +00:00
|
|
|
* @extends AbstractMessageContainer
|
2019-05-04 21:22:52 +00:00
|
|
|
*/
|
2019-11-21 14:26:57 +00:00
|
|
|
export default class MessageContainer extends AbstractMessageContainer<Props> {
|
2019-05-08 17:45:32 +00:00
|
|
|
/**
|
|
|
|
* Whether or not chat has been scrolled to the bottom of the screen. Used
|
|
|
|
* to determine if chat should be scrolled automatically to the bottom when
|
|
|
|
* the {@code ChatInput} resizes.
|
|
|
|
*/
|
|
|
|
_isScrolledToBottom: boolean;
|
|
|
|
|
2019-05-04 21:22:52 +00:00
|
|
|
/**
|
|
|
|
* Reference to the HTML element at the end of the list of displayed chat
|
|
|
|
* messages. Used for scrolling to the end of the chat messages.
|
|
|
|
*/
|
|
|
|
_messagesListEndRef: Object;
|
|
|
|
|
2019-05-08 17:45:32 +00:00
|
|
|
/**
|
|
|
|
* A React ref to the HTML element containing all {@code ChatMessageGroup}
|
|
|
|
* instances.
|
|
|
|
*/
|
|
|
|
_messageListRef: Object;
|
|
|
|
|
2019-05-04 21:22:52 +00:00
|
|
|
/**
|
|
|
|
* Initializes a new {@code MessageContainer} instance.
|
|
|
|
*
|
|
|
|
* @param {Props} props - The React {@code Component} props to initialize
|
|
|
|
* the new {@code MessageContainer} instance with.
|
|
|
|
*/
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
2019-05-08 17:45:32 +00:00
|
|
|
this._isScrolledToBottom = true;
|
|
|
|
|
|
|
|
this._messageListRef = React.createRef();
|
2019-05-04 21:22:52 +00:00
|
|
|
this._messagesListEndRef = React.createRef();
|
2019-05-08 17:45:32 +00:00
|
|
|
|
|
|
|
this._onChatScroll = this._onChatScroll.bind(this);
|
2019-05-04 21:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements {@code Component#render}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
render() {
|
|
|
|
const groupedMessages = this._getMessagesGroupedBySender();
|
|
|
|
const messages = groupedMessages.map((group, index) => {
|
|
|
|
const messageType = group[0] && group[0].messageType;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ChatMessageGroup
|
2019-10-15 14:08:23 +00:00
|
|
|
className = { messageType || MESSAGE_TYPE_REMOTE }
|
2019-05-04 21:22:52 +00:00
|
|
|
key = { index }
|
|
|
|
messages = { group } />
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
2019-05-08 17:45:32 +00:00
|
|
|
<div
|
|
|
|
id = 'chatconversation'
|
|
|
|
onScroll = { this._onChatScroll }
|
|
|
|
ref = { this._messageListRef }>
|
2019-05-04 21:22:52 +00:00
|
|
|
{ messages }
|
|
|
|
<div ref = { this._messagesListEndRef } />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-08 17:45:32 +00:00
|
|
|
/**
|
|
|
|
* Scrolls to the bottom again if the instance had previously been scrolled
|
|
|
|
* to the bottom. This method is used when a resize has occurred below the
|
|
|
|
* instance and bottom scroll needs to be maintained.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
maybeUpdateBottomScroll() {
|
|
|
|
if (this._isScrolledToBottom) {
|
|
|
|
this.scrollToBottom(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-04 21:22:52 +00:00
|
|
|
/**
|
|
|
|
* Automatically scrolls the displayed chat messages down to the latest.
|
|
|
|
*
|
2019-05-06 22:07:16 +00:00
|
|
|
* @param {boolean} withAnimation - Whether or not to show a scrolling
|
|
|
|
* animation.
|
2019-05-04 21:22:52 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2019-05-06 22:07:16 +00:00
|
|
|
scrollToBottom(withAnimation: boolean) {
|
2019-05-04 21:22:52 +00:00
|
|
|
this._messagesListEndRef.current.scrollIntoView({
|
2019-07-17 22:04:35 +00:00
|
|
|
behavior: withAnimation ? 'smooth' : 'auto',
|
|
|
|
block: 'nearest'
|
2019-05-04 21:22:52 +00:00
|
|
|
});
|
|
|
|
}
|
2019-05-06 22:07:16 +00:00
|
|
|
|
|
|
|
_getMessagesGroupedBySender: () => Array<Array<Object>>;
|
2019-05-08 17:45:32 +00:00
|
|
|
|
|
|
|
_onChatScroll: () => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback invoked to listen to the current scroll location.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onChatScroll() {
|
|
|
|
const element = this._messageListRef.current;
|
|
|
|
|
|
|
|
this._isScrolledToBottom
|
|
|
|
= element.scrollHeight - element.scrollTop === element.clientHeight;
|
|
|
|
}
|
2019-05-04 21:22:52 +00:00
|
|
|
}
|