Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // @flow
  2. import clsx from 'clsx';
  3. import React from 'react';
  4. import { translate } from '../../../base/i18n';
  5. import { connect } from '../../../base/redux';
  6. import { PollsPane } from '../../../polls/components';
  7. import { toggleChat } from '../../actions.web';
  8. import AbstractChat, {
  9. _mapStateToProps,
  10. type Props
  11. } from '../AbstractChat';
  12. import ChatHeader from './ChatHeader';
  13. import ChatInput from './ChatInput';
  14. import DisplayNameForm from './DisplayNameForm';
  15. import KeyboardAvoider from './KeyboardAvoider';
  16. import MessageContainer from './MessageContainer';
  17. import MessageRecipient from './MessageRecipient';
  18. /**
  19. * React Component for holding the chat feature in a side panel that slides in
  20. * and out of view.
  21. */
  22. class Chat extends AbstractChat<Props> {
  23. /**
  24. * Reference to the React Component for displaying chat messages. Used for
  25. * scrolling to the end of the chat messages.
  26. */
  27. _messageContainerRef: Object;
  28. /**
  29. * Initializes a new {@code Chat} instance.
  30. *
  31. * @param {Object} props - The read-only properties with which the new
  32. * instance is to be initialized.
  33. */
  34. constructor(props: Props) {
  35. super(props);
  36. this._messageContainerRef = React.createRef();
  37. // Bind event handlers so they are only bound once for every instance.
  38. this._onChatTabKeyDown = this._onChatTabKeyDown.bind(this);
  39. this._onChatInputResize = this._onChatInputResize.bind(this);
  40. this._onEscClick = this._onEscClick.bind(this);
  41. this._onPollsTabKeyDown = this._onPollsTabKeyDown.bind(this);
  42. this._onToggleChat = this._onToggleChat.bind(this);
  43. }
  44. /**
  45. * Implements {@code Component#componentDidMount}.
  46. *
  47. * @inheritdoc
  48. */
  49. componentDidMount() {
  50. this._scrollMessageContainerToBottom(true);
  51. }
  52. /**
  53. * Implements {@code Component#componentDidUpdate}.
  54. *
  55. * @inheritdoc
  56. */
  57. componentDidUpdate(prevProps) {
  58. if (this.props._messages !== prevProps._messages) {
  59. this._scrollMessageContainerToBottom(true);
  60. } else if (this.props._isOpen && !prevProps._isOpen) {
  61. this._scrollMessageContainerToBottom(false);
  62. }
  63. }
  64. /**
  65. * Implements React's {@link Component#render()}.
  66. *
  67. * @inheritdoc
  68. * @returns {ReactElement}
  69. */
  70. render() {
  71. const { _isOpen, _isPollsEnabled, _showNamePrompt } = this.props;
  72. return (
  73. _isOpen ? <div
  74. className = 'sideToolbarContainer'
  75. id = 'sideToolbarContainer'
  76. onKeyDown = { this._onEscClick } >
  77. <ChatHeader
  78. className = 'chat-header'
  79. id = 'chat-header'
  80. isPollsEnabled = { _isPollsEnabled }
  81. onCancel = { this._onToggleChat } />
  82. { _showNamePrompt
  83. ? <DisplayNameForm isPollsEnabled = { _isPollsEnabled } />
  84. : this._renderChat() }
  85. </div> : null
  86. );
  87. }
  88. _onChatInputResize: () => void;
  89. /**
  90. * Callback invoked when {@code ChatInput} changes height. Preserves
  91. * displaying the latest message if it is scrolled to.
  92. *
  93. * @private
  94. * @returns {void}
  95. */
  96. _onChatInputResize() {
  97. this._messageContainerRef.current.maybeUpdateBottomScroll();
  98. }
  99. _onChatTabKeyDown: (KeyboardEvent) => void;
  100. /**
  101. * Key press handler for the chat tab.
  102. *
  103. * @param {KeyboardEvent} event - The event.
  104. * @returns {void}
  105. */
  106. _onChatTabKeyDown(event) {
  107. if (event.key === 'Enter' || event.key === ' ') {
  108. event.preventDefault();
  109. event.stopPropagation();
  110. this._onToggleChatTab();
  111. }
  112. }
  113. _onEscClick: (KeyboardEvent) => void;
  114. /**
  115. * Click handler for the chat sidenav.
  116. *
  117. * @param {KeyboardEvent} event - Esc key click to close the popup.
  118. * @returns {void}
  119. */
  120. _onEscClick(event) {
  121. if (event.key === 'Escape' && this.props._isOpen) {
  122. event.preventDefault();
  123. event.stopPropagation();
  124. this._onToggleChat();
  125. }
  126. }
  127. _onPollsTabKeyDown: (KeyboardEvent) => void;
  128. /**
  129. * Key press handler for the polls tab.
  130. *
  131. * @param {KeyboardEvent} event - The event.
  132. * @returns {void}
  133. */
  134. _onPollsTabKeyDown(event) {
  135. if (event.key === 'Enter' || event.key === ' ') {
  136. event.preventDefault();
  137. event.stopPropagation();
  138. this._onTogglePollsTab();
  139. }
  140. }
  141. /**
  142. * Returns a React Element for showing chat messages and a form to send new
  143. * chat messages.
  144. *
  145. * @private
  146. * @returns {ReactElement}
  147. */
  148. _renderChat() {
  149. const { _isPollsEnabled, _isPollsTabFocused } = this.props;
  150. if (_isPollsTabFocused) {
  151. return (
  152. <>
  153. {_isPollsEnabled && this._renderTabs()}
  154. <div
  155. aria-labelledby = 'polls-tab'
  156. id = 'polls-panel'
  157. role = 'tabpanel'>
  158. <PollsPane />
  159. </div>
  160. <KeyboardAvoider />
  161. </>
  162. );
  163. }
  164. return (
  165. <>
  166. {_isPollsEnabled && this._renderTabs()}
  167. <div
  168. aria-labelledby = 'chat-tab'
  169. className = { clsx('chat-panel', !_isPollsEnabled && 'chat-panel-no-tabs') }
  170. id = 'chat-panel'
  171. role = 'tabpanel'>
  172. <MessageContainer
  173. messages = { this.props._messages }
  174. ref = { this._messageContainerRef } />
  175. <MessageRecipient />
  176. <ChatInput
  177. onResize = { this._onChatInputResize }
  178. onSend = { this._onSendMessage } />
  179. <KeyboardAvoider />
  180. </div>
  181. </>
  182. );
  183. }
  184. /**
  185. * Returns a React Element showing the Chat and Polls tab.
  186. *
  187. * @private
  188. * @returns {ReactElement}
  189. */
  190. _renderTabs() {
  191. const { _isPollsEnabled, _isPollsTabFocused, _nbUnreadMessages, _nbUnreadPolls, t } = this.props;
  192. return (
  193. <div
  194. aria-label = { t(_isPollsEnabled ? 'chat.titleWithPolls' : 'chat.title') }
  195. className = { 'chat-tabs-container' }
  196. role = 'tablist'>
  197. <div
  198. aria-controls = 'chat-panel'
  199. aria-label = { t('chat.tabs.chat') }
  200. aria-selected = { !_isPollsTabFocused }
  201. className = { `chat-tab ${
  202. _isPollsTabFocused ? '' : 'chat-tab-focus'
  203. }` }
  204. id = 'chat-tab'
  205. onClick = { this._onToggleChatTab }
  206. onKeyDown = { this._onChatTabKeyDown }
  207. role = 'tab'
  208. tabIndex = '0'>
  209. <span
  210. className = { 'chat-tab-title' }>
  211. {t('chat.tabs.chat')}
  212. </span>
  213. {this.props._isPollsTabFocused
  214. && _nbUnreadMessages > 0 && (
  215. <span className = { 'chat-tab-badge' }>
  216. {_nbUnreadMessages}
  217. </span>
  218. )}
  219. </div>
  220. <div
  221. aria-controls = 'polls-panel'
  222. aria-label = { t('chat.tabs.polls') }
  223. aria-selected = { _isPollsTabFocused }
  224. className = { `chat-tab ${
  225. _isPollsTabFocused ? 'chat-tab-focus' : ''
  226. }` }
  227. id = 'polls-tab'
  228. onClick = { this._onTogglePollsTab }
  229. onKeyDown = { this._onPollsTabKeyDown }
  230. role = 'tab'
  231. tabIndex = '0'>
  232. <span className = { 'chat-tab-title' }>
  233. {t('chat.tabs.polls')}
  234. </span>
  235. {!_isPollsTabFocused
  236. && this.props._nbUnreadPolls > 0 && (
  237. <span className = { 'chat-tab-badge' }>
  238. {_nbUnreadPolls}
  239. </span>
  240. )}
  241. </div>
  242. </div>
  243. );
  244. }
  245. /**
  246. * Scrolls the chat messages so the latest message is visible.
  247. *
  248. * @param {boolean} withAnimation - Whether or not to show a scrolling
  249. * animation.
  250. * @private
  251. * @returns {void}
  252. */
  253. _scrollMessageContainerToBottom(withAnimation) {
  254. if (this._messageContainerRef.current) {
  255. this._messageContainerRef.current.scrollToBottom(withAnimation);
  256. }
  257. }
  258. _onSendMessage: (string) => void;
  259. _onToggleChat: () => void;
  260. /**
  261. * Toggles the chat window.
  262. *
  263. * @returns {Function}
  264. */
  265. _onToggleChat() {
  266. this.props.dispatch(toggleChat());
  267. }
  268. _onTogglePollsTab: () => void;
  269. _onToggleChatTab: () => void;
  270. }
  271. export default translate(connect(_mapStateToProps)(Chat));