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

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