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 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 DisplayNameForm from './DisplayNameForm';
  13. import MessageContainer from './MessageContainer';
  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 React Component for displaying chat messages. Used for
  26. * scrolling to the end of the chat messages.
  27. */
  28. _messageContainerRef: Object;
  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._messageContainerRef = React.createRef();
  39. // Bind event handlers so they are only bound once for every instance.
  40. this._renderPanelContent = this._renderPanelContent.bind(this);
  41. }
  42. /**
  43. * Implements {@code Component#componentDidMount}.
  44. *
  45. * @inheritdoc
  46. */
  47. componentDidMount() {
  48. this._scrollMessageContainerToBottom(true);
  49. }
  50. /**
  51. * Implements {@code Component#componentDidUpdate}.
  52. *
  53. * @inheritdoc
  54. */
  55. componentDidUpdate(prevProps) {
  56. if (this.props._messages !== prevProps._messages) {
  57. this._scrollMessageContainerToBottom(true);
  58. } else if (this.props._isOpen && !prevProps._isOpen) {
  59. this._scrollMessageContainerToBottom(false);
  60. }
  61. }
  62. /**
  63. * Implements React's {@link Component#render()}.
  64. *
  65. * @inheritdoc
  66. * @returns {ReactElement}
  67. */
  68. render() {
  69. return (
  70. <Transition
  71. in = { this.props._isOpen }
  72. timeout = { 500 }>
  73. { this._renderPanelContent }
  74. </Transition>
  75. );
  76. }
  77. /**
  78. * Iterates over all the messages and creates nested arrays which hold
  79. * consecutive messages sent be the same participant.
  80. *
  81. * @private
  82. * @returns {Array<Array<Object>>}
  83. */
  84. _getMessagesGroupedBySender() {
  85. const messagesCount = this.props._messages.length;
  86. const groups = [];
  87. let currentGrouping = [];
  88. let currentGroupParticipantId;
  89. for (let i = 0; i < messagesCount; i++) {
  90. const message = this.props._messages[i];
  91. if (message.id === currentGroupParticipantId) {
  92. currentGrouping.push(message);
  93. } else {
  94. groups.push(currentGrouping);
  95. currentGrouping = [ message ];
  96. currentGroupParticipantId = message.id;
  97. }
  98. }
  99. groups.push(currentGrouping);
  100. return groups;
  101. }
  102. /**
  103. * Returns a React Element for showing chat messages and a form to send new
  104. * chat messages.
  105. *
  106. * @private
  107. * @returns {ReactElement}
  108. */
  109. _renderChat() {
  110. return (
  111. <>
  112. <MessageContainer
  113. messages = { this.props._messages }
  114. ref = { this._messageContainerRef } />
  115. <ChatInput />
  116. </>
  117. );
  118. }
  119. /**
  120. * Instantiates a React Element to display at the top of {@code Chat} to
  121. * close {@code Chat}.
  122. *
  123. * @private
  124. * @returns {ReactElement}
  125. */
  126. _renderChatHeader() {
  127. return (
  128. <div className = 'chat-header'>
  129. <div
  130. className = 'chat-close'
  131. onClick = { this.props._onToggleChat }>X</div>
  132. </div>
  133. );
  134. }
  135. _renderPanelContent: (string) => React$Node | null;
  136. /**
  137. * Renders the contents of the chat panel, depending on the current
  138. * animation state provided by {@code Transition}.
  139. *
  140. * @param {string} state - The current display transition state of the
  141. * {@code Chat} component, as provided by {@code Transition}.
  142. * @private
  143. * @returns {ReactElement | null}
  144. */
  145. _renderPanelContent(state) {
  146. this._isExited = state === 'exited';
  147. const { _isOpen, _showNamePrompt } = this.props;
  148. const ComponentToRender = !_isOpen && state === 'exited'
  149. ? null
  150. : (
  151. <>
  152. { this._renderChatHeader() }
  153. { _showNamePrompt
  154. ? <DisplayNameForm /> : this._renderChat() }
  155. </>
  156. );
  157. let className = '';
  158. if (_isOpen) {
  159. className = 'slideInExt';
  160. } else if (this._isExited) {
  161. className = 'invisible';
  162. }
  163. return (
  164. <div
  165. className = { `sideToolbarContainer ${className}` }
  166. id = 'sideToolbarContainer'>
  167. { ComponentToRender }
  168. </div>
  169. );
  170. }
  171. /**
  172. * Scrolls the chat messages so the latest message is visible.
  173. *
  174. * @param {boolean} withAnimation - Whether or not to show a scrolling
  175. * animation.
  176. * @private
  177. * @returns {void}
  178. */
  179. _scrollMessageContainerToBottom(withAnimation) {
  180. if (this._messageContainerRef.current) {
  181. this._messageContainerRef.current.scrollToBottom(withAnimation);
  182. }
  183. }
  184. }
  185. export default translate(connect(_mapStateToProps, _mapDispatchToProps)(Chat));