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.

AudioMuteButton.web.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // @flow
  2. import PropTypes from 'prop-types';
  3. import React from 'react';
  4. import { connect } from 'react-redux';
  5. import UIEvents from '../../../../../service/UI/UIEvents';
  6. import {
  7. ACTION_SHORTCUT_TRIGGERED,
  8. AUDIO_MUTE,
  9. createShortcutEvent,
  10. sendAnalytics
  11. } from '../../../analytics';
  12. import { translate } from '../../../base/i18n';
  13. import { MEDIA_TYPE } from '../../../base/media';
  14. import { isLocalTrackMuted } from '../../../base/tracks';
  15. import AbstractAudioMuteButton from './AbstractAudioMuteButton';
  16. import ToolbarButtonV2 from '../ToolbarButtonV2';
  17. declare var APP: Object;
  18. /**
  19. * Component that renders a toolbar button for toggling audio mute.
  20. *
  21. * @extends Component
  22. */
  23. export class AudioMuteButton extends AbstractAudioMuteButton {
  24. /**
  25. * Default values for {@code AudioMuteButton} component's properties.
  26. *
  27. * @static
  28. */
  29. static defaultProps = {
  30. tooltipPosition: 'top'
  31. };
  32. /**
  33. * {@code AudioMuteButton} component's property types.
  34. *
  35. * @static
  36. */
  37. static propTypes = {
  38. ...AbstractAudioMuteButton.propTypes,
  39. /**
  40. * The {@code JitsiConference} for the current conference.
  41. */
  42. _conference: PropTypes.object,
  43. /**
  44. * Invoked to update the audio mute status.
  45. */
  46. dispatch: PropTypes.func,
  47. /**
  48. * Invoked to obtain translated strings.
  49. */
  50. t: PropTypes.func,
  51. /**
  52. * Where the tooltip should display, relative to the button.
  53. */
  54. tooltipPosition: PropTypes.string
  55. };
  56. /**
  57. * Initializes a new {@code AudioMuteButton} instance.
  58. *
  59. * @param {Props} props - The read-only React {@code Component} props with
  60. * which the new instance is to be initialized.
  61. */
  62. constructor(props: Object) {
  63. super(props);
  64. // Bind event handlers so it is only bound once per instance.
  65. this._onShortcutToggleAudio = this._onShortcutToggleAudio.bind(this);
  66. }
  67. /**
  68. * Sets a keyboard shortcuts for toggling audio mute.
  69. *
  70. * @inheritdoc
  71. * @returns {void}
  72. */
  73. componentDidMount() {
  74. APP.keyboardshortcut.registerShortcut(
  75. 'M',
  76. null,
  77. this._onShortcutToggleAudio,
  78. 'keyboardShortcuts.mute');
  79. }
  80. /**
  81. * Removes the registered keyboard shortcut handler.
  82. *
  83. * @inheritdoc
  84. * @returns {void}
  85. */
  86. componentWillUnmount() {
  87. APP.keyboardshortcut.unregisterShortcut('M');
  88. }
  89. /**
  90. * Implements React's {@link Component#render()}.
  91. *
  92. * @inheritdoc
  93. * @returns {ReactElement}
  94. */
  95. render() {
  96. const { _audioMuted, _conference, t, tooltipPosition } = this.props;
  97. return (
  98. <ToolbarButtonV2
  99. iconName = { _audioMuted && _conference
  100. ? 'icon-mic-disabled toggled'
  101. : 'icon-microphone' }
  102. onClick = { this._onToolbarToggleAudio }
  103. tooltip = { t('toolbar.mute') }
  104. tooltipPosition = { tooltipPosition } />
  105. );
  106. }
  107. _doToggleAudio: () => void;
  108. /**
  109. * Emits an event to signal audio mute should be toggled.
  110. *
  111. * @private
  112. * @returns {void}
  113. */
  114. _doToggleAudio() {
  115. // The old conference logic must be used for now as the redux flows do
  116. // not handle all cases, such as unmuting when the config
  117. // startWithAudioMuted is true.
  118. APP.UI.emitEvent(UIEvents.AUDIO_MUTED, !this.props._audioMuted, true);
  119. }
  120. _onShortcutToggleAudio: () => void;
  121. /**
  122. * Creates an analytics keyboard shortcut event and dispatches an action for
  123. * toggling audio mute.
  124. *
  125. * @private
  126. * @returns {void}
  127. */
  128. _onShortcutToggleAudio() {
  129. sendAnalytics(createShortcutEvent(
  130. AUDIO_MUTE,
  131. ACTION_SHORTCUT_TRIGGERED,
  132. { enable: !this.props._audioMuted }));
  133. this._doToggleAudio();
  134. }
  135. _onToolbarToggleAudio: () => void;
  136. }
  137. /**
  138. * Maps (parts of) the Redux state to the associated props for the
  139. * {@code AudioMuteButton} component.
  140. *
  141. * @param {Object} state - The Redux state.
  142. * @private
  143. * @returns {{
  144. * _audioMuted: boolean,
  145. * _conference: Object,
  146. * }}
  147. */
  148. function _mapStateToProps(state) {
  149. const tracks = state['features/base/tracks'];
  150. return {
  151. _audioMuted: isLocalTrackMuted(tracks, MEDIA_TYPE.AUDIO),
  152. _conference: state['features/base/conference'].conference
  153. };
  154. }
  155. export default translate(connect(_mapStateToProps)(AudioMuteButton));