You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Chat.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // @flow
  2. import React from 'react';
  3. import Transition from 'react-transition-group/Transition';
  4. import { translate } from '../../../base/i18n';
  5. import { connect } from '../../../base/redux';
  6. import AbstractChat, {
  7. _mapDispatchToProps,
  8. _mapStateToProps,
  9. type Props
  10. } from '../AbstractChat';
  11. import ChatInput from './ChatInput';
  12. import ChatMessageGroup from './ChatMessageGroup';
  13. import DisplayNameForm from './DisplayNameForm';
  14. /**
  15. * React Component for holding the chat feature in a side panel that slides in
  16. * and out of view.
  17. */
  18. class Chat extends AbstractChat<Props> {
  19. /**
  20. * Whether or not the {@code Chat} component is off-screen, having finished
  21. * its hiding animation.
  22. */
  23. _isExited: boolean;
  24. /**
  25. * Reference to the HTML element at the end of the list of displayed chat
  26. * messages. Used for scrolling to the end of the chat messages.
  27. */
  28. _messagesListEnd: ?HTMLElement;
  29. /**
  30. * Initializes a new {@code Chat} instance.
  31. *
  32. * @param {Object} props - The read-only properties with which the new
  33. * instance is to be initialized.
  34. */
  35. constructor(props: Props) {
  36. super(props);
  37. this._isExited = true;
  38. this._messagesListEnd = null;
  39. // Bind event handlers so they are only bound once for every instance.
  40. this._renderPanelContent = this._renderPanelContent.bind(this);
  41. this._setMessageListEndRef = this._setMessageListEndRef.bind(this);
  42. }
  43. /**
  44. * Implements React's {@link Component#componentDidMount()}.
  45. *
  46. * @inheritdoc
  47. */
  48. componentDidMount() {
  49. this._scrollMessagesToBottom();
  50. }
  51. /**
  52. * Updates chat input focus.
  53. *
  54. * @inheritdoc
  55. */
  56. componentDidUpdate(prevProps) {
  57. if (this.props._messages !== prevProps._messages) {
  58. this._scrollMessagesToBottom();
  59. }
  60. }
  61. /**
  62. * Implements React's {@link Component#render()}.
  63. *
  64. * @inheritdoc
  65. * @returns {ReactElement}
  66. */
  67. render() {
  68. return (
  69. <Transition
  70. in = { this.props._isOpen }
  71. timeout = { 500 }>
  72. { this._renderPanelContent }
  73. </Transition>
  74. );
  75. }
  76. /**
  77. * Iterates over all the messages and creates nested arrays which hold
  78. * consecutive messages sent be the same participant.
  79. *
  80. * @private
  81. * @returns {Array<Array<Object>>}
  82. */
  83. _getMessagesGroupedBySender() {
  84. const messagesCount = this.props._messages.length;
  85. const groups = [];
  86. let currentGrouping = [];
  87. let currentGroupParticipantId;
  88. for (let i = 0; i < messagesCount; i++) {
  89. const message = this.props._messages[i];
  90. if (message.id === currentGroupParticipantId) {
  91. currentGrouping.push(message);
  92. } else {
  93. groups.push(currentGrouping);
  94. currentGrouping = [ message ];
  95. currentGroupParticipantId = message.id;
  96. }
  97. }
  98. groups.push(currentGrouping);
  99. return groups;
  100. }
  101. /**
  102. * Returns a React Element for showing chat messages and a form to send new
  103. * chat messages.
  104. *
  105. * @private
  106. * @returns {ReactElement}
  107. */
  108. _renderChat() {
  109. const groupedMessages = this._getMessagesGroupedBySender();
  110. const messages = groupedMessages.map((group, index) => {
  111. const messageType = group[0] && group[0].messageType;
  112. return (
  113. <ChatMessageGroup
  114. className = { messageType || 'remote' }
  115. key = { index }
  116. messages = { group } />
  117. );
  118. });
  119. messages.push(<div
  120. key = 'end-marker'
  121. ref = { this._setMessageListEndRef } />);
  122. return (
  123. <>
  124. <div id = 'chatconversation'>
  125. { messages }
  126. </div>
  127. <ChatInput />
  128. </>
  129. );
  130. }
  131. /**
  132. * Instantiates a React Element to display at the top of {@code Chat} to
  133. * close {@code Chat}.
  134. *
  135. * @private
  136. * @returns {ReactElement}
  137. */
  138. _renderChatHeader() {
  139. return (
  140. <div className = 'chat-header'>
  141. <div
  142. className = 'chat-close'
  143. onClick = { this.props._onToggleChat }>X</div>
  144. </div>
  145. );
  146. }
  147. _renderPanelContent: (string) => React$Node | null;
  148. /**
  149. * Renders the contents of the chat panel, depending on the current
  150. * animation state provided by {@code Transition}.
  151. *
  152. * @param {string} state - The current display transition state of the
  153. * {@code Chat} component, as provided by {@code Transition}.
  154. * @private
  155. * @returns {ReactElement | null}
  156. */
  157. _renderPanelContent(state) {
  158. this._isExited = state === 'exited';
  159. const { _isOpen, _showNamePrompt } = this.props;
  160. const ComponentToRender = !_isOpen && state === 'exited'
  161. ? null
  162. : (
  163. <>
  164. { this._renderChatHeader() }
  165. { _showNamePrompt
  166. ? <DisplayNameForm /> : this._renderChat() }
  167. </>
  168. );
  169. let className = '';
  170. if (_isOpen) {
  171. className = 'slideInExt';
  172. } else if (this._isExited) {
  173. className = 'invisible';
  174. }
  175. return (
  176. <div
  177. className = { `sideToolbarContainer ${className}` }
  178. id = 'sideToolbarContainer'>
  179. { ComponentToRender }
  180. </div>
  181. );
  182. }
  183. /**
  184. * Automatically scrolls the displayed chat messages down to the latest.
  185. *
  186. * @private
  187. * @returns {void}
  188. */
  189. _scrollMessagesToBottom() {
  190. if (this._messagesListEnd) {
  191. this._messagesListEnd.scrollIntoView({
  192. behavior: this._isExited ? 'auto' : 'smooth'
  193. });
  194. }
  195. }
  196. _setMessageListEndRef: (?HTMLElement) => void;
  197. /**
  198. * Sets a reference to the HTML element at the bottom of the message list.
  199. *
  200. * @param {Object} messageListEnd - The HTML element.
  201. * @private
  202. * @returns {void}
  203. */
  204. _setMessageListEndRef(messageListEnd: ?HTMLElement) {
  205. this._messagesListEnd = messageListEnd;
  206. }
  207. }
  208. export default translate(connect(_mapStateToProps, _mapDispatchToProps)(Chat));