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.6KB

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