Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Chat.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. if (_isPollsTabFocused) {
  121. return (
  122. <>
  123. { _isPollsEnabled && this._renderTabs() }
  124. <div
  125. aria-labelledby = { CHAT_TABS.POLLS }
  126. id = 'polls-panel'
  127. role = 'tabpanel'>
  128. <PollsPane />
  129. </div>
  130. <KeyboardAvoider />
  131. </>
  132. );
  133. }
  134. return (
  135. <>
  136. { _isPollsEnabled && this._renderTabs() }
  137. <div
  138. aria-labelledby = { CHAT_TABS.CHAT }
  139. className = { clsx('chat-panel', !_isPollsEnabled && 'chat-panel-no-tabs') }
  140. id = 'chat-panel'
  141. role = 'tabpanel'>
  142. <MessageContainer
  143. messages = { this.props._messages } />
  144. <MessageRecipient />
  145. <ChatInput
  146. onSend = { this._onSendMessage } />
  147. </div>
  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. label: t('chat.tabs.chat')
  169. }, {
  170. accessibilityLabel: t('chat.tabs.polls'),
  171. countBadge: !_isPollsTabFocused && _nbUnreadPolls > 0 ? _nbUnreadPolls : undefined,
  172. id: CHAT_TABS.POLLS,
  173. label: t('chat.tabs.polls')
  174. }
  175. ] } />
  176. );
  177. }
  178. _onSendMessage: (string) => void;
  179. _onToggleChat: () => void;
  180. /**
  181. * Toggles the chat window.
  182. *
  183. * @returns {Function}
  184. */
  185. _onToggleChat() {
  186. this.props.dispatch(toggleChat());
  187. }
  188. _onTogglePollsTab: () => void;
  189. _onToggleChatTab: () => void;
  190. _onChangeTab: (string) => void;
  191. /**
  192. * Change selected tab.
  193. *
  194. * @param {string} id - Id of the clicked tab.
  195. * @returns {void}
  196. */
  197. _onChangeTab(id) {
  198. id === CHAT_TABS.CHAT ? this._onToggleChatTab() : this._onTogglePollsTab();
  199. }
  200. }
  201. export default translate(connect(_mapStateToProps)(Chat));