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.

VideoSettingsButton.js 4.1KB

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