Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Chat.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import clsx from 'clsx';
  2. import React from 'react';
  3. import { translate } from '../../../base/i18n';
  4. import { connect } from '../../../base/redux';
  5. import Tabs from '../../../base/ui/components/web/Tabs';
  6. import { PollsPane } from '../../../polls/components';
  7. import { toggleChat } from '../../actions.web';
  8. import { CHAT_TABS } from '../../constants';
  9. import AbstractChat, {
  10. type Props,
  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<Props> {
  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: Props) {
  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. id = 'chat-header'
  61. isPollsEnabled = { _isPollsEnabled }
  62. onCancel = { this._onToggleChat } />
  63. { _showNamePrompt
  64. ? <DisplayNameForm isPollsEnabled = { _isPollsEnabled } />
  65. : this._renderChat() }
  66. </div> : null
  67. );
  68. }
  69. _onChatTabKeyDown: (KeyboardEvent) => void;
  70. /**
  71. * Key press handler for the chat tab.
  72. *
  73. * @param {KeyboardEvent} event - The event.
  74. * @returns {void}
  75. */
  76. _onChatTabKeyDown(event) {
  77. if (event.key === 'Enter' || event.key === ' ') {
  78. event.preventDefault();
  79. event.stopPropagation();
  80. this._onToggleChatTab();
  81. }
  82. }
  83. _onEscClick: (KeyboardEvent) => void;
  84. /**
  85. * Click handler for the chat sidenav.
  86. *
  87. * @param {KeyboardEvent} event - Esc key click to close the popup.
  88. * @returns {void}
  89. */
  90. _onEscClick(event) {
  91. if (event.key === 'Escape' && this.props._isOpen) {
  92. event.preventDefault();
  93. event.stopPropagation();
  94. this._onToggleChat();
  95. }
  96. }
  97. _onPollsTabKeyDown: (KeyboardEvent) => void;
  98. /**
  99. * Key press handler for the polls tab.
  100. *
  101. * @param {KeyboardEvent} event - The event.
  102. * @returns {void}
  103. */
  104. _onPollsTabKeyDown(event) {
  105. if (event.key === 'Enter' || event.key === ' ') {
  106. event.preventDefault();
  107. event.stopPropagation();
  108. this._onTogglePollsTab();
  109. }
  110. }
  111. /**
  112. * Returns a React Element for showing chat messages and a form to send new
  113. * chat messages.
  114. *
  115. * @private
  116. * @returns {ReactElement}
  117. */
  118. _renderChat() {
  119. const { _isPollsEnabled, _isPollsTabFocused } = this.props;
  120. return (
  121. <>
  122. { _isPollsEnabled && this._renderTabs() }
  123. <div
  124. aria-labelledby = { CHAT_TABS.CHAT }
  125. className = { clsx(
  126. 'chat-panel',
  127. !_isPollsEnabled && 'chat-panel-no-tabs',
  128. _isPollsTabFocused && 'hide'
  129. ) }
  130. id = { `${CHAT_TABS.CHAT}-panel` }
  131. role = 'tabpanel'
  132. tabIndex = { 0 }>
  133. <MessageContainer
  134. messages = { this.props._messages } />
  135. <MessageRecipient />
  136. <ChatInput
  137. onSend = { this._onSendMessage } />
  138. </div>
  139. { _isPollsEnabled && (
  140. <>
  141. <div
  142. aria-labelledby = { CHAT_TABS.POLLS }
  143. className = { clsx('polls-panel', !_isPollsTabFocused && 'hide') }
  144. id = { `${CHAT_TABS.POLLS}-panel` }
  145. role = 'tabpanel'
  146. tabIndex = { 0 }>
  147. <PollsPane />
  148. </div>
  149. <KeyboardAvoider />
  150. </>
  151. )}
  152. </>
  153. );
  154. }
  155. /**
  156. * Returns a React Element showing the Chat and Polls tab.
  157. *
  158. * @private
  159. * @returns {ReactElement}
  160. */
  161. _renderTabs() {
  162. const { _isPollsEnabled, _isPollsTabFocused, _nbUnreadMessages, _nbUnreadPolls, t } = this.props;
  163. return (
  164. <Tabs
  165. accessibilityLabel = { t(_isPollsEnabled ? 'chat.titleWithPolls' : 'chat.title') }
  166. onChange = { this._onChangeTab }
  167. selected = { _isPollsTabFocused ? CHAT_TABS.POLLS : CHAT_TABS.CHAT }
  168. tabs = { [ {
  169. accessibilityLabel: t('chat.tabs.chat'),
  170. countBadge: _isPollsTabFocused && _nbUnreadMessages > 0 ? _nbUnreadMessages : undefined,
  171. id: CHAT_TABS.CHAT,
  172. controlsId: `${CHAT_TABS.CHAT}-panel`,
  173. label: t('chat.tabs.chat')
  174. }, {
  175. accessibilityLabel: t('chat.tabs.polls'),
  176. countBadge: !_isPollsTabFocused && _nbUnreadPolls > 0 ? _nbUnreadPolls : undefined,
  177. id: CHAT_TABS.POLLS,
  178. controlsId: `${CHAT_TABS.POLLS}-panel`,
  179. label: t('chat.tabs.polls')
  180. }
  181. ] } />
  182. );
  183. }
  184. _onSendMessage: (string) => void;
  185. _onToggleChat: () => void;
  186. /**
  187. * Toggles the chat window.
  188. *
  189. * @returns {Function}
  190. */
  191. _onToggleChat() {
  192. this.props.dispatch(toggleChat());
  193. }
  194. _onTogglePollsTab: () => void;
  195. _onToggleChatTab: () => void;
  196. _onChangeTab: (string) => void;
  197. /**
  198. * Change selected tab.
  199. *
  200. * @param {string} id - Id of the clicked tab.
  201. * @returns {void}
  202. */
  203. _onChangeTab(id) {
  204. id === CHAT_TABS.CHAT ? this._onToggleChatTab() : this._onTogglePollsTab();
  205. }
  206. }
  207. export default translate(connect(_mapStateToProps)(Chat));