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 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 Tabs from '../../../base/ui/components/web/Tabs';
  7. import { PollsPane } from '../../../polls/components';
  8. import { toggleChat } from '../../actions.web';
  9. import { CHAT_TABS } from '../../constants';
  10. import AbstractChat, {
  11. type Props,
  12. _mapStateToProps
  13. } from '../AbstractChat';
  14. import ChatHeader from './ChatHeader';
  15. import ChatInput from './ChatInput';
  16. import DisplayNameForm from './DisplayNameForm';
  17. import KeyboardAvoider from './KeyboardAvoider';
  18. import MessageContainer from './MessageContainer';
  19. import MessageRecipient from './MessageRecipient';
  20. /**
  21. * React Component for holding the chat feature in a side panel that slides in
  22. * and out of view.
  23. */
  24. class Chat extends AbstractChat<Props> {
  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._messageContainerRef = React.createRef();
  39. // Bind event handlers so they are only bound once for every instance.
  40. this._onChatTabKeyDown = this._onChatTabKeyDown.bind(this);
  41. this._onEscClick = this._onEscClick.bind(this);
  42. this._onPollsTabKeyDown = this._onPollsTabKeyDown.bind(this);
  43. this._onToggleChat = this._onToggleChat.bind(this);
  44. this._onChangeTab = this._onChangeTab.bind(this);
  45. }
  46. /**
  47. * Implements React's {@link Component#render()}.
  48. *
  49. * @inheritdoc
  50. * @returns {ReactElement}
  51. */
  52. render() {
  53. const { _isOpen, _isPollsEnabled, _showNamePrompt } = this.props;
  54. return (
  55. _isOpen ? <div
  56. className = 'sideToolbarContainer'
  57. id = 'sideToolbarContainer'
  58. onKeyDown = { this._onEscClick } >
  59. <ChatHeader
  60. className = 'chat-header'
  61. id = 'chat-header'
  62. isPollsEnabled = { _isPollsEnabled }
  63. onCancel = { this._onToggleChat } />
  64. { _showNamePrompt
  65. ? <DisplayNameForm isPollsEnabled = { _isPollsEnabled } />
  66. : this._renderChat() }
  67. </div> : null
  68. );
  69. }
  70. _onChatTabKeyDown: (KeyboardEvent) => void;
  71. /**
  72. * Key press handler for the chat tab.
  73. *
  74. * @param {KeyboardEvent} event - The event.
  75. * @returns {void}
  76. */
  77. _onChatTabKeyDown(event) {
  78. if (event.key === 'Enter' || event.key === ' ') {
  79. event.preventDefault();
  80. event.stopPropagation();
  81. this._onToggleChatTab();
  82. }
  83. }
  84. _onEscClick: (KeyboardEvent) => void;
  85. /**
  86. * Click handler for the chat sidenav.
  87. *
  88. * @param {KeyboardEvent} event - Esc key click to close the popup.
  89. * @returns {void}
  90. */
  91. _onEscClick(event) {
  92. if (event.key === 'Escape' && this.props._isOpen) {
  93. event.preventDefault();
  94. event.stopPropagation();
  95. this._onToggleChat();
  96. }
  97. }
  98. _onPollsTabKeyDown: (KeyboardEvent) => void;
  99. /**
  100. * Key press handler for the polls tab.
  101. *
  102. * @param {KeyboardEvent} event - The event.
  103. * @returns {void}
  104. */
  105. _onPollsTabKeyDown(event) {
  106. if (event.key === 'Enter' || event.key === ' ') {
  107. event.preventDefault();
  108. event.stopPropagation();
  109. this._onTogglePollsTab();
  110. }
  111. }
  112. /**
  113. * Returns a React Element for showing chat messages and a form to send new
  114. * chat messages.
  115. *
  116. * @private
  117. * @returns {ReactElement}
  118. */
  119. _renderChat() {
  120. const { _isPollsEnabled, _isPollsTabFocused } = this.props;
  121. if (_isPollsTabFocused) {
  122. return (
  123. <>
  124. { _isPollsEnabled && this._renderTabs() }
  125. <div
  126. aria-labelledby = { CHAT_TABS.POLLS }
  127. id = 'polls-panel'
  128. role = 'tabpanel'>
  129. <PollsPane />
  130. </div>
  131. <KeyboardAvoider />
  132. </>
  133. );
  134. }
  135. return (
  136. <>
  137. { _isPollsEnabled && this._renderTabs() }
  138. <div
  139. aria-labelledby = { CHAT_TABS.CHAT }
  140. className = { clsx('chat-panel', !_isPollsEnabled && 'chat-panel-no-tabs') }
  141. id = 'chat-panel'
  142. role = 'tabpanel'>
  143. <MessageContainer
  144. messages = { this.props._messages } />
  145. <MessageRecipient />
  146. <ChatInput
  147. onSend = { this._onSendMessage } />
  148. </div>
  149. </>
  150. );
  151. }
  152. /**
  153. * Returns a React Element showing the Chat and Polls tab.
  154. *
  155. * @private
  156. * @returns {ReactElement}
  157. */
  158. _renderTabs() {
  159. const { _isPollsEnabled, _isPollsTabFocused, _nbUnreadMessages, _nbUnreadPolls, t } = this.props;
  160. return (
  161. <Tabs
  162. accessibilityLabel = { t(_isPollsEnabled ? 'chat.titleWithPolls' : 'chat.title') }
  163. onChange = { this._onChangeTab }
  164. selected = { _isPollsTabFocused ? CHAT_TABS.POLLS : CHAT_TABS.CHAT }
  165. tabs = { [ {
  166. accessibilityLabel: t('chat.tabs.chat'),
  167. countBadge: _isPollsTabFocused && _nbUnreadMessages > 0 ? _nbUnreadMessages : undefined,
  168. id: CHAT_TABS.CHAT,
  169. label: t('chat.tabs.chat')
  170. }, {
  171. accessibilityLabel: t('chat.tabs.polls'),
  172. countBadge: !_isPollsTabFocused && _nbUnreadPolls > 0 ? _nbUnreadPolls : undefined,
  173. id: CHAT_TABS.POLLS,
  174. label: t('chat.tabs.polls')
  175. }
  176. ] } />
  177. );
  178. }
  179. _onSendMessage: (string) => void;
  180. _onToggleChat: () => void;
  181. /**
  182. * Toggles the chat window.
  183. *
  184. * @returns {Function}
  185. */
  186. _onToggleChat() {
  187. this.props.dispatch(toggleChat());
  188. }
  189. _onTogglePollsTab: () => void;
  190. _onToggleChatTab: () => void;
  191. _onChangeTab: (string) => void;
  192. /**
  193. * Change selected tab.
  194. *
  195. * @param {string} id - Id of the clicked tab.
  196. * @returns {void}
  197. */
  198. _onChangeTab(id) {
  199. id === CHAT_TABS.CHAT ? this._onToggleChatTab() : this._onTogglePollsTab();
  200. }
  201. }
  202. export default translate(connect(_mapStateToProps)(Chat));