您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Toolbox.tsx 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 { NotifyClickButton } from '../../../base/config/configType';
  7. import { VISITORS_MODE_BUTTONS } from '../../../base/config/constants';
  8. import {
  9. getButtonNotifyMode,
  10. getButtonsWithNotifyClick,
  11. getToolbarButtons,
  12. isToolbarButtonEnabled
  13. } from '../../../base/config/functions.web';
  14. import { isMobileBrowser } from '../../../base/environment/utils';
  15. import { translate } from '../../../base/i18n/functions';
  16. import { isLocalParticipantModerator } from '../../../base/participants/functions';
  17. import ContextMenu from '../../../base/ui/components/web/ContextMenu';
  18. import { isReactionsButtonEnabled, isReactionsEnabled } from '../../../reactions/functions.web';
  19. import { iAmVisitor } from '../../../visitors/functions';
  20. import {
  21. setHangupMenuVisible,
  22. setOverflowMenuVisible,
  23. setToolbarHovered,
  24. showToolbox
  25. } from '../../actions.web';
  26. import { NOT_APPLICABLE, THRESHOLDS } from '../../constants';
  27. import {
  28. getAllToolboxButtons,
  29. getJwtDisabledButtons,
  30. isToolboxVisible
  31. } from '../../functions.web';
  32. import { useKeyboardShortcuts } from '../../hooks.web';
  33. import { IToolboxButton } from '../../types';
  34. import HangupButton from '../HangupButton';
  35. import { EndConferenceButton } from './EndConferenceButton';
  36. import HangupMenuButton from './HangupMenuButton';
  37. import { LeaveConferenceButton } from './LeaveConferenceButton';
  38. import OverflowMenuButton from './OverflowMenuButton';
  39. import Separator from './Separator';
  40. /**
  41. * The type of the React {@code Component} props of {@link Toolbox}.
  42. */
  43. interface IProps extends WithTranslation {
  44. /**
  45. * Toolbar buttons which have their click exposed through the API.
  46. */
  47. _buttonsWithNotifyClick?: NotifyClickButton[];
  48. /**
  49. * Whether or not the chat feature is currently displayed.
  50. */
  51. _chatOpen: boolean;
  52. /**
  53. * The width of the client.
  54. */
  55. _clientWidth: number;
  56. /**
  57. * Custom Toolbar buttons.
  58. */
  59. _customToolbarButtons?: Array<{ icon: string; id: string; text: string; }>;
  60. /**
  61. * Whether or not a dialog is displayed.
  62. */
  63. _dialog: boolean;
  64. /**
  65. * Whether or not the toolbox is disabled. It is for recorders.
  66. */
  67. _disabled: boolean;
  68. /**
  69. * Whether the end conference feature is supported.
  70. */
  71. _endConferenceSupported: boolean;
  72. /**
  73. * Whether the hangup menu is visible.
  74. */
  75. _hangupMenuVisible: boolean;
  76. /**
  77. * Whether or not the app is running in mobile browser.
  78. */
  79. _isMobile: boolean;
  80. /**
  81. * Whether we are in narrow layout mode.
  82. */
  83. _isNarrowLayout: boolean;
  84. /**
  85. * The array of toolbar buttons disabled through jwt features.
  86. */
  87. _jwtDisabledButtons: string[];
  88. /**
  89. * Whether or not the overflow menu is displayed in a drawer drawer.
  90. */
  91. _overflowDrawer: boolean;
  92. /**
  93. * Whether or not the overflow menu is visible.
  94. */
  95. _overflowMenuVisible: boolean;
  96. /**
  97. * Whether or not to display reactions in separate button.
  98. */
  99. _reactionsButtonEnabled: boolean;
  100. /**
  101. * Whether or not reactions feature is enabled.
  102. */
  103. _reactionsEnabled: boolean;
  104. /**
  105. * Whether the toolbox should be shifted up or not.
  106. */
  107. _shiftUp: boolean;
  108. /**
  109. * The enabled buttons.
  110. */
  111. _toolbarButtons: Array<string>;
  112. /**
  113. * Flag showing whether toolbar is visible.
  114. */
  115. _visible: boolean;
  116. /**
  117. * Invoked to active other features of the app.
  118. */
  119. dispatch: IStore['dispatch'];
  120. /**
  121. * Explicitly passed array with the buttons which this Toolbox should display.
  122. */
  123. toolbarButtons: Array<string>;
  124. }
  125. const useStyles = makeStyles()(() => {
  126. return {
  127. contextMenu: {
  128. position: 'relative',
  129. right: 'auto',
  130. margin: 0,
  131. marginBottom: '8px',
  132. maxHeight: 'calc(100vh - 100px)',
  133. minWidth: '240px'
  134. },
  135. hangupMenu: {
  136. position: 'relative',
  137. right: 'auto',
  138. display: 'flex',
  139. flexDirection: 'column',
  140. rowGap: '8px',
  141. margin: 0,
  142. padding: '16px',
  143. marginBottom: '4px'
  144. }
  145. };
  146. });
  147. const Toolbox = ({
  148. _buttonsWithNotifyClick,
  149. _chatOpen,
  150. _clientWidth,
  151. _customToolbarButtons,
  152. _dialog,
  153. _disabled,
  154. _endConferenceSupported,
  155. _hangupMenuVisible,
  156. _isMobile,
  157. _isNarrowLayout,
  158. _jwtDisabledButtons,
  159. _overflowDrawer,
  160. _overflowMenuVisible,
  161. _reactionsButtonEnabled,
  162. _reactionsEnabled,
  163. _shiftUp,
  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?.length) {
  238. return;
  239. }
  240. Object.values(buttons).forEach((button: any) => {
  241. if (typeof button === 'object') {
  242. button.notifyMode = getButtonNotifyMode(button.key, _buttonsWithNotifyClick);
  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. setButtonsNotifyClickMode(buttons);
  255. const isHangupVisible = isToolbarButtonEnabled('hangup', _toolbarButtons);
  256. let { order } = THRESHOLDS.find(({ width }) => _clientWidth > width)
  257. || THRESHOLDS[THRESHOLDS.length - 1];
  258. const keys = Object.keys(buttons);
  259. const filtered = [
  260. ...order.map(key => buttons[key as keyof typeof buttons]),
  261. ...Object.values(buttons).filter((button, index) => !order.includes(keys[index]))
  262. ].filter(({ key, alias = NOT_APPLICABLE }) =>
  263. !_jwtDisabledButtons.includes(key)
  264. && (isToolbarButtonEnabled(key, _toolbarButtons) || isToolbarButtonEnabled(alias, _toolbarButtons))
  265. );
  266. const filteredKeys = filtered.map(button => button.key);
  267. order = order.filter(key => filteredKeys.includes(buttons[key as keyof typeof buttons].key));
  268. let sliceIndex = order.length + 2;
  269. if (isHangupVisible) {
  270. sliceIndex -= 1;
  271. }
  272. // This implies that the overflow button will be displayed, so save some space for it.
  273. if (sliceIndex < filtered.length) {
  274. sliceIndex -= 1;
  275. }
  276. return {
  277. mainMenuButtons: filtered.slice(0, sliceIndex),
  278. overflowMenuButtons: filtered.slice(sliceIndex)
  279. };
  280. }
  281. /**
  282. * Dispatches an action signaling the toolbar is not being hovered.
  283. *
  284. * @private
  285. * @returns {void}
  286. */
  287. function onMouseOut() {
  288. !_overflowMenuVisible && dispatch(setToolbarHovered(false));
  289. }
  290. /**
  291. * Dispatches an action signaling the toolbar is being hovered.
  292. *
  293. * @private
  294. * @returns {void}
  295. */
  296. function onMouseOver() {
  297. dispatch(setToolbarHovered(true));
  298. }
  299. /**
  300. * Toggle the toolbar visibility when tabbing into it.
  301. *
  302. * @returns {void}
  303. */
  304. const onTabIn = useCallback(() => {
  305. if (!_visible) {
  306. dispatch(showToolbox());
  307. }
  308. }, [ _visible ]);
  309. /**
  310. * Renders the toolbox content.
  311. *
  312. * @returns {ReactElement}
  313. */
  314. function renderToolboxContent() {
  315. const toolbarAccLabel = 'toolbar.accessibilityLabel.moreActionsMenu';
  316. const containerClassName = `toolbox-content${_isMobile || _isNarrowLayout ? ' toolbox-content-mobile' : ''}`;
  317. const { mainMenuButtons, overflowMenuButtons } = getVisibleButtons();
  318. const raiseHandInOverflowMenu = overflowMenuButtons.some(({ key }) => key === 'raisehand');
  319. const showReactionsInOverflowMenu
  320. = (_reactionsEnabled && !_reactionsButtonEnabled
  321. && (raiseHandInOverflowMenu || _isNarrowLayout || _isMobile))
  322. || overflowMenuButtons.some(({ key }) => key === 'reactions');
  323. const showRaiseHandInReactionsMenu = showReactionsInOverflowMenu && raiseHandInOverflowMenu;
  324. return (
  325. <div className = { containerClassName }>
  326. <div
  327. className = 'toolbox-content-wrapper'
  328. onFocus = { onTabIn }
  329. { ...(_isMobile ? {} : {
  330. onMouseOut,
  331. onMouseOver
  332. }) }>
  333. <div
  334. className = 'toolbox-content-items'
  335. ref = { _toolboxRef }>
  336. {mainMenuButtons.map(({ Content, key, ...rest }) => Content !== Separator && (
  337. <Content
  338. { ...rest }
  339. buttonKey = { key }
  340. key = { key } />))}
  341. {Boolean(overflowMenuButtons.length) && (
  342. <OverflowMenuButton
  343. ariaControls = 'overflow-menu'
  344. buttons = { overflowMenuButtons.reduce<Array<IToolboxButton[]>>((acc, val) => {
  345. if (val.key === 'reactions' && showReactionsInOverflowMenu) {
  346. return acc;
  347. }
  348. if (val.key === 'raisehand' && showRaiseHandInReactionsMenu) {
  349. return acc;
  350. }
  351. if (acc.length) {
  352. const prev = acc[acc.length - 1];
  353. const group = prev[prev.length - 1].group;
  354. if (group === val.group) {
  355. prev.push(val);
  356. } else {
  357. acc.push([ val ]);
  358. }
  359. } else {
  360. acc.push([ val ]);
  361. }
  362. return acc;
  363. }, []) }
  364. isOpen = { _overflowMenuVisible }
  365. key = 'overflow-menu'
  366. onToolboxEscKey = { onEscKey }
  367. onVisibilityChange = { onSetOverflowVisible }
  368. showRaiseHandInReactionsMenu = { showRaiseHandInReactionsMenu }
  369. showReactionsMenu = { showReactionsInOverflowMenu } />
  370. )}
  371. {isToolbarButtonEnabled('hangup', _toolbarButtons) && (
  372. _endConferenceSupported
  373. ? <HangupMenuButton
  374. ariaControls = 'hangup-menu'
  375. isOpen = { _hangupMenuVisible }
  376. key = 'hangup-menu'
  377. notifyMode = { getButtonNotifyMode('hangup-menu', _buttonsWithNotifyClick) }
  378. onVisibilityChange = { onSetHangupVisible }>
  379. <ContextMenu
  380. accessibilityLabel = { t(toolbarAccLabel) }
  381. className = { classes.hangupMenu }
  382. hidden = { false }
  383. inDrawer = { _overflowDrawer }
  384. onKeyDown = { onEscKey }>
  385. <EndConferenceButton
  386. buttonKey = 'end-meeting'
  387. notifyMode = { getButtonNotifyMode(
  388. 'end-meeting',
  389. _buttonsWithNotifyClick
  390. ) } />
  391. <LeaveConferenceButton
  392. buttonKey = 'hangup'
  393. notifyMode = { getButtonNotifyMode('hangup', _buttonsWithNotifyClick) } />
  394. </ContextMenu>
  395. </HangupMenuButton>
  396. : <HangupButton
  397. buttonKey = 'hangup'
  398. customClass = 'hangup-button'
  399. key = 'hangup-button'
  400. notifyMode = { getButtonNotifyMode('hangup', _buttonsWithNotifyClick) }
  401. visible = { isToolbarButtonEnabled('hangup', _toolbarButtons) } />
  402. )}
  403. </div>
  404. </div>
  405. </div>
  406. );
  407. }
  408. if (_disabled) {
  409. return null;
  410. }
  411. const rootClassNames = `new-toolbox ${_visible ? 'visible' : ''} ${
  412. _toolbarButtons.length ? '' : 'no-buttons'} ${_chatOpen ? 'shift-right' : ''}`;
  413. return (
  414. <div
  415. className = { cx(rootClassNames, _shiftUp && 'shift-up') }
  416. id = 'new-toolbox'>
  417. {renderToolboxContent()}
  418. </div>
  419. );
  420. };
  421. /**
  422. * Maps (parts of) the redux state to {@link Toolbox}'s React {@code Component}
  423. * props.
  424. *
  425. * @param {Object} state - The redux store/state.
  426. * @param {Object} ownProps - The props explicitly passed.
  427. * @private
  428. * @returns {{}}
  429. */
  430. function _mapStateToProps(state: IReduxState, ownProps: any) {
  431. const { conference } = state['features/base/conference'];
  432. const { isNarrowLayout } = state['features/base/responsive-ui'];
  433. const endConferenceSupported = conference?.isEndConferenceSupported() && isLocalParticipantModerator(state);
  434. const {
  435. customToolbarButtons,
  436. iAmRecorder,
  437. iAmSipGateway
  438. } = state['features/base/config'];
  439. const {
  440. hangupMenuVisible,
  441. overflowMenuVisible,
  442. overflowDrawer
  443. } = state['features/toolbox'];
  444. const { clientWidth } = state['features/base/responsive-ui'];
  445. let toolbarButtons = ownProps.toolbarButtons || getToolbarButtons(state);
  446. const _reactionsEnabled = isReactionsEnabled(state);
  447. if (iAmVisitor(state)) {
  448. toolbarButtons = VISITORS_MODE_BUTTONS.filter(e => toolbarButtons.indexOf(e) > -1);
  449. }
  450. return {
  451. _buttonsWithNotifyClick: getButtonsWithNotifyClick(state),
  452. _chatOpen: state['features/chat'].isOpen,
  453. _clientWidth: clientWidth,
  454. _customToolbarButtons: customToolbarButtons,
  455. _dialog: Boolean(state['features/base/dialog'].component),
  456. _disabled: Boolean(iAmRecorder || iAmSipGateway),
  457. _endConferenceSupported: Boolean(endConferenceSupported),
  458. _isMobile: isMobileBrowser(),
  459. _jwtDisabledButtons: getJwtDisabledButtons(state),
  460. _hangupMenuVisible: hangupMenuVisible,
  461. _isNarrowLayout: isNarrowLayout,
  462. _overflowMenuVisible: overflowMenuVisible,
  463. _overflowDrawer: overflowDrawer,
  464. _reactionsButtonEnabled: isReactionsButtonEnabled(state),
  465. _reactionsEnabled,
  466. _shiftUp: state['features/toolbox'].shiftUp,
  467. _toolbarButtons: toolbarButtons,
  468. _visible: isToolboxVisible(state)
  469. };
  470. }
  471. export default translate(connect(_mapStateToProps)(Toolbox));