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.

RaiseHandButton.tsx 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import React, { Component } from 'react';
  2. import { Text } from 'react-native-paper';
  3. import { connect } from 'react-redux';
  4. import { createToolbarEvent } from '../../../analytics/AnalyticsEvents';
  5. import { sendAnalytics } from '../../../analytics/functions';
  6. import { IReduxState } from '../../../app/types';
  7. import { RAISE_HAND_ENABLED } from '../../../base/flags/constants';
  8. import { getFeatureFlag } from '../../../base/flags/functions';
  9. import { translate } from '../../../base/i18n/functions';
  10. import { raiseHand } from '../../../base/participants/actions';
  11. import {
  12. getLocalParticipant,
  13. hasRaisedHand
  14. } from '../../../base/participants/functions';
  15. import { ILocalParticipant } from '../../../base/participants/types';
  16. import { IProps as AbstractButtonProps } from '../../../base/toolbox/components/AbstractButton';
  17. import Button from '../../../base/ui/components/native/Button';
  18. import { BUTTON_TYPES } from '../../../base/ui/constants.native';
  19. import styles from './styles';
  20. /**
  21. * The type of the React {@code Component} props of {@link RaiseHandButton}.
  22. */
  23. interface IProps extends AbstractButtonProps {
  24. /**
  25. * Whether this button is enabled or not.
  26. */
  27. _enabled: boolean;
  28. /**
  29. * The local participant.
  30. */
  31. _localParticipant?: ILocalParticipant;
  32. /**
  33. * Whether the participant raused their hand or not.
  34. */
  35. _raisedHand: boolean;
  36. /**
  37. * Whether or not the click is disabled.
  38. */
  39. disableClick?: boolean;
  40. /**
  41. * Used to close the overflow menu after raise hand is clicked.
  42. */
  43. onCancel: Function;
  44. }
  45. /**
  46. * An implementation of a button to raise or lower hand.
  47. */
  48. class RaiseHandButton extends Component<IProps> {
  49. accessibilityLabel = 'toolbar.accessibilityLabel.raiseHand';
  50. label = 'toolbar.raiseYourHand';
  51. toggledLabel = 'toolbar.lowerYourHand';
  52. /**
  53. * Initializes a new {@code RaiseHandButton} instance.
  54. *
  55. * @param {IProps} props - The React {@code Component} props to initialize
  56. * the new {@code RaiseHandButton} instance with.
  57. */
  58. constructor(props: IProps) {
  59. super(props);
  60. // Bind event handlers so they are only bound once per instance.
  61. this._onClick = this._onClick.bind(this);
  62. this._toggleRaisedHand = this._toggleRaisedHand.bind(this);
  63. this._getLabel = this._getLabel.bind(this);
  64. }
  65. /**
  66. * Handles clicking / pressing the button.
  67. *
  68. * @returns {void}
  69. */
  70. _onClick() {
  71. const { disableClick, onCancel } = this.props;
  72. if (disableClick) {
  73. return;
  74. }
  75. this._toggleRaisedHand();
  76. onCancel();
  77. }
  78. /**
  79. * Toggles the rased hand status of the local participant.
  80. *
  81. * @returns {void}
  82. */
  83. _toggleRaisedHand() {
  84. const enable = !this.props._raisedHand;
  85. sendAnalytics(createToolbarEvent('raise.hand', { enable }));
  86. this.props.dispatch(raiseHand(enable));
  87. }
  88. /**
  89. * Gets the current label, taking the toggled state into account. If no
  90. * toggled label is provided, the regular label will also be used in the
  91. * toggled state.
  92. *
  93. * @returns {string}
  94. */
  95. _getLabel() {
  96. const { _raisedHand, t } = this.props;
  97. return t(_raisedHand ? this.toggledLabel : this.label);
  98. }
  99. /**
  100. * Renders the "raise hand" emoji.
  101. *
  102. * @returns {ReactElement}
  103. */
  104. _renderRaiseHandEmoji() {
  105. return (
  106. <Text>✋</Text>
  107. );
  108. }
  109. /**
  110. * Implements React's {@link Component#render()}.
  111. *
  112. * @inheritdoc
  113. * @returns {ReactElement}
  114. */
  115. render() {
  116. const { _enabled } = this.props;
  117. if (!_enabled) {
  118. return null;
  119. }
  120. return (
  121. <Button
  122. accessibilityLabel = { this.accessibilityLabel }
  123. icon = { this._renderRaiseHandEmoji }
  124. labelKey = { this._getLabel() }
  125. onClick = { this._onClick }
  126. style = { styles.raiseHandButton }
  127. type = { BUTTON_TYPES.SECONDARY } />
  128. );
  129. }
  130. }
  131. /**
  132. * Maps part of the Redux state to the props of this component.
  133. *
  134. * @param {Object} state - The Redux state.
  135. * @private
  136. * @returns {IProps}
  137. */
  138. function _mapStateToProps(state: IReduxState) {
  139. const _localParticipant = getLocalParticipant(state);
  140. const enabled = getFeatureFlag(state, RAISE_HAND_ENABLED, true);
  141. return {
  142. _enabled: enabled,
  143. _localParticipant,
  144. _raisedHand: hasRaisedHand(_localParticipant)
  145. };
  146. }
  147. /**
  148. * Maps part of the Redux state to the props of this component.
  149. *
  150. * @param {Object} state - The Redux state.
  151. * @private
  152. * @returns {IProps}
  153. */
  154. function _standaloneMapStateToProps(state: IReduxState) {
  155. const _enabled = getFeatureFlag(state, RAISE_HAND_ENABLED, true);
  156. return {
  157. _enabled
  158. };
  159. }
  160. const StandaloneRaiseHandButton = translate(connect(_standaloneMapStateToProps)(RaiseHandButton));
  161. export { StandaloneRaiseHandButton };
  162. export default translate(connect(_mapStateToProps)(RaiseHandButton));