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.

ParticipantContextMenu.tsx 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. import React, { useCallback, useMemo } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { useDispatch, useSelector } from 'react-redux';
  4. import { makeStyles } from 'tss-react/mui';
  5. import { IReduxState, IStore } from '../../../app/types';
  6. import { isSupported as isAvModerationSupported } from '../../../av-moderation/functions';
  7. import Avatar from '../../../base/avatar/components/Avatar';
  8. import { isIosMobileBrowser, isMobileBrowser } from '../../../base/environment/utils';
  9. import { MEDIA_TYPE } from '../../../base/media/constants';
  10. import { PARTICIPANT_ROLE } from '../../../base/participants/constants';
  11. import { getLocalParticipant, hasRaisedHand } from '../../../base/participants/functions';
  12. import { IParticipant } from '../../../base/participants/types';
  13. import { isParticipantAudioMuted, isParticipantVideoMuted } from '../../../base/tracks/functions.any';
  14. import ContextMenu from '../../../base/ui/components/web/ContextMenu';
  15. import ContextMenuItemGroup from '../../../base/ui/components/web/ContextMenuItemGroup';
  16. import { getBreakoutRooms, getCurrentRoomId, isInBreakoutRoom } from '../../../breakout-rooms/functions';
  17. import { IRoom } from '../../../breakout-rooms/types';
  18. import { displayVerification } from '../../../e2ee/functions';
  19. import { setVolume } from '../../../filmstrip/actions.web';
  20. import { isStageFilmstripAvailable } from '../../../filmstrip/functions.web';
  21. import { QUICK_ACTION_BUTTON } from '../../../participants-pane/constants';
  22. import { getQuickActionButtonType, isForceMuted } from '../../../participants-pane/functions';
  23. import { requestRemoteControl, stopController } from '../../../remote-control/actions';
  24. import { getParticipantMenuButtonsWithNotifyClick, showOverflowDrawer } from '../../../toolbox/functions.web';
  25. import { NOTIFY_CLICK_MODE } from '../../../toolbox/types';
  26. import { iAmVisitor } from '../../../visitors/functions';
  27. import { PARTICIPANT_MENU_BUTTONS as BUTTONS } from '../../constants';
  28. import AskToUnmuteButton from './AskToUnmuteButton';
  29. import ConnectionStatusButton from './ConnectionStatusButton';
  30. import CustomOptionButton from './CustomOptionButton';
  31. import DemoteToVisitorButton from './DemoteToVisitorButton';
  32. import GrantModeratorButton from './GrantModeratorButton';
  33. import KickButton from './KickButton';
  34. import LowerHandButton from './LowerHandButton';
  35. import MuteButton from './MuteButton';
  36. import MuteEveryoneElseButton from './MuteEveryoneElseButton';
  37. import MuteEveryoneElsesVideoButton from './MuteEveryoneElsesVideoButton';
  38. import MuteVideoButton from './MuteVideoButton';
  39. import PrivateMessageMenuButton from './PrivateMessageMenuButton';
  40. import RemoteControlButton, { REMOTE_CONTROL_MENU_STATES } from './RemoteControlButton';
  41. import SendToRoomButton from './SendToRoomButton';
  42. import TogglePinToStageButton from './TogglePinToStageButton';
  43. import VerifyParticipantButton from './VerifyParticipantButton';
  44. import VolumeSlider from './VolumeSlider';
  45. interface IProps {
  46. /**
  47. * Class name for the context menu.
  48. */
  49. className?: string;
  50. /**
  51. * Closes a drawer if open.
  52. */
  53. closeDrawer?: () => void;
  54. /**
  55. * The participant for which the drawer is open.
  56. * It contains the displayName & participantID.
  57. */
  58. drawerParticipant?: {
  59. displayName: string;
  60. participantID: string;
  61. };
  62. /**
  63. * Target elements against which positioning calculations are made.
  64. */
  65. offsetTarget?: HTMLElement;
  66. /**
  67. * Callback for the mouse entering the component.
  68. */
  69. onEnter?: (e?: React.MouseEvent) => void;
  70. /**
  71. * Callback for the mouse leaving the component.
  72. */
  73. onLeave?: (e?: React.MouseEvent) => void;
  74. /**
  75. * Callback for making a selection in the menu.
  76. */
  77. onSelect: (value?: boolean | React.MouseEvent) => void;
  78. /**
  79. * Participant reference.
  80. */
  81. participant: IParticipant;
  82. /**
  83. * The current state of the participant's remote control session.
  84. */
  85. remoteControlState?: number;
  86. /**
  87. * Whether or not the menu is displayed in the thumbnail remote video menu.
  88. */
  89. thumbnailMenu?: boolean;
  90. }
  91. const useStyles = makeStyles()(theme => {
  92. return {
  93. text: {
  94. color: theme.palette.text02,
  95. padding: '10px 16px',
  96. height: '40px',
  97. overflow: 'hidden',
  98. display: 'flex',
  99. alignItems: 'center',
  100. boxSizing: 'border-box'
  101. }
  102. };
  103. });
  104. const ParticipantContextMenu = ({
  105. className,
  106. closeDrawer,
  107. drawerParticipant,
  108. offsetTarget,
  109. onEnter,
  110. onLeave,
  111. onSelect,
  112. participant,
  113. remoteControlState,
  114. thumbnailMenu
  115. }: IProps) => {
  116. const dispatch: IStore['dispatch'] = useDispatch();
  117. const { t } = useTranslation();
  118. const { classes: styles } = useStyles();
  119. const localParticipant = useSelector(getLocalParticipant);
  120. const _isModerator = Boolean(localParticipant?.role === PARTICIPANT_ROLE.MODERATOR);
  121. const _isVideoForceMuted = useSelector<IReduxState>(state =>
  122. isForceMuted(participant, MEDIA_TYPE.VIDEO, state));
  123. const _isAudioMuted = useSelector((state: IReduxState) => isParticipantAudioMuted(participant, state));
  124. const _isVideoMuted = useSelector((state: IReduxState) => isParticipantVideoMuted(participant, state));
  125. const _overflowDrawer: boolean = useSelector(showOverflowDrawer);
  126. const { remoteVideoMenu = {}, disableRemoteMute, startSilent, customParticipantMenuButtons }
  127. = useSelector((state: IReduxState) => state['features/base/config']);
  128. const visitorsMode = useSelector((state: IReduxState) => iAmVisitor(state));
  129. const visitorsSupported = useSelector((state: IReduxState) => state['features/visitors'].supported);
  130. const { disableDemote, disableKick, disableGrantModerator, disablePrivateChat } = remoteVideoMenu;
  131. const { participantsVolume } = useSelector((state: IReduxState) => state['features/filmstrip']);
  132. const _volume = (participant?.local ?? true ? undefined
  133. : participant?.id ? participantsVolume[participant?.id] : undefined) ?? 1;
  134. const isBreakoutRoom = useSelector(isInBreakoutRoom);
  135. const isModerationSupported = useSelector((state: IReduxState) => isAvModerationSupported()(state));
  136. const raisedHands = hasRaisedHand(participant);
  137. const stageFilmstrip = useSelector(isStageFilmstripAvailable);
  138. const shouldDisplayVerification = useSelector((state: IReduxState) => displayVerification(state, participant?.id));
  139. const buttonsWithNotifyClick = useSelector(getParticipantMenuButtonsWithNotifyClick);
  140. const _currentRoomId = useSelector(getCurrentRoomId);
  141. const _rooms: IRoom[] = Object.values(useSelector(getBreakoutRooms));
  142. const _onVolumeChange = useCallback(value => {
  143. dispatch(setVolume(participant.id, value));
  144. }, [ setVolume, dispatch ]);
  145. const _getCurrentParticipantId = useCallback(() => {
  146. const drawer = _overflowDrawer && !thumbnailMenu;
  147. return (drawer ? drawerParticipant?.participantID : participant?.id) ?? '';
  148. }
  149. , [ thumbnailMenu, _overflowDrawer, drawerParticipant, participant ]);
  150. const notifyClick = useCallback(
  151. (buttonKey: string) => {
  152. const notifyMode = buttonsWithNotifyClick?.get(buttonKey);
  153. if (!notifyMode) {
  154. return;
  155. }
  156. APP.API.notifyParticipantMenuButtonClicked(
  157. buttonKey,
  158. _getCurrentParticipantId(),
  159. notifyMode === NOTIFY_CLICK_MODE.PREVENT_AND_NOTIFY
  160. );
  161. }, [ buttonsWithNotifyClick, _getCurrentParticipantId ]);
  162. const onBreakoutRoomButtonClick = useCallback(() => {
  163. onSelect(true);
  164. }, [ onSelect ]);
  165. const isClickedFromParticipantPane = useMemo(
  166. () => !_overflowDrawer && !thumbnailMenu,
  167. [ _overflowDrawer, thumbnailMenu ]);
  168. const quickActionButtonType = useSelector((state: IReduxState) =>
  169. getQuickActionButtonType(participant, _isAudioMuted, _isVideoMuted, state));
  170. const buttons: JSX.Element[] = [];
  171. const buttons2: JSX.Element[] = [];
  172. const showVolumeSlider = !startSilent
  173. && !isIosMobileBrowser()
  174. && (_overflowDrawer || thumbnailMenu)
  175. && typeof _volume === 'number'
  176. && !isNaN(_volume);
  177. const getButtonProps = useCallback((key: string) => {
  178. const notifyMode = buttonsWithNotifyClick?.get(key);
  179. const shouldNotifyClick = typeof notifyMode !== 'undefined';
  180. return {
  181. key,
  182. notifyMode,
  183. notifyClick: shouldNotifyClick ? () => notifyClick(key) : undefined,
  184. participantID: _getCurrentParticipantId()
  185. };
  186. }, [ _getCurrentParticipantId, buttonsWithNotifyClick, notifyClick ]);
  187. if (_isModerator) {
  188. if (isModerationSupported) {
  189. if (_isAudioMuted && !participant.isSilent
  190. && !(isClickedFromParticipantPane && quickActionButtonType === QUICK_ACTION_BUTTON.ASK_TO_UNMUTE)) {
  191. buttons.push(<AskToUnmuteButton
  192. { ...getButtonProps(BUTTONS.ASK_UNMUTE) }
  193. buttonType = { MEDIA_TYPE.AUDIO } />
  194. );
  195. }
  196. if (_isVideoForceMuted
  197. && !(isClickedFromParticipantPane && quickActionButtonType === QUICK_ACTION_BUTTON.ALLOW_VIDEO)) {
  198. buttons.push(<AskToUnmuteButton
  199. { ...getButtonProps(BUTTONS.ALLOW_VIDEO) }
  200. buttonType = { MEDIA_TYPE.VIDEO } />
  201. );
  202. }
  203. }
  204. if (!disableRemoteMute && !participant.isSilent) {
  205. if (!(isClickedFromParticipantPane && quickActionButtonType === QUICK_ACTION_BUTTON.MUTE)) {
  206. buttons.push(<MuteButton { ...getButtonProps(BUTTONS.MUTE) } />);
  207. }
  208. buttons.push(<MuteEveryoneElseButton { ...getButtonProps(BUTTONS.MUTE_OTHERS) } />);
  209. if (!(isClickedFromParticipantPane && quickActionButtonType === QUICK_ACTION_BUTTON.STOP_VIDEO)) {
  210. buttons.push(<MuteVideoButton { ...getButtonProps(BUTTONS.MUTE_VIDEO) } />);
  211. }
  212. buttons.push(<MuteEveryoneElsesVideoButton { ...getButtonProps(BUTTONS.MUTE_OTHERS_VIDEO) } />);
  213. }
  214. if (raisedHands) {
  215. buttons2.push(<LowerHandButton { ...getButtonProps(BUTTONS.LOWER_PARTICIPANT_HAND) } />);
  216. }
  217. if (!disableGrantModerator && !isBreakoutRoom) {
  218. buttons2.push(<GrantModeratorButton { ...getButtonProps(BUTTONS.GRANT_MODERATOR) } />);
  219. }
  220. if (!disableDemote && visitorsSupported && _isModerator) {
  221. buttons2.push(<DemoteToVisitorButton { ...getButtonProps(BUTTONS.DEMOTE) } />);
  222. }
  223. if (!disableKick) {
  224. buttons2.push(<KickButton { ...getButtonProps(BUTTONS.KICK) } />);
  225. }
  226. if (shouldDisplayVerification) {
  227. buttons2.push(<VerifyParticipantButton { ...getButtonProps(BUTTONS.VERIFY) } />);
  228. }
  229. }
  230. if (stageFilmstrip) {
  231. buttons2.push(<TogglePinToStageButton { ...getButtonProps(BUTTONS.PIN_TO_STAGE) } />);
  232. }
  233. if (!disablePrivateChat && !visitorsMode) {
  234. buttons2.push(<PrivateMessageMenuButton { ...getButtonProps(BUTTONS.PRIVATE_MESSAGE) } />);
  235. }
  236. if (thumbnailMenu && isMobileBrowser()) {
  237. buttons2.push(<ConnectionStatusButton { ...getButtonProps(BUTTONS.CONN_STATUS) } />);
  238. }
  239. if (thumbnailMenu && remoteControlState) {
  240. const onRemoteControlToggle = useCallback(() => {
  241. if (remoteControlState === REMOTE_CONTROL_MENU_STATES.STARTED) {
  242. dispatch(stopController(true));
  243. } else if (remoteControlState === REMOTE_CONTROL_MENU_STATES.NOT_STARTED) {
  244. dispatch(requestRemoteControl(_getCurrentParticipantId()));
  245. }
  246. }, [ dispatch, remoteControlState, stopController, requestRemoteControl ]);
  247. buttons2.push(<RemoteControlButton
  248. { ...getButtonProps(BUTTONS.REMOTE_CONTROL) }
  249. onClick = { onRemoteControlToggle }
  250. remoteControlState = { remoteControlState } />
  251. );
  252. }
  253. if (customParticipantMenuButtons) {
  254. customParticipantMenuButtons.forEach(
  255. ({ icon, id, text }) => {
  256. buttons2.push(
  257. <CustomOptionButton
  258. icon = { icon }
  259. key = { id }
  260. // eslint-disable-next-line react/jsx-no-bind
  261. onClick = { () => notifyClick(id) }
  262. text = { text } />
  263. );
  264. }
  265. );
  266. }
  267. const breakoutRoomsButtons: any = [];
  268. if (!thumbnailMenu && _isModerator) {
  269. _rooms.forEach(room => {
  270. if (room.id !== _currentRoomId) {
  271. breakoutRoomsButtons.push(
  272. <SendToRoomButton
  273. { ...getButtonProps(BUTTONS.SEND_PARTICIPANT_TO_ROOM) }
  274. key = { room.id }
  275. onClick = { onBreakoutRoomButtonClick }
  276. room = { room } />
  277. );
  278. }
  279. });
  280. }
  281. return (
  282. <ContextMenu
  283. className = { className }
  284. entity = { participant }
  285. hidden = { thumbnailMenu ? false : undefined }
  286. inDrawer = { thumbnailMenu && _overflowDrawer }
  287. isDrawerOpen = { Boolean(drawerParticipant) }
  288. offsetTarget = { offsetTarget }
  289. onClick = { onSelect }
  290. onDrawerClose = { thumbnailMenu ? onSelect : closeDrawer }
  291. onMouseEnter = { onEnter }
  292. onMouseLeave = { onLeave }>
  293. {!thumbnailMenu && _overflowDrawer && drawerParticipant && <ContextMenuItemGroup
  294. actions = { [ {
  295. accessibilityLabel: drawerParticipant.displayName,
  296. customIcon: <Avatar
  297. participantId = { drawerParticipant.participantID }
  298. size = { 20 } />,
  299. text: drawerParticipant.displayName
  300. } ] } />}
  301. {buttons.length > 0 && (
  302. <ContextMenuItemGroup>
  303. {buttons}
  304. </ContextMenuItemGroup>
  305. )}
  306. <ContextMenuItemGroup>
  307. {buttons2}
  308. </ContextMenuItemGroup>
  309. {showVolumeSlider && (
  310. <ContextMenuItemGroup>
  311. <VolumeSlider
  312. initialValue = { _volume }
  313. key = 'volume-slider'
  314. onChange = { _onVolumeChange } />
  315. </ContextMenuItemGroup>
  316. )}
  317. {breakoutRoomsButtons.length > 0 && (
  318. <ContextMenuItemGroup>
  319. <div className = { styles.text }>
  320. {t('breakoutRooms.actions.sendToBreakoutRoom')}
  321. </div>
  322. {breakoutRoomsButtons}
  323. </ContextMenuItemGroup>
  324. )}
  325. </ContextMenu>
  326. );
  327. };
  328. export default ParticipantContextMenu;