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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. import MessageRecipient from './MessageRecipient';
  15. /**
  16. * React Component for holding the chat feature in a side panel that slides in
  17. * and out of view.
  18. */
  19. class Chat extends AbstractChat<Props> {
  20. /**
  21. * Whether or not the {@code Chat} component is off-screen, having finished
  22. * its hiding animation.
  23. */
  24. _isExited: boolean;
  25. /**
  26. * Reference to the React Component for displaying chat messages. Used for
  27. * scrolling to the end of the chat messages.
  28. */
  29. _messageContainerRef: Object;
  30. /**
  31. * Initializes a new {@code Chat} instance.
  32. *
  33. * @param {Object} props - The read-only properties with which the new
  34. * instance is to be initialized.
  35. */
  36. constructor(props: Props) {
  37. super(props);
  38. this._isExited = true;
  39. this._messageContainerRef = React.createRef();
  40. // Bind event handlers so they are only bound once for every instance.
  41. this._renderPanelContent = this._renderPanelContent.bind(this);
  42. // Bind event handlers so they are only bound once for every instance.
  43. this._onChatInputResize = this._onChatInputResize.bind(this);
  44. }
  45. /**
  46. * Implements {@code Component#componentDidMount}.
  47. *
  48. * @inheritdoc
  49. */
  50. componentDidMount() {
  51. this._scrollMessageContainerToBottom(true);
  52. }
  53. /**
  54. * Implements {@code Component#componentDidUpdate}.
  55. *
  56. * @inheritdoc
  57. */
  58. componentDidUpdate(prevProps) {
  59. if (this.props._messages !== prevProps._messages) {
  60. this._scrollMessageContainerToBottom(true);
  61. } else if (this.props._isOpen && !prevProps._isOpen) {
  62. this._scrollMessageContainerToBottom(false);
  63. }
  64. }
  65. /**
  66. * Implements React's {@link Component#render()}.
  67. *
  68. * @inheritdoc
  69. * @returns {ReactElement}
  70. */
  71. render() {
  72. return (
  73. <Transition
  74. in = { this.props._isOpen }
  75. timeout = { 500 }>
  76. { this._renderPanelContent }
  77. </Transition>
  78. );
  79. }
  80. _onChatInputResize: () => void;
  81. /**
  82. * Callback invoked when {@code ChatInput} changes height. Preserves
  83. * displaying the latest message if it is scrolled to.
  84. *
  85. * @private
  86. * @returns {void}
  87. */
  88. _onChatInputResize() {
  89. this._messageContainerRef.current.maybeUpdateBottomScroll();
  90. }
  91. /**
  92. * Returns a React Element for showing chat messages and a form to send new
  93. * chat messages.
  94. *
  95. * @private
  96. * @returns {ReactElement}
  97. */
  98. _renderChat() {
  99. return (
  100. <>
  101. <MessageContainer
  102. messages = { this.props._messages }
  103. ref = { this._messageContainerRef } />
  104. <MessageRecipient />
  105. <ChatInput
  106. onResize = { this._onChatInputResize }
  107. onSend = { this.props._onSendMessage } />
  108. </>
  109. );
  110. }
  111. /**
  112. * Instantiates a React Element to display at the top of {@code Chat} to
  113. * close {@code Chat}.
  114. *
  115. * @private
  116. * @returns {ReactElement}
  117. */
  118. _renderChatHeader() {
  119. return (
  120. <div className = 'chat-header'>
  121. <div
  122. className = 'chat-close'
  123. onClick = { this.props._onToggleChat }>X</div>
  124. </div>
  125. );
  126. }
  127. _renderPanelContent: (string) => React$Node | null;
  128. /**
  129. * Renders the contents of the chat panel, depending on the current
  130. * animation state provided by {@code Transition}.
  131. *
  132. * @param {string} state - The current display transition state of the
  133. * {@code Chat} component, as provided by {@code Transition}.
  134. * @private
  135. * @returns {ReactElement | null}
  136. */
  137. _renderPanelContent(state) {
  138. this._isExited = state === 'exited';
  139. const { _isOpen, _showNamePrompt } = this.props;
  140. const ComponentToRender = !_isOpen && state === 'exited'
  141. ? null
  142. : (
  143. <>
  144. { this._renderChatHeader() }
  145. { _showNamePrompt
  146. ? <DisplayNameForm /> : this._renderChat() }
  147. </>
  148. );
  149. let className = '';
  150. if (_isOpen) {
  151. className = 'slideInExt';
  152. } else if (this._isExited) {
  153. className = 'invisible';
  154. }
  155. return (
  156. <div
  157. className = { `sideToolbarContainer ${className}` }
  158. id = 'sideToolbarContainer'>
  159. { ComponentToRender }
  160. </div>
  161. );
  162. }
  163. /**
  164. * Scrolls the chat messages so the latest message is visible.
  165. *
  166. * @param {boolean} withAnimation - Whether or not to show a scrolling
  167. * animation.
  168. * @private
  169. * @returns {void}
  170. */
  171. _scrollMessageContainerToBottom(withAnimation) {
  172. if (this._messageContainerRef.current) {
  173. this._messageContainerRef.current.scrollToBottom(withAnimation);
  174. }
  175. }
  176. }
  177. export default translate(connect(_mapStateToProps, _mapDispatchToProps)(Chat));