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.

ReactionsMenuButton.tsx 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import React, { ReactElement, useCallback } from 'react';
  2. import { WithTranslation } from 'react-i18next';
  3. import { connect } from 'react-redux';
  4. import { IReduxState, IStore } from '../../../app/types';
  5. import { isMobileBrowser } from '../../../base/environment/utils';
  6. import { translate } from '../../../base/i18n/functions';
  7. import { IconArrowUp, IconFaceSmile } from '../../../base/icons/svg';
  8. import AbstractButton, { type IProps as AbstractButtonProps } from '../../../base/toolbox/components/AbstractButton';
  9. import ToolboxButtonWithPopup from '../../../base/toolbox/components/web/ToolboxButtonWithPopup';
  10. import { toggleReactionsMenuVisibility } from '../../actions.web';
  11. import { IReactionEmojiProps } from '../../constants';
  12. import { getReactionsQueue, isReactionsEnabled } from '../../functions.any';
  13. import { getReactionsMenuVisibility, isReactionsButtonEnabled } from '../../functions.web';
  14. import { IReactionsMenuParent } from '../../types';
  15. import RaiseHandButton from './RaiseHandButton';
  16. import ReactionEmoji from './ReactionEmoji';
  17. import ReactionsMenu from './ReactionsMenu';
  18. interface IProps extends WithTranslation {
  19. /**
  20. * Whether a mobile browser is used or not.
  21. */
  22. _isMobile: boolean;
  23. /**
  24. * Whether the reactions should be displayed on separate button or not.
  25. */
  26. _reactionsButtonEnabled: boolean;
  27. /**
  28. * Whether or not the reactions are enabled.
  29. */
  30. _reactionsEnabled: boolean;
  31. /**
  32. * The button's key.
  33. */
  34. buttonKey?: string;
  35. /**
  36. * Redux dispatch function.
  37. */
  38. dispatch: IStore['dispatch'];
  39. /**
  40. * Whether or not it's narrow mode or mobile browser.
  41. */
  42. isNarrow: boolean;
  43. /**
  44. * Whether or not the reactions menu is open.
  45. */
  46. isOpen: boolean;
  47. /**
  48. * Notify mode for `toolbarButtonClicked` event -
  49. * whether to only notify or to also prevent button click routine.
  50. */
  51. notifyMode?: string;
  52. /**
  53. * The array of reactions to be displayed.
  54. */
  55. reactionsQueue: Array<IReactionEmojiProps>;
  56. /**
  57. * Whether or not to show the raise hand button.
  58. */
  59. showRaiseHand?: boolean;
  60. }
  61. /**
  62. * Implementation of a button for reactions.
  63. */
  64. class ReactionsButtonImpl extends AbstractButton<AbstractButtonProps> {
  65. accessibilityLabel = 'toolbar.accessibilityLabel.reactions';
  66. icon = IconFaceSmile;
  67. label = 'toolbar.reactions';
  68. toggledLabel = 'toolbar.reactions';
  69. tooltip = 'toolbar.reactions';
  70. }
  71. const ReactionsButton = translate(connect()(ReactionsButtonImpl));
  72. /**
  73. * Button used for the reactions menu.
  74. *
  75. * @returns {ReactElement}
  76. */
  77. function ReactionsMenuButton({
  78. _reactionsButtonEnabled,
  79. _reactionsEnabled,
  80. _isMobile,
  81. buttonKey,
  82. dispatch,
  83. isOpen,
  84. isNarrow,
  85. notifyMode,
  86. reactionsQueue,
  87. showRaiseHand,
  88. t
  89. }: IProps) {
  90. const toggleReactionsMenu = useCallback(() => {
  91. dispatch(toggleReactionsMenuVisibility());
  92. }, [ dispatch ]);
  93. const openReactionsMenu = useCallback(() => {
  94. !isOpen && toggleReactionsMenu();
  95. }, [ isOpen, toggleReactionsMenu ]);
  96. const closeReactionsMenu = useCallback(() => {
  97. isOpen && toggleReactionsMenu();
  98. }, [ isOpen, toggleReactionsMenu ]);
  99. if (!showRaiseHand && (!_reactionsButtonEnabled || !_reactionsEnabled)) {
  100. return null;
  101. }
  102. const reactionsMenu = (<div className = 'reactions-menu-container'>
  103. <ReactionsMenu parent = { IReactionsMenuParent.Button } />
  104. </div>);
  105. let content: ReactElement | null = null;
  106. if (showRaiseHand) {
  107. content = isNarrow
  108. ? (
  109. <RaiseHandButton
  110. buttonKey = { buttonKey }
  111. notifyMode = { notifyMode } />)
  112. : (
  113. <ToolboxButtonWithPopup
  114. ariaLabel = { t('toolbar.accessibilityLabel.reactionsMenu') }
  115. icon = { IconArrowUp }
  116. iconDisabled = { false }
  117. onPopoverClose = { toggleReactionsMenu }
  118. onPopoverOpen = { openReactionsMenu }
  119. popoverContent = { reactionsMenu }
  120. visible = { isOpen }>
  121. <RaiseHandButton
  122. buttonKey = { buttonKey }
  123. notifyMode = { notifyMode } />
  124. </ToolboxButtonWithPopup>);
  125. } else {
  126. content = (
  127. <ToolboxButtonWithPopup
  128. ariaLabel = { t('toolbar.accessibilityLabel.reactionsMenu') }
  129. onPopoverClose = { closeReactionsMenu }
  130. onPopoverOpen = { openReactionsMenu }
  131. popoverContent = { reactionsMenu }
  132. trigger = { _isMobile ? 'click' : undefined }
  133. visible = { isOpen }>
  134. <ReactionsButton
  135. buttonKey = { buttonKey }
  136. notifyMode = { notifyMode } />
  137. </ToolboxButtonWithPopup>);
  138. }
  139. return (
  140. <div className = 'reactions-menu-popup-container'>
  141. { content }
  142. {reactionsQueue.map(({ reaction, uid }, index) => (<ReactionEmoji
  143. index = { index }
  144. key = { uid }
  145. reaction = { reaction }
  146. uid = { uid } />))}
  147. </div>
  148. );
  149. }
  150. /**
  151. * Function that maps parts of Redux state tree into component props.
  152. *
  153. * @param {Object} state - Redux state.
  154. * @returns {Object}
  155. */
  156. function mapStateToProps(state: IReduxState) {
  157. const { isNarrowLayout } = state['features/base/responsive-ui'];
  158. return {
  159. _reactionsButtonEnabled: isReactionsButtonEnabled(state),
  160. _reactionsEnabled: isReactionsEnabled(state),
  161. _isMobile: isMobileBrowser(),
  162. isOpen: getReactionsMenuVisibility(state),
  163. isNarrow: isNarrowLayout,
  164. reactionsQueue: getReactionsQueue(state)
  165. };
  166. }
  167. export default translate(connect(mapStateToProps)(ReactionsMenuButton));