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.web.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import Transition from 'react-transition-group/Transition';
  5. import { translate } from '../../base/i18n';
  6. import { getLocalParticipant } from '../../base/participants';
  7. import { toggleChat } from '../actions';
  8. import ChatInput from './ChatInput';
  9. import ChatMessage from './ChatMessage';
  10. import DisplayNameForm from './DisplayNameForm';
  11. /**
  12. * The type of the React {@code Component} props of {@link Chat}.
  13. */
  14. type Props = {
  15. /**
  16. * The JitsiConference instance to send messages to.
  17. */
  18. _conference: Object,
  19. /**
  20. * Whether or not chat is displayed.
  21. */
  22. _isOpen: Boolean,
  23. /**
  24. * The local participant's ID.
  25. */
  26. _localUserId: String,
  27. /**
  28. * All the chat messages in the conference.
  29. */
  30. _messages: Array<Object>,
  31. /**
  32. * Whether or not to block chat access with a nickname input form.
  33. */
  34. _showNamePrompt: boolean,
  35. /**
  36. * Invoked to change the chat panel status.
  37. */
  38. dispatch: Dispatch<*>
  39. };
  40. /**
  41. * The type of the React {@code Component} state of {@Chat}.
  42. */
  43. type State = {
  44. /**
  45. * User provided nickname when the input text is provided in the view.
  46. *
  47. * @type {String}
  48. */
  49. message: string
  50. };
  51. /**
  52. * React Component for holding the chat feature in a side panel that slides in
  53. * and out of view.
  54. *
  55. * @extends Component
  56. */
  57. class Chat extends Component<Props, State> {
  58. /**
  59. * Reference to the HTML element used for typing in a chat message.
  60. */
  61. _chatInput: ?HTMLElement;
  62. /**
  63. * Whether or not the {@code Chat} component is off-screen, having finished
  64. * its hiding animation.
  65. */
  66. _isExited: boolean;
  67. /**
  68. * Reference to the HTML element at the end of the list of displayed chat
  69. * messages. Used for scrolling to the end of the chat messages.
  70. */
  71. _messagesListEnd: ?HTMLElement;
  72. /**
  73. * Initializes a new {@code Chat} instance.
  74. *
  75. * @param {Object} props - The read-only properties with which the new
  76. * instance is to be initialized.
  77. */
  78. constructor(props: Props) {
  79. super(props);
  80. this._chatInput = null;
  81. this._isExited = true;
  82. this._messagesListEnd = null;
  83. // Bind event handlers so they are only bound once for every instance.
  84. this._onCloseClick = this._onCloseClick.bind(this);
  85. this._renderMessage = this._renderMessage.bind(this);
  86. this._renderPanelContent = this._renderPanelContent.bind(this);
  87. this._setChatInputRef = this._setChatInputRef.bind(this);
  88. this._setMessageListEndRef = this._setMessageListEndRef.bind(this);
  89. }
  90. /**
  91. * Implements React's {@link Component#componentDidMount()}.
  92. *
  93. * @inheritdoc
  94. */
  95. componentDidMount() {
  96. this._scrollMessagesToBottom();
  97. }
  98. /**
  99. * Updates chat input focus.
  100. *
  101. * @inheritdoc
  102. */
  103. componentDidUpdate(prevProps) {
  104. if (this.props._messages !== prevProps._messages) {
  105. this._scrollMessagesToBottom();
  106. }
  107. }
  108. /**
  109. * Implements React's {@link Component#render()}.
  110. *
  111. * @inheritdoc
  112. * @returns {ReactElement}
  113. */
  114. render() {
  115. return (
  116. <Transition
  117. in = { this.props._isOpen }
  118. timeout = { 500 }>
  119. { this._renderPanelContent }
  120. </Transition>
  121. );
  122. }
  123. _onCloseClick: () => void;
  124. /**
  125. * Callback invoked to hide {@code Chat}.
  126. *
  127. * @returns {void}
  128. */
  129. _onCloseClick() {
  130. this.props.dispatch(toggleChat());
  131. }
  132. /**
  133. * Returns a React Element for showing chat messages and a form to send new
  134. * chat messages.
  135. *
  136. * @private
  137. * @returns {ReactElement}
  138. */
  139. _renderChat() {
  140. const messages = this.props._messages.map(this._renderMessage);
  141. messages.push(<div
  142. key = 'end-marker'
  143. ref = { this._setMessageListEndRef } />);
  144. return (
  145. <div
  146. className = 'sideToolbarContainer__inner'
  147. id = 'chat_container'>
  148. <div id = 'chatconversation'>
  149. { messages }
  150. </div>
  151. <ChatInput getChatInputRef = { this._setChatInputRef } />
  152. </div>
  153. );
  154. }
  155. _renderMessage: (Object) => void;
  156. /**
  157. * Called by {@code _onSubmitMessage} to create the chat div.
  158. *
  159. * @param {string} message - The chat message to display.
  160. * @param {string} id - The chat message ID to use as a unique key.
  161. * @returns {Array<ReactElement>}
  162. */
  163. _renderMessage(message: Object, id: string) {
  164. return (
  165. <ChatMessage
  166. key = { id }
  167. message = { message } />
  168. );
  169. }
  170. _renderPanelContent: (string) => React$Node | null;
  171. /**
  172. * Renders the contents of the chat panel, depending on the current
  173. * animation state provided by {@code Transition}.
  174. *
  175. * @param {string} state - The current display transition state of the
  176. * {@code Chat} component, as provided by {@code Transition}.
  177. * @private
  178. * @returns {ReactElement | null}
  179. */
  180. _renderPanelContent(state) {
  181. this._isExited = state === 'exited';
  182. const { _isOpen, _showNamePrompt } = this.props;
  183. const ComponentToRender = !_isOpen && state === 'exited'
  184. ? null
  185. : (
  186. <div>
  187. <div
  188. className = 'chat-close'
  189. onClick = { this._onCloseClick }>X</div>
  190. { _showNamePrompt
  191. ? <DisplayNameForm /> : this._renderChat() }
  192. </div>
  193. );
  194. let className = '';
  195. if (_isOpen) {
  196. className = 'slideInExt';
  197. } else if (this._isExited) {
  198. className = 'invisible';
  199. }
  200. return (
  201. <div
  202. className = { className }
  203. id = 'sideToolbarContainer'>
  204. { ComponentToRender }
  205. </div>
  206. );
  207. }
  208. /**
  209. * Automatically scrolls the displayed chat messages down to the latest.
  210. *
  211. * @private
  212. * @returns {void}
  213. */
  214. _scrollMessagesToBottom() {
  215. if (this._messagesListEnd) {
  216. this._messagesListEnd.scrollIntoView({
  217. behavior: this._isExited ? 'auto' : 'smooth'
  218. });
  219. }
  220. }
  221. _setChatInputRef: (?HTMLElement) => void;
  222. /**
  223. * Sets a reference to the HTML text input element used for typing in chat
  224. * messages.
  225. *
  226. * @param {Object} chatInput - The input for typing chat messages.
  227. * @private
  228. * @returns {void}
  229. */
  230. _setChatInputRef(chatInput: ?HTMLElement) {
  231. this._chatInput = chatInput;
  232. }
  233. _setMessageListEndRef: (?HTMLElement) => void;
  234. /**
  235. * Sets a reference to the HTML element at the bottom of the message list.
  236. *
  237. * @param {Object} messageListEnd - The HTML element.
  238. * @private
  239. * @returns {void}
  240. */
  241. _setMessageListEndRef(messageListEnd: ?HTMLElement) {
  242. this._messagesListEnd = messageListEnd;
  243. }
  244. }
  245. /**
  246. * Maps (parts of) the redux state to {@link Chat} React {@code Component}
  247. * props.
  248. *
  249. * @param {Object} state - The redux store/state.
  250. * @private
  251. * @returns {{
  252. * _conference: Object,
  253. * _isOpen: boolean,
  254. * _messages: Array<Object>,
  255. * _showNamePrompt: boolean
  256. * }}
  257. */
  258. function _mapStateToProps(state) {
  259. const { isOpen, messages } = state['features/chat'];
  260. const localParticipant = getLocalParticipant(state);
  261. return {
  262. _conference: state['features/base/conference'].conference,
  263. _isOpen: isOpen,
  264. _messages: messages,
  265. _showNamePrompt: !localParticipant.name
  266. };
  267. }
  268. export default translate(connect(_mapStateToProps)(Chat));