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.tsx 6.9KB

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