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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 ChatMessage from './ChatMessage';
  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._renderMessage = this._renderMessage.bind(this);
  41. this._renderPanelContent = this._renderPanelContent.bind(this);
  42. this._setMessageListEndRef = this._setMessageListEndRef.bind(this);
  43. }
  44. /**
  45. * Implements React's {@link Component#componentDidMount()}.
  46. *
  47. * @inheritdoc
  48. */
  49. componentDidMount() {
  50. this._scrollMessagesToBottom();
  51. }
  52. /**
  53. * Updates chat input focus.
  54. *
  55. * @inheritdoc
  56. */
  57. componentDidUpdate(prevProps) {
  58. if (this.props._messages !== prevProps._messages) {
  59. this._scrollMessagesToBottom();
  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. * Returns a React Element for showing chat messages and a form to send new
  79. * chat messages.
  80. *
  81. * @private
  82. * @returns {ReactElement}
  83. */
  84. _renderChat() {
  85. const messages = this.props._messages.map(this._renderMessage);
  86. messages.push(<div
  87. key = 'end-marker'
  88. ref = { this._setMessageListEndRef } />);
  89. return (
  90. <div
  91. className = 'sideToolbarContainer__inner'
  92. id = 'chat_container'>
  93. <div id = 'chatconversation'>
  94. { messages }
  95. </div>
  96. <ChatInput />
  97. </div>
  98. );
  99. }
  100. _renderMessage: (Object) => void;
  101. /**
  102. * Called by {@code _onSubmitMessage} to create the chat div.
  103. *
  104. * @param {string} message - The chat message to display.
  105. * @param {string} id - The chat message ID to use as a unique key.
  106. * @returns {Array<ReactElement>}
  107. */
  108. _renderMessage(message: Object, id: string) {
  109. return (
  110. <ChatMessage
  111. key = { id }
  112. message = { message } />
  113. );
  114. }
  115. _renderPanelContent: (string) => React$Node | null;
  116. /**
  117. * Renders the contents of the chat panel, depending on the current
  118. * animation state provided by {@code Transition}.
  119. *
  120. * @param {string} state - The current display transition state of the
  121. * {@code Chat} component, as provided by {@code Transition}.
  122. * @private
  123. * @returns {ReactElement | null}
  124. */
  125. _renderPanelContent(state) {
  126. this._isExited = state === 'exited';
  127. const { _isOpen, _onToggleChat, _showNamePrompt } = this.props;
  128. const ComponentToRender = !_isOpen && state === 'exited'
  129. ? null
  130. : (
  131. <div>
  132. <div
  133. className = 'chat-close'
  134. onClick = { _onToggleChat }>X</div>
  135. { _showNamePrompt
  136. ? <DisplayNameForm /> : this._renderChat() }
  137. </div>
  138. );
  139. let className = '';
  140. if (_isOpen) {
  141. className = 'slideInExt';
  142. } else if (this._isExited) {
  143. className = 'invisible';
  144. }
  145. return (
  146. <div
  147. className = { className }
  148. id = 'sideToolbarContainer'>
  149. { ComponentToRender }
  150. </div>
  151. );
  152. }
  153. /**
  154. * Automatically scrolls the displayed chat messages down to the latest.
  155. *
  156. * @private
  157. * @returns {void}
  158. */
  159. _scrollMessagesToBottom() {
  160. if (this._messagesListEnd) {
  161. this._messagesListEnd.scrollIntoView({
  162. behavior: this._isExited ? 'auto' : 'smooth'
  163. });
  164. }
  165. }
  166. _setMessageListEndRef: (?HTMLElement) => void;
  167. /**
  168. * Sets a reference to the HTML element at the bottom of the message list.
  169. *
  170. * @param {Object} messageListEnd - The HTML element.
  171. * @private
  172. * @returns {void}
  173. */
  174. _setMessageListEndRef(messageListEnd: ?HTMLElement) {
  175. this._messagesListEnd = messageListEnd;
  176. }
  177. }
  178. export default translate(connect(_mapStateToProps, _mapDispatchToProps)(Chat));