Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

functions.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // @flow
  2. import {
  3. isParticipantApproved,
  4. isEnabledFromState,
  5. isLocalParticipantApprovedFromState,
  6. isSupported
  7. } from '../av-moderation/functions';
  8. import { getFeatureFlag, INVITE_ENABLED } from '../base/flags';
  9. import { MEDIA_TYPE, type MediaType } from '../base/media/constants';
  10. import {
  11. getDominantSpeakerParticipant,
  12. isLocalParticipantModerator,
  13. isParticipantModerator,
  14. getLocalParticipant,
  15. getRemoteParticipantsSorted,
  16. getRaiseHandsQueue
  17. } from '../base/participants/functions';
  18. import { toState } from '../base/redux';
  19. import { isInBreakoutRoom } from '../breakout-rooms/functions';
  20. import { QUICK_ACTION_BUTTON, REDUCER_KEY, MEDIA_STATE } from './constants';
  21. /**
  22. * Generates a class attribute value.
  23. *
  24. * @param {Iterable<string>} args - String iterable.
  25. * @returns {string} Class attribute value.
  26. */
  27. export const classList = (...args: Array<string | boolean>) => args.filter(Boolean).join(' ');
  28. /**
  29. * Find the first styled ancestor component of an element.
  30. *
  31. * @param {Element} target - Element to look up.
  32. * @param {string} cssClass - Styled component reference.
  33. * @returns {Element|null} Ancestor.
  34. */
  35. export const findAncestorByClass = (target: Object, cssClass: string) => {
  36. if (!target || target.classList.contains(cssClass)) {
  37. return target;
  38. }
  39. return findAncestorByClass(target.parentElement, cssClass);
  40. };
  41. /**
  42. * Checks if a participant is force muted.
  43. *
  44. * @param {Object} participant - The participant.
  45. * @param {MediaType} mediaType - The media type.
  46. * @param {Object} state - The redux state.
  47. * @returns {MediaState}
  48. */
  49. export function isForceMuted(participant: Object, mediaType: MediaType, state: Object) {
  50. if (isEnabledFromState(mediaType, state)) {
  51. if (participant.local) {
  52. return !isLocalParticipantApprovedFromState(mediaType, state);
  53. }
  54. // moderators cannot be force muted
  55. if (isParticipantModerator(participant)) {
  56. return false;
  57. }
  58. return !isParticipantApproved(participant.id, mediaType)(state);
  59. }
  60. return false;
  61. }
  62. /**
  63. * Determines the audio media state (the mic icon) for a participant.
  64. *
  65. * @param {Object} participant - The participant.
  66. * @param {boolean} muted - The mute state of the participant.
  67. * @param {Object} state - The redux state.
  68. * @returns {MediaState}
  69. */
  70. export function getParticipantAudioMediaState(participant: Object, muted: Boolean, state: Object) {
  71. const dominantSpeaker = getDominantSpeakerParticipant(state);
  72. if (muted) {
  73. if (isForceMuted(participant, MEDIA_TYPE.AUDIO, state)) {
  74. return MEDIA_STATE.FORCE_MUTED;
  75. }
  76. return MEDIA_STATE.MUTED;
  77. }
  78. if (participant === dominantSpeaker) {
  79. return MEDIA_STATE.DOMINANT_SPEAKER;
  80. }
  81. return MEDIA_STATE.UNMUTED;
  82. }
  83. /**
  84. * Determines the video media state (the mic icon) for a participant.
  85. *
  86. * @param {Object} participant - The participant.
  87. * @param {boolean} muted - The mute state of the participant.
  88. * @param {Object} state - The redux state.
  89. * @returns {MediaState}
  90. */
  91. export function getParticipantVideoMediaState(participant: Object, muted: Boolean, state: Object) {
  92. if (muted) {
  93. if (isForceMuted(participant, MEDIA_TYPE.VIDEO, state)) {
  94. return MEDIA_STATE.FORCE_MUTED;
  95. }
  96. return MEDIA_STATE.MUTED;
  97. }
  98. return MEDIA_STATE.UNMUTED;
  99. }
  100. /**
  101. * Get a style property from a style declaration as a float.
  102. *
  103. * @param {CSSStyleDeclaration} styles - Style declaration.
  104. * @param {string} name - Property name.
  105. * @returns {number} Float value.
  106. */
  107. export const getFloatStyleProperty = (styles: Object, name: string) =>
  108. parseFloat(styles.getPropertyValue(name));
  109. /**
  110. * Gets the outer height of an element, including margins.
  111. *
  112. * @param {Element} element - Target element.
  113. * @returns {number} Computed height.
  114. */
  115. export const getComputedOuterHeight = (element: HTMLElement) => {
  116. const computedStyle = getComputedStyle(element);
  117. return element.offsetHeight
  118. + getFloatStyleProperty(computedStyle, 'margin-top')
  119. + getFloatStyleProperty(computedStyle, 'margin-bottom');
  120. };
  121. /**
  122. * Returns this feature's root state.
  123. *
  124. * @param {Object} state - Global state.
  125. * @returns {Object} Feature state.
  126. */
  127. const getState = (state: Object) => state[REDUCER_KEY];
  128. /**
  129. * Is the participants pane open.
  130. *
  131. * @param {Object} state - Global state.
  132. * @returns {boolean} Is the participants pane open.
  133. */
  134. export const getParticipantsPaneOpen = (state: Object) => Boolean(getState(state)?.isOpen);
  135. /**
  136. * Returns the type of quick action button to be displayed for a participant.
  137. * The button is displayed when hovering a participant from the participant list.
  138. *
  139. * @param {Object} participant - The participant.
  140. * @param {boolean} isAudioMuted - If audio is muted for the participant.
  141. * @param {Object} state - The redux state.
  142. * @returns {string} - The type of the quick action button.
  143. */
  144. export function getQuickActionButtonType(participant: Object, isAudioMuted: Boolean, state: Object) {
  145. // handled only by moderators
  146. if (isLocalParticipantModerator(state)) {
  147. if (!isAudioMuted) {
  148. return QUICK_ACTION_BUTTON.MUTE;
  149. }
  150. if (isSupported()(state)) {
  151. return QUICK_ACTION_BUTTON.ASK_TO_UNMUTE;
  152. }
  153. }
  154. return QUICK_ACTION_BUTTON.NONE;
  155. }
  156. /**
  157. * Returns true if the invite button should be rendered.
  158. *
  159. * @param {Object} state - Global state.
  160. * @returns {boolean}
  161. */
  162. export const shouldRenderInviteButton = (state: Object) => {
  163. const { disableInviteFunctions } = toState(state)['features/base/config'];
  164. const flagEnabled = getFeatureFlag(state, INVITE_ENABLED, true);
  165. const inBreakoutRoom = isInBreakoutRoom(state);
  166. return flagEnabled && !disableInviteFunctions && !inBreakoutRoom;
  167. };
  168. /**
  169. * Selector for retrieving ids of participants in the order that they are displayed in the filmstrip (with the
  170. * exception of participants with raised hand). The participants are reordered as follows.
  171. * 1. Dominant speaker.
  172. * 2. Local participant.
  173. * 3. Participants with raised hand.
  174. * 4. Participants with screenshare sorted alphabetically by their display name.
  175. * 5. Shared video participants.
  176. * 6. Recent speakers sorted alphabetically by their display name.
  177. * 7. Rest of the participants sorted alphabetically by their display name.
  178. *
  179. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  180. * {@code getState} function to be used to retrieve the state features/base/participants.
  181. * @returns {Array<string>}
  182. */
  183. export function getSortedParticipantIds(stateful: Object | Function): Array<string> {
  184. const { id } = getLocalParticipant(stateful);
  185. const remoteParticipants = getRemoteParticipantsSorted(stateful);
  186. const reorderedParticipants = new Set(remoteParticipants);
  187. const raisedHandParticipants = getRaiseHandsQueue(stateful).map(({ id: particId }) => particId);
  188. const remoteRaisedHandParticipants = new Set(raisedHandParticipants || []);
  189. const dominantSpeaker = getDominantSpeakerParticipant(stateful);
  190. for (const participant of remoteRaisedHandParticipants.keys()) {
  191. // Avoid duplicates.
  192. if (reorderedParticipants.has(participant)) {
  193. reorderedParticipants.delete(participant);
  194. }
  195. }
  196. const dominant = [];
  197. const local = remoteRaisedHandParticipants.has(id) ? [] : [ id ];
  198. // Remove dominant speaker.
  199. if (dominantSpeaker && dominantSpeaker.id !== id) {
  200. remoteRaisedHandParticipants.delete(dominantSpeaker.id);
  201. reorderedParticipants.delete(dominantSpeaker.id);
  202. dominant.push(dominantSpeaker.id);
  203. }
  204. // Move self and participants with raised hand to the top of the list.
  205. return [
  206. ...dominant,
  207. ...local,
  208. ...Array.from(remoteRaisedHandParticipants.keys()),
  209. ...Array.from(reorderedParticipants.keys())
  210. ];
  211. }