Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

hooks.web.ts 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. import { useEffect } from 'react';
  2. import { batch, useDispatch, useSelector } from 'react-redux';
  3. import { ACTION_SHORTCUT_TRIGGERED, createShortcutEvent } from '../analytics/AnalyticsEvents';
  4. import { sendAnalytics } from '../analytics/functions';
  5. import { IReduxState } from '../app/types';
  6. import { toggleDialog } from '../base/dialog/actions';
  7. import { isIosMobileBrowser, isIpadMobileBrowser } from '../base/environment/utils';
  8. import { HELP_BUTTON_ENABLED } from '../base/flags/constants';
  9. import { getFeatureFlag } from '../base/flags/functions';
  10. import JitsiMeetJS from '../base/lib-jitsi-meet';
  11. import { raiseHand } from '../base/participants/actions';
  12. import { getLocalParticipant, hasRaisedHand } from '../base/participants/functions';
  13. import { isToggleCameraEnabled } from '../base/tracks/functions.web';
  14. import { toggleChat } from '../chat/actions.web';
  15. import ChatButton from '../chat/components/web/ChatButton';
  16. import { useEmbedButton } from '../embed-meeting/hooks';
  17. import { useEtherpadButton } from '../etherpad/hooks';
  18. import { useFeedbackButton } from '../feedback/hooks.web';
  19. import { setGifMenuVisibility } from '../gifs/actions';
  20. import { isGifEnabled } from '../gifs/function.any';
  21. import InviteButton from '../invite/components/add-people-dialog/web/InviteButton';
  22. import { registerShortcut, unregisterShortcut } from '../keyboard-shortcuts/actions';
  23. import { useKeyboardShortcutsButton } from '../keyboard-shortcuts/hooks';
  24. import NoiseSuppressionButton from '../noise-suppression/components/NoiseSuppressionButton';
  25. import {
  26. close as closeParticipantsPane,
  27. open as openParticipantsPane
  28. } from '../participants-pane/actions.web';
  29. import {
  30. getParticipantsPaneOpen,
  31. isParticipantsPaneEnabled
  32. } from '../participants-pane/functions';
  33. import { useParticipantPaneButton } from '../participants-pane/hooks.web';
  34. import { addReactionToBuffer } from '../reactions/actions.any';
  35. import { toggleReactionsMenuVisibility } from '../reactions/actions.web';
  36. import RaiseHandContainerButton from '../reactions/components/web/RaiseHandContainerButtons';
  37. import { REACTIONS } from '../reactions/constants';
  38. import { shouldDisplayReactionsButtons } from '../reactions/functions.any';
  39. import { useReactionsButton } from '../reactions/hooks.web';
  40. import { useLiveStreamingButton, useRecordingButton } from '../recording/hooks.web';
  41. import { isSalesforceEnabled } from '../salesforce/functions';
  42. import { startScreenShareFlow } from '../screen-share/actions.web';
  43. import ShareAudioButton from '../screen-share/components/web/ShareAudioButton';
  44. import { isScreenAudioSupported, isScreenVideoShared } from '../screen-share/functions';
  45. import { useSecurityDialogButton } from '../security/hooks.web';
  46. import SettingsButton from '../settings/components/web/SettingsButton';
  47. import { useSharedVideoButton } from '../shared-video/hooks';
  48. import SpeakerStats from '../speaker-stats/components/web/SpeakerStats';
  49. import { isSpeakerStatsDisabled } from '../speaker-stats/functions';
  50. import { useSpeakerStatsButton } from '../speaker-stats/hooks.web';
  51. import { useClosedCaptionButton } from '../subtitles/hooks.web';
  52. import { toggleTileView } from '../video-layout/actions.any';
  53. import { shouldDisplayTileView } from '../video-layout/functions.web';
  54. import { useTileViewButton } from '../video-layout/hooks';
  55. import VideoQualityButton from '../video-quality/components/VideoQualityButton.web';
  56. import VideoQualityDialog from '../video-quality/components/VideoQualityDialog.web';
  57. import { useVirtualBackgroundButton } from '../virtual-background/hooks';
  58. import { useWhiteboardButton } from '../whiteboard/hooks';
  59. import { setFullScreen } from './actions.web';
  60. import DownloadButton from './components/DownloadButton';
  61. import HelpButton from './components/HelpButton';
  62. import AudioSettingsButton from './components/web/AudioSettingsButton';
  63. import CustomOptionButton from './components/web/CustomOptionButton';
  64. import FullscreenButton from './components/web/FullscreenButton';
  65. import LinkToSalesforceButton from './components/web/LinkToSalesforceButton';
  66. import ProfileButton from './components/web/ProfileButton';
  67. import ShareDesktopButton from './components/web/ShareDesktopButton';
  68. import ToggleCameraButton from './components/web/ToggleCameraButton';
  69. import VideoSettingsButton from './components/web/VideoSettingsButton';
  70. import { isButtonEnabled, isDesktopShareButtonDisabled } from './functions.web';
  71. import { ICustomToolbarButton, IToolboxButton, ToolbarButton } from './types';
  72. const microphone = {
  73. key: 'microphone',
  74. Content: AudioSettingsButton,
  75. group: 0
  76. };
  77. const camera = {
  78. key: 'camera',
  79. Content: VideoSettingsButton,
  80. group: 0
  81. };
  82. const profile = {
  83. key: 'profile',
  84. Content: ProfileButton,
  85. group: 1
  86. };
  87. const chat = {
  88. key: 'chat',
  89. Content: ChatButton,
  90. group: 2
  91. };
  92. const desktop = {
  93. key: 'desktop',
  94. Content: ShareDesktopButton,
  95. group: 2
  96. };
  97. // In Narrow layout and mobile web we are using drawer for popups and that is why it is better to include
  98. // all forms of reactions in the overflow menu. Otherwise the toolbox will be hidden and the reactions popup
  99. // misaligned.
  100. const raisehand = {
  101. key: 'raisehand',
  102. Content: RaiseHandContainerButton,
  103. group: 2
  104. };
  105. const invite = {
  106. key: 'invite',
  107. Content: InviteButton,
  108. group: 2
  109. };
  110. const toggleCamera = {
  111. key: 'toggle-camera',
  112. Content: ToggleCameraButton,
  113. group: 2
  114. };
  115. const videoQuality = {
  116. key: 'videoquality',
  117. Content: VideoQualityButton,
  118. group: 2
  119. };
  120. const fullscreen = {
  121. key: 'fullscreen',
  122. Content: FullscreenButton,
  123. group: 2
  124. };
  125. const linkToSalesforce = {
  126. key: 'linktosalesforce',
  127. Content: LinkToSalesforceButton,
  128. group: 2
  129. };
  130. const shareAudio = {
  131. key: 'shareaudio',
  132. Content: ShareAudioButton,
  133. group: 3
  134. };
  135. const noiseSuppression = {
  136. key: 'noisesuppression',
  137. Content: NoiseSuppressionButton,
  138. group: 3
  139. };
  140. const settings = {
  141. key: 'settings',
  142. Content: SettingsButton,
  143. group: 4
  144. };
  145. const download = {
  146. key: 'download',
  147. Content: DownloadButton,
  148. group: 4
  149. };
  150. const help = {
  151. key: 'help',
  152. Content: HelpButton,
  153. group: 4
  154. };
  155. /**
  156. * A hook that returns the toggle camera button if it is enabled and undefined otherwise.
  157. *
  158. * @returns {Object | undefined}
  159. */
  160. function useToggleCameraButton() {
  161. const toggleCameraEnabled = useSelector(isToggleCameraEnabled);
  162. if (toggleCameraEnabled) {
  163. return toggleCamera;
  164. }
  165. }
  166. /**
  167. * A hook that returns the desktop sharing button if it is enabled and undefined otherwise.
  168. *
  169. * @returns {Object | undefined}
  170. */
  171. function getDesktopSharingButton() {
  172. if (JitsiMeetJS.isDesktopSharingEnabled()) {
  173. return desktop;
  174. }
  175. }
  176. /**
  177. * A hook that returns the fullscreen button if it is enabled and undefined otherwise.
  178. *
  179. * @returns {Object | undefined}
  180. */
  181. function getFullscreenButton() {
  182. if (!isIosMobileBrowser() || isIpadMobileBrowser()) {
  183. return fullscreen;
  184. }
  185. }
  186. /**
  187. * A hook that returns the "link to salesforce" button if it is enabled and undefined otherwise.
  188. *
  189. * @returns {Object | undefined}
  190. */
  191. function useLinkToSalesforceButton() {
  192. const _isSalesforceEnabled = useSelector(isSalesforceEnabled);
  193. if (_isSalesforceEnabled) {
  194. return linkToSalesforce;
  195. }
  196. }
  197. /**
  198. * A hook that returns the share audio button if it is enabled and undefined otherwise.
  199. *
  200. * @returns {Object | undefined}
  201. */
  202. function getShareAudioButton() {
  203. if (JitsiMeetJS.isDesktopSharingEnabled() && isScreenAudioSupported()) {
  204. return shareAudio;
  205. }
  206. }
  207. /**
  208. * A hook that returns the download button if it is enabled and undefined otherwise.
  209. *
  210. * @returns {Object | undefined}
  211. */
  212. function useDownloadButton() {
  213. const visible = useSelector(
  214. (state: IReduxState) => typeof state['features/base/config'].deploymentUrls?.downloadAppsUrl === 'string');
  215. if (visible) {
  216. return download;
  217. }
  218. }
  219. /**
  220. * A hook that returns the help button if it is enabled and undefined otherwise.
  221. *
  222. * @returns {Object | undefined}
  223. */
  224. function useHelpButton() {
  225. const visible = useSelector(
  226. (state: IReduxState) =>
  227. typeof state['features/base/config'].deploymentUrls?.userDocumentationURL === 'string'
  228. && getFeatureFlag(state, HELP_BUTTON_ENABLED, true));
  229. if (visible) {
  230. return help;
  231. }
  232. }
  233. /**
  234. * Returns all buttons that could be rendered.
  235. *
  236. * @param {Object} _customToolbarButtons - An array containing custom buttons objects.
  237. * @returns {Object} The button maps mainMenuButtons and overflowMenuButtons.
  238. */
  239. export function useToolboxButtons(
  240. _customToolbarButtons?: ICustomToolbarButton[]): { [key: string]: IToolboxButton; } {
  241. const dekstopSharing = getDesktopSharingButton();
  242. const toggleCameraButton = useToggleCameraButton();
  243. const _fullscreen = getFullscreenButton();
  244. const security = useSecurityDialogButton();
  245. const reactions = useReactionsButton();
  246. const participants = useParticipantPaneButton();
  247. const tileview = useTileViewButton();
  248. const cc = useClosedCaptionButton();
  249. const recording = useRecordingButton();
  250. const liveStreaming = useLiveStreamingButton();
  251. const linktosalesforce = useLinkToSalesforceButton();
  252. const shareaudio = getShareAudioButton();
  253. const shareVideo = useSharedVideoButton();
  254. const whiteboard = useWhiteboardButton();
  255. const etherpad = useEtherpadButton();
  256. const virtualBackground = useVirtualBackgroundButton();
  257. const speakerStats = useSpeakerStatsButton();
  258. const shortcuts = useKeyboardShortcutsButton();
  259. const embed = useEmbedButton();
  260. const feedback = useFeedbackButton();
  261. const _download = useDownloadButton();
  262. const _help = useHelpButton();
  263. const buttons: { [key in ToolbarButton]?: IToolboxButton; } = {
  264. microphone,
  265. camera,
  266. profile,
  267. desktop: dekstopSharing,
  268. chat,
  269. raisehand,
  270. reactions,
  271. 'participants-pane': participants,
  272. invite,
  273. tileview,
  274. 'toggle-camera': toggleCameraButton,
  275. videoquality: videoQuality,
  276. fullscreen: _fullscreen,
  277. security,
  278. closedcaptions: cc,
  279. recording,
  280. livestreaming: liveStreaming,
  281. linktosalesforce,
  282. sharedvideo: shareVideo,
  283. shareaudio,
  284. noisesuppression: noiseSuppression,
  285. whiteboard,
  286. etherpad,
  287. 'select-background': virtualBackground,
  288. stats: speakerStats,
  289. settings,
  290. shortcuts,
  291. embedmeeting: embed,
  292. feedback,
  293. download: _download,
  294. help: _help
  295. };
  296. const buttonKeys = Object.keys(buttons) as ToolbarButton[];
  297. buttonKeys.forEach(
  298. key => typeof buttons[key] === 'undefined' && delete buttons[key]);
  299. const customButtons = _customToolbarButtons?.reduce((prev, { backgroundColor, icon, id, text }) => {
  300. prev[id] = {
  301. backgroundColor,
  302. key: id,
  303. id,
  304. Content: CustomOptionButton,
  305. group: 4,
  306. icon,
  307. text
  308. };
  309. return prev;
  310. }, {} as { [key: string]: ICustomToolbarButton; });
  311. return {
  312. ...buttons,
  313. ...customButtons
  314. };
  315. }
  316. export const useKeyboardShortcuts = (toolbarButtons: Array<string>) => {
  317. const dispatch = useDispatch();
  318. const _isSpeakerStatsDisabled = useSelector(isSpeakerStatsDisabled);
  319. const _isParticipantsPaneEnabled = useSelector(isParticipantsPaneEnabled);
  320. const _shouldDisplayReactionsButtons = useSelector(shouldDisplayReactionsButtons);
  321. const _toolbarButtons = useSelector(
  322. (state: IReduxState) => toolbarButtons || state['features/toolbox'].toolbarButtons);
  323. const chatOpen = useSelector((state: IReduxState) => state['features/chat'].isOpen);
  324. const desktopSharingButtonDisabled = useSelector(isDesktopShareButtonDisabled);
  325. const desktopSharingEnabled = JitsiMeetJS.isDesktopSharingEnabled();
  326. const fullScreen = useSelector((state: IReduxState) => state['features/toolbox'].fullScreen);
  327. const gifsEnabled = useSelector(isGifEnabled);
  328. const participantsPaneOpen = useSelector(getParticipantsPaneOpen);
  329. const raisedHand = useSelector((state: IReduxState) => hasRaisedHand(getLocalParticipant(state)));
  330. const screenSharing = useSelector(isScreenVideoShared);
  331. const tileViewEnabled = useSelector(shouldDisplayTileView);
  332. /**
  333. * Creates an analytics keyboard shortcut event and dispatches an action for
  334. * toggling the display of chat.
  335. *
  336. * @private
  337. * @returns {void}
  338. */
  339. function onToggleChat() {
  340. sendAnalytics(createShortcutEvent(
  341. 'toggle.chat',
  342. ACTION_SHORTCUT_TRIGGERED,
  343. {
  344. enable: !chatOpen
  345. }));
  346. // Checks if there was any text selected by the user.
  347. // Used for when we press simultaneously keys for copying
  348. // text messages from the chat board
  349. if (window.getSelection()?.toString() !== '') {
  350. return false;
  351. }
  352. dispatch(toggleChat());
  353. }
  354. /**
  355. * Creates an analytics keyboard shortcut event and dispatches an action for
  356. * toggling the display of the participants pane.
  357. *
  358. * @private
  359. * @returns {void}
  360. */
  361. function onToggleParticipantsPane() {
  362. sendAnalytics(createShortcutEvent(
  363. 'toggle.participants-pane',
  364. ACTION_SHORTCUT_TRIGGERED,
  365. {
  366. enable: !participantsPaneOpen
  367. }));
  368. if (participantsPaneOpen) {
  369. dispatch(closeParticipantsPane());
  370. } else {
  371. dispatch(openParticipantsPane());
  372. }
  373. }
  374. /**
  375. * Creates an analytics keyboard shortcut event and dispatches an action for
  376. * toggling the display of Video Quality.
  377. *
  378. * @private
  379. * @returns {void}
  380. */
  381. function onToggleVideoQuality() {
  382. sendAnalytics(createShortcutEvent('video.quality'));
  383. dispatch(toggleDialog(VideoQualityDialog));
  384. }
  385. /**
  386. * Dispatches an action for toggling the tile view.
  387. *
  388. * @private
  389. * @returns {void}
  390. */
  391. function onToggleTileView() {
  392. sendAnalytics(createShortcutEvent(
  393. 'toggle.tileview',
  394. ACTION_SHORTCUT_TRIGGERED,
  395. {
  396. enable: !tileViewEnabled
  397. }));
  398. dispatch(toggleTileView());
  399. }
  400. /**
  401. * Creates an analytics keyboard shortcut event and dispatches an action for
  402. * toggling full screen mode.
  403. *
  404. * @private
  405. * @returns {void}
  406. */
  407. function onToggleFullScreen() {
  408. sendAnalytics(createShortcutEvent(
  409. 'toggle.fullscreen',
  410. ACTION_SHORTCUT_TRIGGERED,
  411. {
  412. enable: !fullScreen
  413. }));
  414. dispatch(setFullScreen(!fullScreen));
  415. }
  416. /**
  417. * Creates an analytics keyboard shortcut event and dispatches an action for
  418. * toggling raise hand.
  419. *
  420. * @private
  421. * @returns {void}
  422. */
  423. function onToggleRaiseHand() {
  424. sendAnalytics(createShortcutEvent(
  425. 'toggle.raise.hand',
  426. ACTION_SHORTCUT_TRIGGERED,
  427. { enable: !raisedHand }));
  428. dispatch(raiseHand(!raisedHand));
  429. }
  430. /**
  431. * Creates an analytics keyboard shortcut event and dispatches an action for
  432. * toggling screensharing.
  433. *
  434. * @private
  435. * @returns {void}
  436. */
  437. function onToggleScreenshare() {
  438. // Ignore the shortcut if the button is disabled.
  439. if (desktopSharingButtonDisabled) {
  440. return;
  441. }
  442. sendAnalytics(createShortcutEvent(
  443. 'toggle.screen.sharing',
  444. ACTION_SHORTCUT_TRIGGERED,
  445. {
  446. enable: !screenSharing
  447. }));
  448. if (desktopSharingEnabled && !desktopSharingButtonDisabled) {
  449. dispatch(startScreenShareFlow(!screenSharing));
  450. }
  451. }
  452. /**
  453. * Creates an analytics keyboard shortcut event and dispatches an action for
  454. * toggling speaker stats.
  455. *
  456. * @private
  457. * @returns {void}
  458. */
  459. function onSpeakerStats() {
  460. sendAnalytics(createShortcutEvent(
  461. 'speaker.stats'
  462. ));
  463. dispatch(toggleDialog(SpeakerStats, {
  464. conference: APP.conference
  465. }));
  466. }
  467. useEffect(() => {
  468. const KEYBOARD_SHORTCUTS = [
  469. isButtonEnabled('videoquality', _toolbarButtons) && {
  470. character: 'A',
  471. exec: onToggleVideoQuality,
  472. helpDescription: 'toolbar.callQuality'
  473. },
  474. isButtonEnabled('chat', _toolbarButtons) && {
  475. character: 'C',
  476. exec: onToggleChat,
  477. helpDescription: 'keyboardShortcuts.toggleChat'
  478. },
  479. isButtonEnabled('desktop', _toolbarButtons) && {
  480. character: 'D',
  481. exec: onToggleScreenshare,
  482. helpDescription: 'keyboardShortcuts.toggleScreensharing'
  483. },
  484. _isParticipantsPaneEnabled && isButtonEnabled('participants-pane', _toolbarButtons) && {
  485. character: 'P',
  486. exec: onToggleParticipantsPane,
  487. helpDescription: 'keyboardShortcuts.toggleParticipantsPane'
  488. },
  489. isButtonEnabled('raisehand', _toolbarButtons) && {
  490. character: 'R',
  491. exec: onToggleRaiseHand,
  492. helpDescription: 'keyboardShortcuts.raiseHand'
  493. },
  494. isButtonEnabled('fullscreen', _toolbarButtons) && {
  495. character: 'S',
  496. exec: onToggleFullScreen,
  497. helpDescription: 'keyboardShortcuts.fullScreen'
  498. },
  499. isButtonEnabled('tileview', _toolbarButtons) && {
  500. character: 'W',
  501. exec: onToggleTileView,
  502. helpDescription: 'toolbar.tileViewToggle'
  503. },
  504. !_isSpeakerStatsDisabled && isButtonEnabled('stats', _toolbarButtons) && {
  505. character: 'T',
  506. exec: onSpeakerStats,
  507. helpDescription: 'keyboardShortcuts.showSpeakerStats'
  508. }
  509. ];
  510. KEYBOARD_SHORTCUTS.forEach(shortcut => {
  511. if (typeof shortcut === 'object') {
  512. dispatch(registerShortcut({
  513. character: shortcut.character,
  514. handler: shortcut.exec,
  515. helpDescription: shortcut.helpDescription
  516. }));
  517. }
  518. });
  519. // If the buttons for sending reactions are not displayed we should disable the shortcuts too.
  520. if (_shouldDisplayReactionsButtons) {
  521. const REACTION_SHORTCUTS = Object.keys(REACTIONS).map(key => {
  522. const onShortcutSendReaction = () => {
  523. dispatch(addReactionToBuffer(key));
  524. sendAnalytics(createShortcutEvent(
  525. `reaction.${key}`
  526. ));
  527. };
  528. return {
  529. character: REACTIONS[key].shortcutChar,
  530. exec: onShortcutSendReaction,
  531. helpDescription: `toolbar.reaction${key.charAt(0).toUpperCase()}${key.slice(1)}`,
  532. altKey: true
  533. };
  534. });
  535. REACTION_SHORTCUTS.forEach(shortcut => {
  536. dispatch(registerShortcut({
  537. alt: shortcut.altKey,
  538. character: shortcut.character,
  539. handler: shortcut.exec,
  540. helpDescription: shortcut.helpDescription
  541. }));
  542. });
  543. if (gifsEnabled) {
  544. const onGifShortcut = () => {
  545. batch(() => {
  546. dispatch(toggleReactionsMenuVisibility());
  547. dispatch(setGifMenuVisibility(true));
  548. });
  549. };
  550. dispatch(registerShortcut({
  551. character: 'G',
  552. handler: onGifShortcut,
  553. helpDescription: 'keyboardShortcuts.giphyMenu'
  554. }));
  555. }
  556. }
  557. return () => {
  558. [ 'A', 'C', 'D', 'P', 'R', 'S', 'W', 'T', 'G' ].forEach(letter =>
  559. dispatch(unregisterShortcut(letter)));
  560. if (_shouldDisplayReactionsButtons) {
  561. Object.keys(REACTIONS).map(key => REACTIONS[key].shortcutChar)
  562. .forEach(letter =>
  563. dispatch(unregisterShortcut(letter, true)));
  564. }
  565. };
  566. }, [
  567. _shouldDisplayReactionsButtons,
  568. chatOpen,
  569. desktopSharingButtonDisabled,
  570. desktopSharingEnabled,
  571. fullScreen,
  572. gifsEnabled,
  573. participantsPaneOpen,
  574. raisedHand,
  575. screenSharing,
  576. tileViewEnabled
  577. ]);
  578. };