Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. let className = 'remote';
  113. if (messageType === 'local') {
  114. className = 'local';
  115. } else if (messageType === 'error') {
  116. className = 'error';
  117. }
  118. return (
  119. <ChatMessageGroup
  120. className = { className }
  121. key = { index }
  122. messages = { group } />
  123. );
  124. });
  125. messages.push(<div
  126. key = 'end-marker'
  127. ref = { this._setMessageListEndRef } />);
  128. return (
  129. <>
  130. <div id = 'chatconversation'>
  131. { messages }
  132. </div>
  133. <ChatInput />
  134. </>
  135. );
  136. }
  137. /**
  138. * Instantiates a React Element to display at the top of {@code Chat} to
  139. * close {@code Chat}.
  140. *
  141. * @private
  142. * @returns {ReactElement}
  143. */
  144. _renderChatHeader() {
  145. return (
  146. <div className = 'chat-header'>
  147. <div
  148. className = 'chat-close'
  149. onClick = { this.props._onToggleChat }>X</div>
  150. </div>
  151. );
  152. }
  153. _renderPanelContent: (string) => React$Node | null;
  154. /**
  155. * Renders the contents of the chat panel, depending on the current
  156. * animation state provided by {@code Transition}.
  157. *
  158. * @param {string} state - The current display transition state of the
  159. * {@code Chat} component, as provided by {@code Transition}.
  160. * @private
  161. * @returns {ReactElement | null}
  162. */
  163. _renderPanelContent(state) {
  164. this._isExited = state === 'exited';
  165. const { _isOpen, _showNamePrompt } = this.props;
  166. const ComponentToRender = !_isOpen && state === 'exited'
  167. ? null
  168. : (
  169. <>
  170. { this._renderChatHeader() }
  171. { _showNamePrompt
  172. ? <DisplayNameForm /> : this._renderChat() }
  173. </>
  174. );
  175. let className = '';
  176. if (_isOpen) {
  177. className = 'slideInExt';
  178. } else if (this._isExited) {
  179. className = 'invisible';
  180. }
  181. return (
  182. <div
  183. className = { `sideToolbarContainer ${className}` }
  184. id = 'sideToolbarContainer'>
  185. { ComponentToRender }
  186. </div>
  187. );
  188. }
  189. /**
  190. * Automatically scrolls the displayed chat messages down to the latest.
  191. *
  192. * @private
  193. * @returns {void}
  194. */
  195. _scrollMessagesToBottom() {
  196. if (this._messagesListEnd) {
  197. this._messagesListEnd.scrollIntoView({
  198. behavior: this._isExited ? 'auto' : 'smooth'
  199. });
  200. }
  201. }
  202. _setMessageListEndRef: (?HTMLElement) => void;
  203. /**
  204. * Sets a reference to the HTML element at the bottom of the message list.
  205. *
  206. * @param {Object} messageListEnd - The HTML element.
  207. * @private
  208. * @returns {void}
  209. */
  210. _setMessageListEndRef(messageListEnd: ?HTMLElement) {
  211. this._messagesListEnd = messageListEnd;
  212. }
  213. }
  214. export default translate(connect(_mapStateToProps, _mapDispatchToProps)(Chat));