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.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // @flow
  2. import React, { Component } from 'react';
  3. import AudioMuteButton from '../AudioMuteButton';
  4. import { isAudioSettingsButtonDisabled } from '../../functions';
  5. import { IconArrowDown } from '../../../base/icons';
  6. import JitsiMeetJS from '../../../base/lib-jitsi-meet/_';
  7. import { ToolboxButtonWithIcon } from '../../../base/toolbox';
  8. import { connect } from '../../../base/redux';
  9. import { getMediaPermissionPromptVisibility } from '../../../overlay';
  10. import { AudioSettingsPopup, toggleAudioSettings } from '../../../settings';
  11. type Props = {
  12. /**
  13. * Click handler for the small icon. Opens audio options.
  14. */
  15. onAudioOptionsClick: Function,
  16. /**
  17. * Whether the permission prompt is visible or not.
  18. * Useful for enabling the button on permission grant.
  19. */
  20. permissionPromptVisibility: boolean,
  21. /**
  22. * If the button should be disabled.
  23. */
  24. isDisabled: boolean,
  25. /**
  26. * Flag controlling the visibility of the button.
  27. */
  28. visible: boolean,
  29. };
  30. type State = {
  31. /**
  32. * If there are permissions for audio devices.
  33. */
  34. hasPermissions: boolean,
  35. }
  36. /**
  37. * Button used for audio & audio settings.
  38. *
  39. * @returns {ReactElement}
  40. */
  41. class AudioSettingsButton extends Component<Props, State> {
  42. _isMounted: boolean;
  43. /**
  44. * Initializes a new {@code AudioSettingsButton} instance.
  45. *
  46. * @param {Object} props - The read-only properties with which the new
  47. * instance is to be initialized.
  48. */
  49. constructor(props) {
  50. super(props);
  51. this._isMounted = true;
  52. this.state = {
  53. hasPermissions: false
  54. };
  55. }
  56. /**
  57. * Updates device permissions.
  58. *
  59. * @returns {Promise<void>}
  60. */
  61. async _updatePermissions() {
  62. const hasPermissions = await JitsiMeetJS.mediaDevices.isDevicePermissionGranted(
  63. 'audio',
  64. );
  65. this._isMounted && this.setState({
  66. hasPermissions
  67. });
  68. }
  69. /**
  70. * Implements React's {@link Component#componentDidMount}.
  71. *
  72. * @inheritdoc
  73. */
  74. componentDidMount() {
  75. this._updatePermissions();
  76. }
  77. /**
  78. * Implements React's {@link Component#componentDidUpdate}.
  79. *
  80. * @inheritdoc
  81. */
  82. componentDidUpdate(prevProps) {
  83. if (this.props.permissionPromptVisibility !== prevProps.permissionPromptVisibility) {
  84. this._updatePermissions();
  85. }
  86. }
  87. /**
  88. * Implements React's {@link Component#componentWillUnmount}.
  89. *
  90. * @inheritdoc
  91. */
  92. componentWillUnmount() {
  93. this._isMounted = false;
  94. }
  95. /**
  96. * Implements React's {@link Component#render}.
  97. *
  98. * @inheritdoc
  99. */
  100. render() {
  101. const { isDisabled, onAudioOptionsClick, visible } = this.props;
  102. const settingsDisabled = !this.state.hasPermissions || isDisabled;
  103. return visible ? (
  104. <AudioSettingsPopup>
  105. <ToolboxButtonWithIcon
  106. icon = { IconArrowDown }
  107. iconDisabled = { settingsDisabled }
  108. onIconClick = { onAudioOptionsClick }>
  109. <AudioMuteButton />
  110. </ToolboxButtonWithIcon>
  111. </AudioSettingsPopup>
  112. ) : null;
  113. }
  114. }
  115. /**
  116. * Function that maps parts of Redux state tree into component props.
  117. *
  118. * @param {Object} state - Redux state.
  119. * @returns {Object}
  120. */
  121. function mapStateToProps(state) {
  122. return {
  123. isDisabled: isAudioSettingsButtonDisabled(state),
  124. permissionPromptVisibility: getMediaPermissionPromptVisibility(state)
  125. };
  126. }
  127. const mapDispatchToProps = {
  128. onAudioOptionsClick: toggleAudioSettings
  129. };
  130. export default connect(
  131. mapStateToProps,
  132. mapDispatchToProps,
  133. )(AudioSettingsButton);