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.

Toolbox.tsx 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. import React, { useCallback, useEffect, useRef } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { useDispatch, useSelector } from 'react-redux';
  4. import { makeStyles } from 'tss-react/mui';
  5. import { IReduxState } from '../../../app/types';
  6. import { isMobileBrowser } from '../../../base/environment/utils';
  7. import { getLocalParticipant, isLocalParticipantModerator } from '../../../base/participants/functions';
  8. import ContextMenu from '../../../base/ui/components/web/ContextMenu';
  9. import { isReactionsButtonEnabled, shouldDisplayReactionsButtons } from '../../../reactions/functions.web';
  10. import { isTranscribing } from '../../../transcribing/functions';
  11. import {
  12. setHangupMenuVisible,
  13. setOverflowMenuVisible,
  14. setToolbarHovered,
  15. setToolboxVisible
  16. } from '../../actions.web';
  17. import {
  18. getJwtDisabledButtons,
  19. getVisibleButtons,
  20. isButtonEnabled,
  21. isToolboxVisible
  22. } from '../../functions.web';
  23. import { useKeyboardShortcuts, useToolboxButtons } from '../../hooks.web';
  24. import { IToolboxButton } from '../../types';
  25. import HangupButton from '../HangupButton';
  26. import { EndConferenceButton } from './EndConferenceButton';
  27. import HangupMenuButton from './HangupMenuButton';
  28. import { LeaveConferenceButton } from './LeaveConferenceButton';
  29. import OverflowMenuButton from './OverflowMenuButton';
  30. import Separator from './Separator';
  31. /**
  32. * The type of the React {@code Component} props of {@link Toolbox}.
  33. */
  34. interface IProps {
  35. /**
  36. * Explicitly passed array with the buttons which this Toolbox should display.
  37. */
  38. toolbarButtons?: Array<string>;
  39. }
  40. const useStyles = makeStyles()(() => {
  41. return {
  42. hangupMenu: {
  43. position: 'relative',
  44. right: 'auto',
  45. display: 'flex',
  46. flexDirection: 'column',
  47. rowGap: '8px',
  48. margin: 0,
  49. padding: '16px',
  50. marginBottom: '8px'
  51. }
  52. };
  53. });
  54. /**
  55. * A component that renders the main toolbar.
  56. *
  57. * @param {IProps} props - The props of the component.
  58. * @returns {ReactElement}
  59. */
  60. export default function Toolbox({
  61. toolbarButtons
  62. }: IProps) {
  63. const { classes, cx } = useStyles();
  64. const { t } = useTranslation();
  65. const dispatch = useDispatch();
  66. const _toolboxRef = useRef<HTMLDivElement>(null);
  67. const conference = useSelector((state: IReduxState) => state['features/base/conference'].conference);
  68. const isNarrowLayout = useSelector((state: IReduxState) => state['features/base/responsive-ui'].isNarrowLayout);
  69. const clientWidth = useSelector((state: IReduxState) => state['features/base/responsive-ui'].clientWidth);
  70. const isModerator = useSelector(isLocalParticipantModerator);
  71. const customToolbarButtons = useSelector(
  72. (state: IReduxState) => state['features/base/config'].customToolbarButtons);
  73. const iAmRecorder = useSelector((state: IReduxState) => state['features/base/config'].iAmRecorder);
  74. const iAmSipGateway = useSelector((state: IReduxState) => state['features/base/config'].iAmSipGateway);
  75. const overflowDrawer = useSelector((state: IReduxState) => state['features/toolbox'].overflowDrawer);
  76. const shiftUp = useSelector((state: IReduxState) => state['features/toolbox'].shiftUp);
  77. const overflowMenuVisible = useSelector((state: IReduxState) => state['features/toolbox'].overflowMenuVisible);
  78. const hangupMenuVisible = useSelector((state: IReduxState) => state['features/toolbox'].hangupMenuVisible);
  79. const buttonsWithNotifyClick
  80. = useSelector((state: IReduxState) => state['features/toolbox'].buttonsWithNotifyClick);
  81. const reduxToolbarButtons = useSelector((state: IReduxState) => state['features/toolbox'].toolbarButtons);
  82. const toolbarButtonsToUse = toolbarButtons || reduxToolbarButtons;
  83. const chatOpen = useSelector((state: IReduxState) => state['features/chat'].isOpen);
  84. const isDialogVisible = useSelector((state: IReduxState) => Boolean(state['features/base/dialog'].component));
  85. const jwt = useSelector((state: IReduxState) => state['features/base/jwt'].jwt);
  86. const localParticipant = useSelector(getLocalParticipant);
  87. const transcribing = useSelector(isTranscribing);
  88. // Do not convert to selector, it returns new array and will cause re-rendering of toolbox on every action.
  89. const jwtDisabledButtons = getJwtDisabledButtons(transcribing, isModerator, jwt, localParticipant?.features);
  90. const reactionsButtonEnabled = useSelector(isReactionsButtonEnabled);
  91. const _shouldDisplayReactionsButtons = useSelector(shouldDisplayReactionsButtons);
  92. const toolbarVisible = useSelector(isToolboxVisible);
  93. const mainToolbarButtonsThresholds
  94. = useSelector((state: IReduxState) => state['features/toolbox'].mainToolbarButtonsThresholds);
  95. const allButtons = useToolboxButtons(customToolbarButtons);
  96. const isMobile = isMobileBrowser();
  97. const endConferenceSupported = Boolean(conference?.isEndConferenceSupported() && isModerator);
  98. useKeyboardShortcuts(toolbarButtonsToUse);
  99. useEffect(() => {
  100. if (!toolbarVisible) {
  101. if (document.activeElement instanceof HTMLElement
  102. && _toolboxRef.current?.contains(document.activeElement)) {
  103. document.activeElement.blur();
  104. }
  105. }
  106. }, [ toolbarVisible ]);
  107. /**
  108. * Sets the visibility of the hangup menu.
  109. *
  110. * @param {boolean} visible - Whether or not the hangup menu should be
  111. * displayed.
  112. * @private
  113. * @returns {void}
  114. */
  115. const onSetHangupVisible = useCallback((visible: boolean) => {
  116. dispatch(setHangupMenuVisible(visible));
  117. dispatch(setToolbarHovered(visible));
  118. }, [ dispatch ]);
  119. /**
  120. * Sets the visibility of the overflow menu.
  121. *
  122. * @param {boolean} visible - Whether or not the overflow menu should be
  123. * displayed.
  124. * @private
  125. * @returns {void}
  126. */
  127. const onSetOverflowVisible = useCallback((visible: boolean) => {
  128. dispatch(setOverflowMenuVisible(visible));
  129. dispatch(setToolbarHovered(visible));
  130. }, [ dispatch ]);
  131. useEffect(() => {
  132. // On mobile web we want to keep both toolbox and hang up menu visible
  133. // because they depend on each other.
  134. if (endConferenceSupported && isMobile) {
  135. hangupMenuVisible && dispatch(setToolboxVisible(true));
  136. } else if (hangupMenuVisible && !toolbarVisible) {
  137. onSetHangupVisible(false);
  138. dispatch(setToolbarHovered(false));
  139. }
  140. }, [ dispatch, hangupMenuVisible, toolbarVisible, onSetHangupVisible ]);
  141. useEffect(() => {
  142. if (overflowMenuVisible && isDialogVisible) {
  143. onSetOverflowVisible(false);
  144. dispatch(setToolbarHovered(false));
  145. }
  146. }, [ dispatch, overflowMenuVisible, isDialogVisible, onSetOverflowVisible ]);
  147. /**
  148. * Key handler for overflow/hangup menus.
  149. *
  150. * @param {KeyboardEvent} e - Esc key click to close the popup.
  151. * @returns {void}
  152. */
  153. const onEscKey = useCallback((e?: React.KeyboardEvent) => {
  154. if (e?.key === 'Escape') {
  155. e?.stopPropagation();
  156. hangupMenuVisible && dispatch(setHangupMenuVisible(false));
  157. overflowMenuVisible && dispatch(setOverflowMenuVisible(false));
  158. }
  159. }, [ dispatch, hangupMenuVisible, overflowMenuVisible ]);
  160. /**
  161. * Dispatches an action signaling the toolbar is not being hovered.
  162. *
  163. * @private
  164. * @returns {void}
  165. */
  166. const onMouseOut = useCallback(() => {
  167. !overflowMenuVisible && dispatch(setToolbarHovered(false));
  168. }, [ dispatch, overflowMenuVisible ]);
  169. /**
  170. * Dispatches an action signaling the toolbar is being hovered.
  171. *
  172. * @private
  173. * @returns {void}
  174. */
  175. const onMouseOver = useCallback(() => {
  176. dispatch(setToolbarHovered(true));
  177. }, [ dispatch ]);
  178. /**
  179. * Handle focus on the toolbar.
  180. *
  181. * @returns {void}
  182. */
  183. const handleFocus = useCallback(() => {
  184. dispatch(setToolboxVisible(true));
  185. }, [ dispatch ]);
  186. /**
  187. * Handle blur the toolbar..
  188. *
  189. * @returns {void}
  190. */
  191. const handleBlur = useCallback(() => {
  192. dispatch(setToolboxVisible(false));
  193. }, [ dispatch ]);
  194. if (iAmRecorder || iAmSipGateway) {
  195. return null;
  196. }
  197. const rootClassNames = `new-toolbox ${toolbarVisible ? 'visible' : ''} ${
  198. toolbarButtonsToUse.length ? '' : 'no-buttons'} ${chatOpen ? 'shift-right' : ''}`;
  199. const toolbarAccLabel = 'toolbar.accessibilityLabel.moreActionsMenu';
  200. const containerClassName = `toolbox-content${isMobile || isNarrowLayout ? ' toolbox-content-mobile' : ''}`;
  201. const { mainMenuButtons, overflowMenuButtons } = getVisibleButtons({
  202. allButtons,
  203. buttonsWithNotifyClick,
  204. toolbarButtons: toolbarButtonsToUse,
  205. clientWidth,
  206. jwtDisabledButtons,
  207. mainToolbarButtonsThresholds
  208. });
  209. const raiseHandInOverflowMenu = overflowMenuButtons.some(({ key }) => key === 'raisehand');
  210. const showReactionsInOverflowMenu = _shouldDisplayReactionsButtons
  211. && (
  212. (!reactionsButtonEnabled && (raiseHandInOverflowMenu || isNarrowLayout || isMobile))
  213. || overflowMenuButtons.some(({ key }) => key === 'reactions'));
  214. const showRaiseHandInReactionsMenu = showReactionsInOverflowMenu && raiseHandInOverflowMenu;
  215. return (
  216. <div
  217. className = { cx(rootClassNames, shiftUp && 'shift-up') }
  218. id = 'new-toolbox'>
  219. <div className = { containerClassName }>
  220. <div
  221. className = 'toolbox-content-wrapper'
  222. onBlur = { handleBlur }
  223. onFocus = { handleFocus }
  224. { ...(isMobile ? {} : {
  225. onMouseOut,
  226. onMouseOver
  227. }) }>
  228. <div
  229. className = 'toolbox-content-items'
  230. ref = { _toolboxRef }>
  231. {mainMenuButtons.map(({ Content, key, ...rest }) => Content !== Separator && (
  232. <Content
  233. { ...rest }
  234. buttonKey = { key }
  235. key = { key } />))}
  236. {Boolean(overflowMenuButtons.length) && (
  237. <OverflowMenuButton
  238. ariaControls = 'overflow-menu'
  239. buttons = { overflowMenuButtons.reduce<Array<IToolboxButton[]>>((acc, val) => {
  240. if (val.key === 'reactions' && showReactionsInOverflowMenu) {
  241. return acc;
  242. }
  243. if (val.key === 'raisehand' && showRaiseHandInReactionsMenu) {
  244. return acc;
  245. }
  246. if (acc.length) {
  247. const prev = acc[acc.length - 1];
  248. const group = prev[prev.length - 1].group;
  249. if (group === val.group) {
  250. prev.push(val);
  251. } else {
  252. acc.push([ val ]);
  253. }
  254. } else {
  255. acc.push([ val ]);
  256. }
  257. return acc;
  258. }, []) }
  259. isOpen = { overflowMenuVisible }
  260. key = 'overflow-menu'
  261. onToolboxEscKey = { onEscKey }
  262. onVisibilityChange = { onSetOverflowVisible }
  263. showRaiseHandInReactionsMenu = { showRaiseHandInReactionsMenu }
  264. showReactionsMenu = { showReactionsInOverflowMenu } />
  265. )}
  266. {isButtonEnabled('hangup', toolbarButtonsToUse) && (
  267. endConferenceSupported
  268. ? <HangupMenuButton
  269. ariaControls = 'hangup-menu'
  270. isOpen = { hangupMenuVisible }
  271. key = 'hangup-menu'
  272. notifyMode = { buttonsWithNotifyClick?.get('hangup-menu') }
  273. onVisibilityChange = { onSetHangupVisible }>
  274. <ContextMenu
  275. accessibilityLabel = { t(toolbarAccLabel) }
  276. className = { classes.hangupMenu }
  277. hidden = { false }
  278. inDrawer = { overflowDrawer }
  279. onKeyDown = { onEscKey }>
  280. <EndConferenceButton
  281. buttonKey = 'end-meeting'
  282. notifyMode = { buttonsWithNotifyClick?.get('end-meeting') } />
  283. <LeaveConferenceButton
  284. buttonKey = 'hangup'
  285. notifyMode = { buttonsWithNotifyClick?.get('hangup') } />
  286. </ContextMenu>
  287. </HangupMenuButton>
  288. : <HangupButton
  289. buttonKey = 'hangup'
  290. customClass = 'hangup-button'
  291. key = 'hangup-button'
  292. notifyMode = { buttonsWithNotifyClick.get('hangup') }
  293. visible = { isButtonEnabled('hangup', toolbarButtonsToUse) } />
  294. )}
  295. </div>
  296. </div>
  297. </div>
  298. </div>
  299. );
  300. }