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.

MeetingParticipants.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // @flow
  2. import React, { useCallback, useRef, useState } from 'react';
  3. import { useTranslation } from 'react-i18next';
  4. import { useDispatch } from 'react-redux';
  5. import { rejectParticipantAudio } from '../../../av-moderation/actions';
  6. import { isToolbarButtonEnabled } from '../../../base/config/functions.web';
  7. import { MEDIA_TYPE } from '../../../base/media';
  8. import {
  9. getParticipantCountWithFake,
  10. getSortedParticipantIds
  11. } from '../../../base/participants';
  12. import { connect } from '../../../base/redux';
  13. import { showOverflowDrawer } from '../../../toolbox/functions';
  14. import { muteRemote } from '../../../video-menu/actions.any';
  15. import { findStyledAncestor, shouldRenderInviteButton } from '../../functions';
  16. import { useParticipantDrawer } from '../../hooks';
  17. import { InviteButton } from './InviteButton';
  18. import MeetingParticipantContextMenu from './MeetingParticipantContextMenu';
  19. import MeetingParticipantItems from './MeetingParticipantItems';
  20. import { Heading, ParticipantContainer } from './styled';
  21. type NullProto = {
  22. [key: string]: any,
  23. __proto__: null
  24. };
  25. type RaiseContext = NullProto | {|
  26. /**
  27. * Target elements against which positioning calculations are made.
  28. */
  29. offsetTarget?: HTMLElement,
  30. /**
  31. * The ID of the participant.
  32. */
  33. participantID ?: string,
  34. |};
  35. const initialState = Object.freeze(Object.create(null));
  36. /**
  37. * Renders the MeetingParticipantList component.
  38. * NOTE: This component is not using useSelector on purpose. The child components MeetingParticipantItem
  39. * and MeetingParticipantContextMenu are using connect. Having those mixed leads to problems.
  40. * When this one was using useSelector and the other two were not -the other two were re-rendered before this one was
  41. * re-rendered, so when participant is leaving, we first re-render the item and menu components,
  42. * throwing errors (closing the page) before removing those components for the participant that left.
  43. *
  44. * @returns {ReactNode} - The component.
  45. */
  46. function MeetingParticipants({ participantsCount, showInviteButton, overflowDrawer, sortedParticipantIds = [] }) {
  47. const dispatch = useDispatch();
  48. const isMouseOverMenu = useRef(false);
  49. const [ raiseContext, setRaiseContext ] = useState < RaiseContext >(initialState);
  50. const { t } = useTranslation();
  51. const lowerMenu = useCallback(() => {
  52. /**
  53. * We are tracking mouse movement over the active participant item and
  54. * the context menu. Due to the order of enter/leave events, we need to
  55. * defer checking if the mouse is over the context menu with
  56. * queueMicrotask
  57. */
  58. window.queueMicrotask(() => {
  59. if (isMouseOverMenu.current) {
  60. return;
  61. }
  62. if (raiseContext !== initialState) {
  63. setRaiseContext(initialState);
  64. }
  65. });
  66. }, [ raiseContext ]);
  67. const raiseMenu = useCallback((participantID, target) => {
  68. setRaiseContext({
  69. participantID,
  70. offsetTarget: findStyledAncestor(target, ParticipantContainer)
  71. });
  72. }, [ raiseContext ]);
  73. const toggleMenu = useCallback(participantID => e => {
  74. const { participantID: raisedParticipant } = raiseContext;
  75. if (raisedParticipant && raisedParticipant === participantID) {
  76. lowerMenu();
  77. } else {
  78. raiseMenu(participantID, e.target);
  79. }
  80. }, [ raiseContext ]);
  81. const menuEnter = useCallback(() => {
  82. isMouseOverMenu.current = true;
  83. }, []);
  84. const menuLeave = useCallback(() => {
  85. isMouseOverMenu.current = false;
  86. lowerMenu();
  87. }, [ lowerMenu ]);
  88. const muteAudio = useCallback(id => () => {
  89. dispatch(muteRemote(id, MEDIA_TYPE.AUDIO));
  90. dispatch(rejectParticipantAudio(id));
  91. }, [ dispatch ]);
  92. const [ drawerParticipant, closeDrawer, openDrawerForParticipant ] = useParticipantDrawer();
  93. // FIXME:
  94. // It seems that useTranslation is not very scallable. Unmount 500 components that have the useTranslation hook is
  95. // taking more than 10s. To workaround the issue we need to pass the texts as props. This is temporary and dirty
  96. // solution!!!
  97. // One potential proper fix would be to use react-window component in order to lower the number of components
  98. // mounted.
  99. const participantActionEllipsisLabel = t('MeetingParticipantItem.ParticipantActionEllipsis.options');
  100. const youText = t('chat.you');
  101. const askUnmuteText = t('participantsPane.actions.askUnmute');
  102. const muteParticipantButtonText = t('dialog.muteParticipantButton');
  103. return (
  104. <>
  105. <Heading>{t('participantsPane.headings.participantsList', { count: participantsCount })}</Heading>
  106. {showInviteButton && <InviteButton />}
  107. <div>
  108. <MeetingParticipantItems
  109. askUnmuteText = { askUnmuteText }
  110. lowerMenu = { lowerMenu }
  111. muteAudio = { muteAudio }
  112. muteParticipantButtonText = { muteParticipantButtonText }
  113. openDrawerForParticipant = { openDrawerForParticipant }
  114. overflowDrawer = { overflowDrawer }
  115. participantActionEllipsisLabel = { participantActionEllipsisLabel }
  116. participantIds = { sortedParticipantIds }
  117. participantsCount = { participantsCount }
  118. raiseContextId = { raiseContext.participantID }
  119. toggleMenu = { toggleMenu }
  120. youText = { youText } />
  121. </div>
  122. <MeetingParticipantContextMenu
  123. closeDrawer = { closeDrawer }
  124. drawerParticipant = { drawerParticipant }
  125. muteAudio = { muteAudio }
  126. onEnter = { menuEnter }
  127. onLeave = { menuLeave }
  128. onSelect = { lowerMenu }
  129. overflowDrawer = { overflowDrawer }
  130. { ...raiseContext } />
  131. </>
  132. );
  133. }
  134. /**
  135. * Maps (parts of) the redux state to the associated props for this component.
  136. *
  137. * @param {Object} state - The Redux state.
  138. * @param {Object} ownProps - The own props of the component.
  139. * @private
  140. * @returns {Props}
  141. */
  142. function _mapStateToProps(state): Object {
  143. const sortedParticipantIds = getSortedParticipantIds(state);
  144. // This is very important as getRemoteParticipants is not changing its reference object
  145. // and we will not re-render on change, but if count changes we will do
  146. const participantsCount = getParticipantCountWithFake(state);
  147. const showInviteButton = shouldRenderInviteButton(state) && isToolbarButtonEnabled('invite', state);
  148. const overflowDrawer = showOverflowDrawer(state);
  149. return {
  150. sortedParticipantIds,
  151. participantsCount,
  152. showInviteButton,
  153. overflowDrawer
  154. };
  155. }
  156. export default connect(_mapStateToProps)(MeetingParticipants);