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.

Toolbox.tsx 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. import React, { useCallback, useEffect, useRef } from 'react';
  2. import { WithTranslation } from 'react-i18next';
  3. import { connect } from 'react-redux';
  4. import { makeStyles } from 'tss-react/mui';
  5. import { IReduxState, IStore } from '../../../app/types';
  6. import { isMobileBrowser } from '../../../base/environment/utils';
  7. import { translate } from '../../../base/i18n/functions';
  8. import { isLocalParticipantModerator } from '../../../base/participants/functions';
  9. import ContextMenu from '../../../base/ui/components/web/ContextMenu';
  10. import { isReactionsButtonEnabled, shouldDisplayReactionsButtons } from '../../../reactions/functions.web';
  11. import {
  12. setHangupMenuVisible,
  13. setOverflowMenuVisible,
  14. setToolbarHovered,
  15. showToolbox
  16. } from '../../actions.web';
  17. import { MAIN_TOOLBAR_BUTTONS_PRIORITY } from '../../constants';
  18. import {
  19. getAllToolboxButtons,
  20. getJwtDisabledButtons,
  21. isButtonEnabled,
  22. isToolboxVisible
  23. } from '../../functions.web';
  24. import { useKeyboardShortcuts } from '../../hooks.web';
  25. import { IToolboxButton, NOTIFY_CLICK_MODE, ToolbarButton } from '../../types';
  26. import HangupButton from '../HangupButton';
  27. import { EndConferenceButton } from './EndConferenceButton';
  28. import HangupMenuButton from './HangupMenuButton';
  29. import { LeaveConferenceButton } from './LeaveConferenceButton';
  30. import OverflowMenuButton from './OverflowMenuButton';
  31. import Separator from './Separator';
  32. /**
  33. * The type of the React {@code Component} props of {@link Toolbox}.
  34. */
  35. interface IProps extends WithTranslation {
  36. /**
  37. * Toolbar buttons which have their click exposed through the API.
  38. */
  39. _buttonsWithNotifyClick: Map<string, NOTIFY_CLICK_MODE>;
  40. /**
  41. * Whether or not the chat feature is currently displayed.
  42. */
  43. _chatOpen: boolean;
  44. /**
  45. * The width of the client.
  46. */
  47. _clientWidth: number;
  48. /**
  49. * Custom Toolbar buttons.
  50. */
  51. _customToolbarButtons?: Array<{ backgroundColor?: string; icon: string; id: string; text: string; }>;
  52. /**
  53. * Whether or not a dialog is displayed.
  54. */
  55. _dialog: boolean;
  56. /**
  57. * Whether or not the toolbox is disabled. It is for recorders.
  58. */
  59. _disabled: boolean;
  60. /**
  61. * Whether the end conference feature is supported.
  62. */
  63. _endConferenceSupported: boolean;
  64. /**
  65. * Whether the hangup menu is visible.
  66. */
  67. _hangupMenuVisible: boolean;
  68. /**
  69. * Whether or not the app is running in mobile browser.
  70. */
  71. _isMobile: boolean;
  72. /**
  73. * Whether we are in narrow layout mode.
  74. */
  75. _isNarrowLayout: boolean;
  76. /**
  77. * The array of toolbar buttons disabled through jwt features.
  78. */
  79. _jwtDisabledButtons: string[];
  80. /**
  81. * The main toolbar buttons thresholds used to determine the visible buttons depending on the current screen size.
  82. */
  83. _mainToolbarButtonsThresholds: Array<{
  84. order: Array<ToolbarButton | string>;
  85. width: number;
  86. }>;
  87. /**
  88. * Whether or not the overflow menu is displayed in a drawer drawer.
  89. */
  90. _overflowDrawer: boolean;
  91. /**
  92. * Whether or not the overflow menu is visible.
  93. */
  94. _overflowMenuVisible: boolean;
  95. /**
  96. * Whether or not to display reactions in separate button.
  97. */
  98. _reactionsButtonEnabled: boolean;
  99. /**
  100. * Whether the toolbox should be shifted up or not.
  101. */
  102. _shiftUp: boolean;
  103. /**
  104. * Whether any reactions buttons should be displayed or not.
  105. */
  106. _shouldDisplayReactionsButtons: boolean;
  107. /**
  108. * The enabled buttons.
  109. */
  110. _toolbarButtons: Array<string>;
  111. /**
  112. * Flag showing whether toolbar is visible.
  113. */
  114. _visible: boolean;
  115. /**
  116. * Invoked to active other features of the app.
  117. */
  118. dispatch: IStore['dispatch'];
  119. /**
  120. * Explicitly passed array with the buttons which this Toolbox should display.
  121. */
  122. toolbarButtons: Array<string>;
  123. }
  124. const useStyles = makeStyles()(() => {
  125. return {
  126. contextMenu: {
  127. position: 'relative',
  128. right: 'auto',
  129. margin: 0,
  130. marginBottom: '8px',
  131. maxHeight: 'calc(100dvh - 100px)',
  132. minWidth: '240px'
  133. },
  134. hangupMenu: {
  135. position: 'relative',
  136. right: 'auto',
  137. display: 'flex',
  138. flexDirection: 'column',
  139. rowGap: '8px',
  140. margin: 0,
  141. padding: '16px',
  142. marginBottom: '4px'
  143. }
  144. };
  145. });
  146. const Toolbox = ({
  147. _buttonsWithNotifyClick,
  148. _chatOpen,
  149. _clientWidth,
  150. _customToolbarButtons,
  151. _dialog,
  152. _disabled,
  153. _endConferenceSupported,
  154. _hangupMenuVisible,
  155. _isMobile,
  156. _isNarrowLayout,
  157. _jwtDisabledButtons,
  158. _mainToolbarButtonsThresholds,
  159. _overflowDrawer,
  160. _overflowMenuVisible,
  161. _reactionsButtonEnabled,
  162. _shiftUp,
  163. _shouldDisplayReactionsButtons,
  164. _toolbarButtons,
  165. _visible,
  166. dispatch,
  167. t,
  168. toolbarButtons
  169. }: IProps) => {
  170. const { classes, cx } = useStyles();
  171. const _toolboxRef = useRef<HTMLDivElement>(null);
  172. useKeyboardShortcuts(toolbarButtons);
  173. useEffect(() => {
  174. if (!_visible) {
  175. if (document.activeElement instanceof HTMLElement
  176. && _toolboxRef.current?.contains(document.activeElement)) {
  177. document.activeElement.blur();
  178. }
  179. }
  180. }, [ _visible ]);
  181. /**
  182. * Sets the visibility of the hangup menu.
  183. *
  184. * @param {boolean} visible - Whether or not the hangup menu should be
  185. * displayed.
  186. * @private
  187. * @returns {void}
  188. */
  189. const onSetHangupVisible = useCallback((visible: boolean) => {
  190. dispatch(setHangupMenuVisible(visible));
  191. dispatch(setToolbarHovered(visible));
  192. }, []);
  193. /**
  194. * Sets the visibility of the overflow menu.
  195. *
  196. * @param {boolean} visible - Whether or not the overflow menu should be
  197. * displayed.
  198. * @private
  199. * @returns {void}
  200. */
  201. const onSetOverflowVisible = useCallback((visible: boolean) => {
  202. dispatch(setOverflowMenuVisible(visible));
  203. dispatch(setToolbarHovered(visible));
  204. }, []);
  205. useEffect(() => {
  206. if (_hangupMenuVisible && !_visible) {
  207. onSetHangupVisible(false);
  208. dispatch(setToolbarHovered(false));
  209. }
  210. }, [ _hangupMenuVisible, _visible ]);
  211. useEffect(() => {
  212. if (_overflowMenuVisible && _dialog) {
  213. onSetOverflowVisible(false);
  214. dispatch(setToolbarHovered(false));
  215. }
  216. }, [ _overflowMenuVisible, _dialog ]);
  217. /**
  218. * Key handler for overflow/hangup menus.
  219. *
  220. * @param {KeyboardEvent} e - Esc key click to close the popup.
  221. * @returns {void}
  222. */
  223. const onEscKey = useCallback((e?: React.KeyboardEvent) => {
  224. if (e?.key === 'Escape') {
  225. e?.stopPropagation();
  226. _hangupMenuVisible && dispatch(setHangupMenuVisible(false));
  227. _overflowMenuVisible && dispatch(setOverflowMenuVisible(false));
  228. }
  229. }, [ _hangupMenuVisible, _overflowMenuVisible ]);
  230. /**
  231. * Sets the notify click mode for the buttons.
  232. *
  233. * @param {Object} buttons - The list of toolbar buttons.
  234. * @returns {void}
  235. */
  236. function setButtonsNotifyClickMode(buttons: Object) {
  237. if (typeof APP === 'undefined' || (_buttonsWithNotifyClick?.size ?? 0) <= 0) {
  238. return;
  239. }
  240. Object.values(buttons).forEach((button: any) => {
  241. if (typeof button === 'object') {
  242. button.notifyMode = _buttonsWithNotifyClick.get(button.key);
  243. }
  244. });
  245. }
  246. /**
  247. * Returns all buttons that need to be rendered.
  248. *
  249. * @param {Object} state - The redux state.
  250. * @returns {Object} The visible buttons arrays .
  251. */
  252. function getVisibleButtons() {
  253. const buttons = getAllToolboxButtons(_customToolbarButtons);
  254. const filteredButtons = Object.keys(buttons).filter(key =>
  255. typeof key !== 'undefined' // filter invalid buttons that may be comming from config.mainToolbarButtons
  256. // override
  257. && !_jwtDisabledButtons.includes(key)
  258. && isButtonEnabled(key, _toolbarButtons));
  259. setButtonsNotifyClickMode(buttons);
  260. const { order } = _mainToolbarButtonsThresholds.find(({ width }) => _clientWidth > width)
  261. || _mainToolbarButtonsThresholds[_mainToolbarButtonsThresholds.length - 1];
  262. const mainToolbarButtonKeysOrder = [
  263. ...order.filter(key => filteredButtons.includes(key)),
  264. ...MAIN_TOOLBAR_BUTTONS_PRIORITY.filter(key => !order.includes(key) && filteredButtons.includes(key)),
  265. ...filteredButtons.filter(key => !order.includes(key) && !MAIN_TOOLBAR_BUTTONS_PRIORITY.includes(key))
  266. ];
  267. const mainButtonsKeys = mainToolbarButtonKeysOrder.slice(0, order.length);
  268. const overflowMenuButtons = filteredButtons.reduce((acc, key) => {
  269. if (!mainButtonsKeys.includes(key)) {
  270. acc.push(buttons[key]);
  271. }
  272. return acc;
  273. }, [] as IToolboxButton[]);
  274. // if we have 1 button in the overflow menu it is better to directly display it in the main toolbar by replacing
  275. // the "More" menu button with it.
  276. if (overflowMenuButtons.length === 1) {
  277. const button = overflowMenuButtons.shift()?.key;
  278. button && mainButtonsKeys.push(button);
  279. }
  280. return {
  281. mainMenuButtons: mainButtonsKeys.map(key => buttons[key]),
  282. overflowMenuButtons
  283. };
  284. }
  285. /**
  286. * Dispatches an action signaling the toolbar is not being hovered.
  287. *
  288. * @private
  289. * @returns {void}
  290. */
  291. function onMouseOut() {
  292. !_overflowMenuVisible && dispatch(setToolbarHovered(false));
  293. }
  294. /**
  295. * Dispatches an action signaling the toolbar is being hovered.
  296. *
  297. * @private
  298. * @returns {void}
  299. */
  300. function onMouseOver() {
  301. dispatch(setToolbarHovered(true));
  302. }
  303. /**
  304. * Toggle the toolbar visibility when tabbing into it.
  305. *
  306. * @returns {void}
  307. */
  308. const onTabIn = useCallback(() => {
  309. if (!_visible) {
  310. dispatch(showToolbox());
  311. }
  312. }, [ _visible ]);
  313. /**
  314. * Renders the toolbox content.
  315. *
  316. * @returns {ReactElement}
  317. */
  318. function renderToolboxContent() {
  319. const toolbarAccLabel = 'toolbar.accessibilityLabel.moreActionsMenu';
  320. const containerClassName = `toolbox-content${_isMobile || _isNarrowLayout ? ' toolbox-content-mobile' : ''}`;
  321. const { mainMenuButtons, overflowMenuButtons } = getVisibleButtons();
  322. const raiseHandInOverflowMenu = overflowMenuButtons.some(({ key }) => key === 'raisehand');
  323. const showReactionsInOverflowMenu = _shouldDisplayReactionsButtons
  324. && (
  325. (!_reactionsButtonEnabled && (raiseHandInOverflowMenu || _isNarrowLayout || _isMobile))
  326. || overflowMenuButtons.some(({ key }) => key === 'reactions')
  327. );
  328. const showRaiseHandInReactionsMenu = showReactionsInOverflowMenu && raiseHandInOverflowMenu;
  329. return (
  330. <div className = { containerClassName }>
  331. <div
  332. className = 'toolbox-content-wrapper'
  333. onFocus = { onTabIn }
  334. { ...(_isMobile ? {} : {
  335. onMouseOut,
  336. onMouseOver
  337. }) }>
  338. <div
  339. className = 'toolbox-content-items'
  340. ref = { _toolboxRef }>
  341. {mainMenuButtons.map(({ Content, key, ...rest }) => Content !== Separator && (
  342. <Content
  343. { ...rest }
  344. buttonKey = { key }
  345. key = { key } />))}
  346. {Boolean(overflowMenuButtons.length) && (
  347. <OverflowMenuButton
  348. ariaControls = 'overflow-menu'
  349. buttons = { overflowMenuButtons.reduce<Array<IToolboxButton[]>>((acc, val) => {
  350. if (val.key === 'reactions' && showReactionsInOverflowMenu) {
  351. return acc;
  352. }
  353. if (val.key === 'raisehand' && showRaiseHandInReactionsMenu) {
  354. return acc;
  355. }
  356. if (acc.length) {
  357. const prev = acc[acc.length - 1];
  358. const group = prev[prev.length - 1].group;
  359. if (group === val.group) {
  360. prev.push(val);
  361. } else {
  362. acc.push([ val ]);
  363. }
  364. } else {
  365. acc.push([ val ]);
  366. }
  367. return acc;
  368. }, []) }
  369. isOpen = { _overflowMenuVisible }
  370. key = 'overflow-menu'
  371. onToolboxEscKey = { onEscKey }
  372. onVisibilityChange = { onSetOverflowVisible }
  373. showRaiseHandInReactionsMenu = { showRaiseHandInReactionsMenu }
  374. showReactionsMenu = { showReactionsInOverflowMenu } />
  375. )}
  376. {isButtonEnabled('hangup', _toolbarButtons) && (
  377. _endConferenceSupported
  378. ? <HangupMenuButton
  379. ariaControls = 'hangup-menu'
  380. isOpen = { _hangupMenuVisible }
  381. key = 'hangup-menu'
  382. notifyMode = { _buttonsWithNotifyClick?.get('hangup-menu') }
  383. onVisibilityChange = { onSetHangupVisible }>
  384. <ContextMenu
  385. accessibilityLabel = { t(toolbarAccLabel) }
  386. className = { classes.hangupMenu }
  387. hidden = { false }
  388. inDrawer = { _overflowDrawer }
  389. onKeyDown = { onEscKey }>
  390. <EndConferenceButton
  391. buttonKey = 'end-meeting'
  392. notifyMode = { _buttonsWithNotifyClick?.get('end-meeting') } />
  393. <LeaveConferenceButton
  394. buttonKey = 'hangup'
  395. notifyMode = { _buttonsWithNotifyClick?.get('hangup') } />
  396. </ContextMenu>
  397. </HangupMenuButton>
  398. : <HangupButton
  399. buttonKey = 'hangup'
  400. customClass = 'hangup-button'
  401. key = 'hangup-button'
  402. notifyMode = { _buttonsWithNotifyClick.get('hangup') }
  403. visible = { isButtonEnabled('hangup', _toolbarButtons) } />
  404. )}
  405. </div>
  406. </div>
  407. </div>
  408. );
  409. }
  410. if (_disabled) {
  411. return null;
  412. }
  413. const rootClassNames = `new-toolbox ${_visible ? 'visible' : ''} ${
  414. _toolbarButtons.length ? '' : 'no-buttons'} ${_chatOpen ? 'shift-right' : ''}`;
  415. return (
  416. <div
  417. className = { cx(rootClassNames, _shiftUp && 'shift-up') }
  418. id = 'new-toolbox'>
  419. {renderToolboxContent()}
  420. </div>
  421. );
  422. };
  423. /**
  424. * Maps (parts of) the redux state to {@link Toolbox}'s React {@code Component}
  425. * props.
  426. *
  427. * @param {Object} state - The redux store/state.
  428. * @param {Object} ownProps - The props explicitly passed.
  429. * @private
  430. * @returns {{}}
  431. */
  432. function _mapStateToProps(state: IReduxState, ownProps: any) {
  433. const { conference } = state['features/base/conference'];
  434. const { isNarrowLayout } = state['features/base/responsive-ui'];
  435. const endConferenceSupported = conference?.isEndConferenceSupported() && isLocalParticipantModerator(state);
  436. const {
  437. customToolbarButtons,
  438. iAmRecorder,
  439. iAmSipGateway
  440. } = state['features/base/config'];
  441. const {
  442. hangupMenuVisible,
  443. overflowMenuVisible,
  444. overflowDrawer
  445. } = state['features/toolbox'];
  446. const { clientWidth } = state['features/base/responsive-ui'];
  447. const toolbarButtons = ownProps.toolbarButtons || state['features/toolbox'].toolbarButtons;
  448. return {
  449. _buttonsWithNotifyClick: state['features/toolbox'].buttonsWithNotifyClick,
  450. _chatOpen: state['features/chat'].isOpen,
  451. _clientWidth: clientWidth,
  452. _customToolbarButtons: customToolbarButtons,
  453. _dialog: Boolean(state['features/base/dialog'].component),
  454. _disabled: Boolean(iAmRecorder || iAmSipGateway),
  455. _endConferenceSupported: Boolean(endConferenceSupported),
  456. _isMobile: isMobileBrowser(),
  457. _jwtDisabledButtons: getJwtDisabledButtons(state),
  458. _hangupMenuVisible: hangupMenuVisible,
  459. _isNarrowLayout: isNarrowLayout,
  460. _mainToolbarButtonsThresholds: state['features/toolbox'].mainToolbarButtonsThresholds,
  461. _overflowMenuVisible: overflowMenuVisible,
  462. _overflowDrawer: overflowDrawer,
  463. _reactionsButtonEnabled: isReactionsButtonEnabled(state),
  464. _shiftUp: state['features/toolbox'].shiftUp,
  465. _shouldDisplayReactionsButtons: shouldDisplayReactionsButtons(state),
  466. _toolbarButtons: toolbarButtons,
  467. _visible: isToolboxVisible(state)
  468. };
  469. }
  470. export default translate(connect(_mapStateToProps)(Toolbox));