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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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 { NOT_APPLICABLE, THRESHOLDS } 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 } 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. * Whether or not the overflow menu is displayed in a drawer drawer.
  82. */
  83. _overflowDrawer: boolean;
  84. /**
  85. * Whether or not the overflow menu is visible.
  86. */
  87. _overflowMenuVisible: boolean;
  88. /**
  89. * Whether or not to display reactions in separate button.
  90. */
  91. _reactionsButtonEnabled: boolean;
  92. /**
  93. * Whether the toolbox should be shifted up or not.
  94. */
  95. _shiftUp: boolean;
  96. /**
  97. * Whether any reactions buttons should be displayed or not.
  98. */
  99. _shouldDisplayReactionsButtons: boolean;
  100. /**
  101. * The enabled buttons.
  102. */
  103. _toolbarButtons: Array<string>;
  104. /**
  105. * Flag showing whether toolbar is visible.
  106. */
  107. _visible: boolean;
  108. /**
  109. * Invoked to active other features of the app.
  110. */
  111. dispatch: IStore['dispatch'];
  112. /**
  113. * Explicitly passed array with the buttons which this Toolbox should display.
  114. */
  115. toolbarButtons: Array<string>;
  116. }
  117. const useStyles = makeStyles()(() => {
  118. return {
  119. contextMenu: {
  120. position: 'relative',
  121. right: 'auto',
  122. margin: 0,
  123. marginBottom: '8px',
  124. maxHeight: 'calc(100dvh - 100px)',
  125. minWidth: '240px'
  126. },
  127. hangupMenu: {
  128. position: 'relative',
  129. right: 'auto',
  130. display: 'flex',
  131. flexDirection: 'column',
  132. rowGap: '8px',
  133. margin: 0,
  134. padding: '16px',
  135. marginBottom: '4px'
  136. }
  137. };
  138. });
  139. const Toolbox = ({
  140. _buttonsWithNotifyClick,
  141. _chatOpen,
  142. _clientWidth,
  143. _customToolbarButtons,
  144. _dialog,
  145. _disabled,
  146. _endConferenceSupported,
  147. _hangupMenuVisible,
  148. _isMobile,
  149. _isNarrowLayout,
  150. _jwtDisabledButtons,
  151. _overflowDrawer,
  152. _overflowMenuVisible,
  153. _reactionsButtonEnabled,
  154. _shiftUp,
  155. _shouldDisplayReactionsButtons,
  156. _toolbarButtons,
  157. _visible,
  158. dispatch,
  159. t,
  160. toolbarButtons
  161. }: IProps) => {
  162. const { classes, cx } = useStyles();
  163. const _toolboxRef = useRef<HTMLDivElement>(null);
  164. useKeyboardShortcuts(toolbarButtons);
  165. useEffect(() => {
  166. if (!_visible) {
  167. if (document.activeElement instanceof HTMLElement
  168. && _toolboxRef.current?.contains(document.activeElement)) {
  169. document.activeElement.blur();
  170. }
  171. }
  172. }, [ _visible ]);
  173. /**
  174. * Sets the visibility of the hangup menu.
  175. *
  176. * @param {boolean} visible - Whether or not the hangup menu should be
  177. * displayed.
  178. * @private
  179. * @returns {void}
  180. */
  181. const onSetHangupVisible = useCallback((visible: boolean) => {
  182. dispatch(setHangupMenuVisible(visible));
  183. dispatch(setToolbarHovered(visible));
  184. }, []);
  185. /**
  186. * Sets the visibility of the overflow menu.
  187. *
  188. * @param {boolean} visible - Whether or not the overflow menu should be
  189. * displayed.
  190. * @private
  191. * @returns {void}
  192. */
  193. const onSetOverflowVisible = useCallback((visible: boolean) => {
  194. dispatch(setOverflowMenuVisible(visible));
  195. dispatch(setToolbarHovered(visible));
  196. }, []);
  197. useEffect(() => {
  198. if (_hangupMenuVisible && !_visible) {
  199. onSetHangupVisible(false);
  200. dispatch(setToolbarHovered(false));
  201. }
  202. }, [ _hangupMenuVisible, _visible ]);
  203. useEffect(() => {
  204. if (_overflowMenuVisible && _dialog) {
  205. onSetOverflowVisible(false);
  206. dispatch(setToolbarHovered(false));
  207. }
  208. }, [ _overflowMenuVisible, _dialog ]);
  209. /**
  210. * Key handler for overflow/hangup menus.
  211. *
  212. * @param {KeyboardEvent} e - Esc key click to close the popup.
  213. * @returns {void}
  214. */
  215. const onEscKey = useCallback((e?: React.KeyboardEvent) => {
  216. if (e?.key === 'Escape') {
  217. e?.stopPropagation();
  218. _hangupMenuVisible && dispatch(setHangupMenuVisible(false));
  219. _overflowMenuVisible && dispatch(setOverflowMenuVisible(false));
  220. }
  221. }, [ _hangupMenuVisible, _overflowMenuVisible ]);
  222. /**
  223. * Sets the notify click mode for the buttons.
  224. *
  225. * @param {Object} buttons - The list of toolbar buttons.
  226. * @returns {void}
  227. */
  228. function setButtonsNotifyClickMode(buttons: Object) {
  229. if (typeof APP === 'undefined' || (_buttonsWithNotifyClick?.size ?? 0) <= 0) {
  230. return;
  231. }
  232. Object.values(buttons).forEach((button: any) => {
  233. if (typeof button === 'object') {
  234. button.notifyMode = _buttonsWithNotifyClick.get(button.key);
  235. }
  236. });
  237. }
  238. /**
  239. * Returns all buttons that need to be rendered.
  240. *
  241. * @param {Object} state - The redux state.
  242. * @returns {Object} The visible buttons arrays .
  243. */
  244. function getVisibleButtons() {
  245. const buttons = getAllToolboxButtons(_customToolbarButtons);
  246. setButtonsNotifyClickMode(buttons);
  247. const isHangupVisible = isButtonEnabled('hangup', _toolbarButtons);
  248. const { order } = THRESHOLDS.find(({ width }) => _clientWidth > width)
  249. || THRESHOLDS[THRESHOLDS.length - 1];
  250. const keys = Object.keys(buttons);
  251. const filtered = [
  252. ...order.map(key => buttons[key as keyof typeof buttons]),
  253. ...Object.values(buttons).filter((button, index) => !order.includes(keys[index]))
  254. ].filter(({ key, alias = NOT_APPLICABLE }) =>
  255. !_jwtDisabledButtons.includes(key)
  256. && (isButtonEnabled(key, _toolbarButtons) || isButtonEnabled(alias, _toolbarButtons))
  257. );
  258. let sliceIndex = _overflowDrawer || _reactionsButtonEnabled ? order.length + 2 : order.length + 1;
  259. if (isHangupVisible) {
  260. sliceIndex -= 1;
  261. }
  262. // This implies that the overflow button will be displayed, so save some space for it.
  263. if (sliceIndex < filtered.length) {
  264. sliceIndex -= 1;
  265. }
  266. return {
  267. mainMenuButtons: filtered.slice(0, sliceIndex),
  268. overflowMenuButtons: filtered.slice(sliceIndex)
  269. };
  270. }
  271. /**
  272. * Dispatches an action signaling the toolbar is not being hovered.
  273. *
  274. * @private
  275. * @returns {void}
  276. */
  277. function onMouseOut() {
  278. !_overflowMenuVisible && dispatch(setToolbarHovered(false));
  279. }
  280. /**
  281. * Dispatches an action signaling the toolbar is being hovered.
  282. *
  283. * @private
  284. * @returns {void}
  285. */
  286. function onMouseOver() {
  287. dispatch(setToolbarHovered(true));
  288. }
  289. /**
  290. * Toggle the toolbar visibility when tabbing into it.
  291. *
  292. * @returns {void}
  293. */
  294. const onTabIn = useCallback(() => {
  295. if (!_visible) {
  296. dispatch(showToolbox());
  297. }
  298. }, [ _visible ]);
  299. /**
  300. * Renders the toolbox content.
  301. *
  302. * @returns {ReactElement}
  303. */
  304. function renderToolboxContent() {
  305. const toolbarAccLabel = 'toolbar.accessibilityLabel.moreActionsMenu';
  306. const containerClassName = `toolbox-content${_isMobile || _isNarrowLayout ? ' toolbox-content-mobile' : ''}`;
  307. const { mainMenuButtons, overflowMenuButtons } = getVisibleButtons();
  308. const raiseHandInOverflowMenu = overflowMenuButtons.some(({ key }) => key === 'raisehand');
  309. const showReactionsInOverflowMenu = _shouldDisplayReactionsButtons
  310. && (
  311. (!_reactionsButtonEnabled && (raiseHandInOverflowMenu || _isNarrowLayout || _isMobile))
  312. || overflowMenuButtons.some(({ key }) => key === 'reactions')
  313. );
  314. const showRaiseHandInReactionsMenu = showReactionsInOverflowMenu && raiseHandInOverflowMenu;
  315. return (
  316. <div className = { containerClassName }>
  317. <div
  318. className = 'toolbox-content-wrapper'
  319. onFocus = { onTabIn }
  320. { ...(_isMobile ? {} : {
  321. onMouseOut,
  322. onMouseOver
  323. }) }>
  324. <div
  325. className = 'toolbox-content-items'
  326. ref = { _toolboxRef }>
  327. {mainMenuButtons.map(({ Content, key, ...rest }) => Content !== Separator && (
  328. <Content
  329. { ...rest }
  330. buttonKey = { key }
  331. key = { key } />))}
  332. {Boolean(overflowMenuButtons.length) && (
  333. <OverflowMenuButton
  334. ariaControls = 'overflow-menu'
  335. buttons = { overflowMenuButtons.reduce<Array<IToolboxButton[]>>((acc, val) => {
  336. if (val.key === 'reactions' && showReactionsInOverflowMenu) {
  337. return acc;
  338. }
  339. if (val.key === 'raisehand' && showRaiseHandInReactionsMenu) {
  340. return acc;
  341. }
  342. if (acc.length) {
  343. const prev = acc[acc.length - 1];
  344. const group = prev[prev.length - 1].group;
  345. if (group === val.group) {
  346. prev.push(val);
  347. } else {
  348. acc.push([ val ]);
  349. }
  350. } else {
  351. acc.push([ val ]);
  352. }
  353. return acc;
  354. }, []) }
  355. isOpen = { _overflowMenuVisible }
  356. key = 'overflow-menu'
  357. onToolboxEscKey = { onEscKey }
  358. onVisibilityChange = { onSetOverflowVisible }
  359. showRaiseHandInReactionsMenu = { showRaiseHandInReactionsMenu }
  360. showReactionsMenu = { showReactionsInOverflowMenu } />
  361. )}
  362. {isButtonEnabled('hangup', _toolbarButtons) && (
  363. _endConferenceSupported
  364. ? <HangupMenuButton
  365. ariaControls = 'hangup-menu'
  366. isOpen = { _hangupMenuVisible }
  367. key = 'hangup-menu'
  368. notifyMode = { _buttonsWithNotifyClick?.get('hangup-menu') }
  369. onVisibilityChange = { onSetHangupVisible }>
  370. <ContextMenu
  371. accessibilityLabel = { t(toolbarAccLabel) }
  372. className = { classes.hangupMenu }
  373. hidden = { false }
  374. inDrawer = { _overflowDrawer }
  375. onKeyDown = { onEscKey }>
  376. <EndConferenceButton
  377. buttonKey = 'end-meeting'
  378. notifyMode = { _buttonsWithNotifyClick?.get('end-meeting') } />
  379. <LeaveConferenceButton
  380. buttonKey = 'hangup'
  381. notifyMode = { _buttonsWithNotifyClick?.get('hangup') } />
  382. </ContextMenu>
  383. </HangupMenuButton>
  384. : <HangupButton
  385. buttonKey = 'hangup'
  386. customClass = 'hangup-button'
  387. key = 'hangup-button'
  388. notifyMode = { _buttonsWithNotifyClick.get('hangup') }
  389. visible = { isButtonEnabled('hangup', _toolbarButtons) } />
  390. )}
  391. </div>
  392. </div>
  393. </div>
  394. );
  395. }
  396. if (_disabled) {
  397. return null;
  398. }
  399. const rootClassNames = `new-toolbox ${_visible ? 'visible' : ''} ${
  400. _toolbarButtons.length ? '' : 'no-buttons'} ${_chatOpen ? 'shift-right' : ''}`;
  401. return (
  402. <div
  403. className = { cx(rootClassNames, _shiftUp && 'shift-up') }
  404. id = 'new-toolbox'>
  405. {renderToolboxContent()}
  406. </div>
  407. );
  408. };
  409. /**
  410. * Maps (parts of) the redux state to {@link Toolbox}'s React {@code Component}
  411. * props.
  412. *
  413. * @param {Object} state - The redux store/state.
  414. * @param {Object} ownProps - The props explicitly passed.
  415. * @private
  416. * @returns {{}}
  417. */
  418. function _mapStateToProps(state: IReduxState, ownProps: any) {
  419. const { conference } = state['features/base/conference'];
  420. const { isNarrowLayout } = state['features/base/responsive-ui'];
  421. const endConferenceSupported = conference?.isEndConferenceSupported() && isLocalParticipantModerator(state);
  422. const {
  423. customToolbarButtons,
  424. iAmRecorder,
  425. iAmSipGateway
  426. } = state['features/base/config'];
  427. const {
  428. hangupMenuVisible,
  429. overflowMenuVisible,
  430. overflowDrawer
  431. } = state['features/toolbox'];
  432. const { clientWidth } = state['features/base/responsive-ui'];
  433. const toolbarButtons = ownProps.toolbarButtons || state['features/toolbox'].toolbarButtons;
  434. return {
  435. _buttonsWithNotifyClick: state['features/toolbox'].buttonsWithNotifyClick,
  436. _chatOpen: state['features/chat'].isOpen,
  437. _clientWidth: clientWidth,
  438. _customToolbarButtons: customToolbarButtons,
  439. _dialog: Boolean(state['features/base/dialog'].component),
  440. _disabled: Boolean(iAmRecorder || iAmSipGateway),
  441. _endConferenceSupported: Boolean(endConferenceSupported),
  442. _isMobile: isMobileBrowser(),
  443. _jwtDisabledButtons: getJwtDisabledButtons(state),
  444. _hangupMenuVisible: hangupMenuVisible,
  445. _isNarrowLayout: isNarrowLayout,
  446. _overflowMenuVisible: overflowMenuVisible,
  447. _overflowDrawer: overflowDrawer,
  448. _reactionsButtonEnabled: isReactionsButtonEnabled(state),
  449. _shiftUp: state['features/toolbox'].shiftUp,
  450. _shouldDisplayReactionsButtons: shouldDisplayReactionsButtons(state),
  451. _toolbarButtons: toolbarButtons,
  452. _visible: isToolboxVisible(state)
  453. };
  454. }
  455. export default translate(connect(_mapStateToProps)(Toolbox));