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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // @flow
  2. import React from 'react';
  3. import { translate } from '../../../base/i18n';
  4. import { connect } from '../../../base/redux';
  5. import AbstractChat, {
  6. _mapDispatchToProps,
  7. _mapStateToProps,
  8. type Props
  9. } from '../AbstractChat';
  10. import ChatDialog from './ChatDialog';
  11. import Header from './ChatDialogHeader';
  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. <>
  75. { this._renderPanelContent() }
  76. </>
  77. );
  78. }
  79. _onChatInputResize: () => void;
  80. /**
  81. * Callback invoked when {@code ChatInput} changes height. Preserves
  82. * displaying the latest message if it is scrolled to.
  83. *
  84. * @private
  85. * @returns {void}
  86. */
  87. _onChatInputResize() {
  88. this._messageContainerRef.current.maybeUpdateBottomScroll();
  89. }
  90. /**
  91. * Returns a React Element for showing chat messages and a form to send new
  92. * chat messages.
  93. *
  94. * @private
  95. * @returns {ReactElement}
  96. */
  97. _renderChat() {
  98. return (
  99. <>
  100. <MessageContainer
  101. messages = { this.props._messages }
  102. ref = { this._messageContainerRef } />
  103. <MessageRecipient />
  104. <ChatInput
  105. onResize = { this._onChatInputResize }
  106. onSend = { this.props._onSendMessage } />
  107. </>
  108. );
  109. }
  110. /**
  111. * Instantiates a React Element to display at the top of {@code Chat} to
  112. * close {@code Chat}.
  113. *
  114. * @private
  115. * @returns {ReactElement}
  116. */
  117. _renderChatHeader() {
  118. return (
  119. <Header
  120. className = 'chat-header'
  121. onCancel = { this.props._onToggleChat } />
  122. );
  123. }
  124. _renderPanelContent: () => React$Node | null;
  125. /**
  126. * Renders the contents of the chat panel.
  127. *
  128. * @private
  129. * @returns {ReactElement | null}
  130. */
  131. _renderPanelContent() {
  132. const { _isModal, _isOpen, _showNamePrompt } = this.props;
  133. let ComponentToRender = null;
  134. if (_isOpen) {
  135. if (_isModal) {
  136. ComponentToRender = (
  137. <ChatDialog>
  138. { _showNamePrompt ? <DisplayNameForm /> : this._renderChat() }
  139. </ChatDialog>
  140. );
  141. } else {
  142. ComponentToRender = (
  143. <>
  144. { this._renderChatHeader() }
  145. { _showNamePrompt ? <DisplayNameForm /> : this._renderChat() }
  146. </>
  147. );
  148. }
  149. }
  150. let className = '';
  151. if (_isOpen) {
  152. className = 'slideInExt';
  153. } else if (this._isExited) {
  154. className = 'invisible';
  155. }
  156. return (
  157. <div
  158. className = { `sideToolbarContainer ${className}` }
  159. id = 'sideToolbarContainer'>
  160. { ComponentToRender }
  161. </div>
  162. );
  163. }
  164. /**
  165. * Scrolls the chat messages so the latest message is visible.
  166. *
  167. * @param {boolean} withAnimation - Whether or not to show a scrolling
  168. * animation.
  169. * @private
  170. * @returns {void}
  171. */
  172. _scrollMessageContainerToBottom(withAnimation) {
  173. if (this._messageContainerRef.current) {
  174. this._messageContainerRef.current.scrollToBottom(withAnimation);
  175. }
  176. }
  177. }
  178. export default translate(connect(_mapStateToProps, _mapDispatchToProps)(Chat));