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

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