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

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