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

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