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.

AudioSettingsButton.tsx 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import React, { Component } from 'react';
  2. import { WithTranslation } from 'react-i18next';
  3. import { connect } from 'react-redux';
  4. import { IReduxState } from '../../../app/types';
  5. import { isMobileBrowser } from '../../../base/environment/utils';
  6. import { translate } from '../../../base/i18n/functions';
  7. import { IconArrowUp } from '../../../base/icons/svg';
  8. import JitsiMeetJS from '../../../base/lib-jitsi-meet/_';
  9. import { IGUMPendingState } from '../../../base/media/types';
  10. import ToolboxButtonWithIcon from '../../../base/toolbox/components/web/ToolboxButtonWithIcon';
  11. import { toggleAudioSettings } from '../../../settings/actions.web';
  12. import AudioSettingsPopup from '../../../settings/components/web/audio/AudioSettingsPopup';
  13. import { getAudioSettingsVisibility } from '../../../settings/functions.web';
  14. import { isAudioSettingsButtonDisabled } from '../../functions.web';
  15. import AudioMuteButton from './AudioMuteButton';
  16. interface IProps extends WithTranslation {
  17. /**
  18. * The button's key.
  19. */
  20. buttonKey?: string;
  21. /**
  22. * The gumPending state from redux.
  23. */
  24. gumPending: IGUMPendingState;
  25. /**
  26. * External handler for click action.
  27. */
  28. handleClick: Function;
  29. /**
  30. * Indicates whether audio permissions have been granted or denied.
  31. */
  32. hasPermissions: boolean;
  33. /**
  34. * If the button should be disabled.
  35. */
  36. isDisabled: boolean;
  37. /**
  38. * Defines is popup is open.
  39. */
  40. isOpen: boolean;
  41. /**
  42. * Notify mode for `toolbarButtonClicked` event -
  43. * whether to only notify or to also prevent button click routine.
  44. */
  45. notifyMode?: string;
  46. /**
  47. * Click handler for the small icon. Opens audio options.
  48. */
  49. onAudioOptionsClick: Function;
  50. /**
  51. * Flag controlling the visibility of the button.
  52. * AudioSettings popup is disabled on mobile browsers.
  53. */
  54. visible: boolean;
  55. }
  56. /**
  57. * Button used for audio & audio settings.
  58. *
  59. * @returns {ReactElement}
  60. */
  61. class AudioSettingsButton extends Component<IProps> {
  62. /**
  63. * Initializes a new {@code AudioSettingsButton} instance.
  64. *
  65. * @inheritdoc
  66. */
  67. constructor(props: IProps) {
  68. super(props);
  69. this._onEscClick = this._onEscClick.bind(this);
  70. this._onClick = this._onClick.bind(this);
  71. }
  72. /**
  73. * Click handler for the more actions entries.
  74. *
  75. * @param {KeyboardEvent} event - Esc key click to close the popup.
  76. * @returns {void}
  77. */
  78. _onEscClick(event: React.KeyboardEvent) {
  79. if (event.key === 'Escape' && this.props.isOpen) {
  80. event.preventDefault();
  81. event.stopPropagation();
  82. this._onClick();
  83. }
  84. }
  85. /**
  86. * Click handler for the more actions entries.
  87. *
  88. * @param {MouseEvent} e - Mouse event.
  89. * @returns {void}
  90. */
  91. _onClick(e?: React.MouseEvent) {
  92. const { onAudioOptionsClick, isOpen } = this.props;
  93. if (isOpen) {
  94. e?.stopPropagation();
  95. }
  96. onAudioOptionsClick();
  97. }
  98. /**
  99. * Implements React's {@link Component#render}.
  100. *
  101. * @inheritdoc
  102. */
  103. render() {
  104. const { gumPending, hasPermissions, isDisabled, visible, isOpen, buttonKey, notifyMode, t } = this.props;
  105. const settingsDisabled = !hasPermissions
  106. || isDisabled
  107. || !JitsiMeetJS.mediaDevices.isMultipleAudioInputSupported();
  108. return visible ? (
  109. <AudioSettingsPopup>
  110. <ToolboxButtonWithIcon
  111. ariaControls = 'audio-settings-dialog'
  112. ariaExpanded = { isOpen }
  113. ariaHasPopup = { true }
  114. ariaLabel = { t('toolbar.audioSettings') }
  115. buttonKey = { buttonKey }
  116. icon = { IconArrowUp }
  117. iconDisabled = { settingsDisabled || gumPending !== IGUMPendingState.NONE }
  118. iconId = 'audio-settings-button'
  119. iconTooltip = { t('toolbar.audioSettings') }
  120. notifyMode = { notifyMode }
  121. onIconClick = { this._onClick }
  122. onIconKeyDown = { this._onEscClick }>
  123. <AudioMuteButton
  124. buttonKey = { buttonKey }
  125. notifyMode = { notifyMode } />
  126. </ToolboxButtonWithIcon>
  127. </AudioSettingsPopup>
  128. ) : <AudioMuteButton
  129. buttonKey = { buttonKey }
  130. notifyMode = { notifyMode } />;
  131. }
  132. }
  133. /**
  134. * Function that maps parts of Redux state tree into component props.
  135. *
  136. * @param {Object} state - Redux state.
  137. * @returns {Object}
  138. */
  139. function mapStateToProps(state: IReduxState) {
  140. const { permissions = { audio: false } } = state['features/base/devices'];
  141. const { isNarrowLayout } = state['features/base/responsive-ui'];
  142. const { gumPending } = state['features/base/media'].audio;
  143. return {
  144. gumPending,
  145. hasPermissions: permissions.audio,
  146. isDisabled: Boolean(isAudioSettingsButtonDisabled(state)),
  147. isOpen: Boolean(getAudioSettingsVisibility(state)),
  148. visible: !isMobileBrowser() && !isNarrowLayout
  149. };
  150. }
  151. const mapDispatchToProps = {
  152. onAudioOptionsClick: toggleAudioSettings
  153. };
  154. export default translate(connect(
  155. mapStateToProps,
  156. mapDispatchToProps
  157. )(AudioSettingsButton));