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.

actions.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // @flow
  2. import throttle from 'lodash/throttle';
  3. import type { Dispatch } from 'redux';
  4. import { NOTIFICATIONS_ENABLED, getFeatureFlag } from '../base/flags';
  5. import {
  6. CLEAR_NOTIFICATIONS,
  7. HIDE_NOTIFICATION,
  8. SET_NOTIFICATIONS_ENABLED,
  9. SHOW_NOTIFICATION
  10. } from './actionTypes';
  11. import { NOTIFICATION_TIMEOUT, NOTIFICATION_TYPE } from './constants';
  12. /**
  13. * Clears (removes) all the notifications.
  14. *
  15. * @returns {{
  16. * type: CLEAR_NOTIFICATIONS
  17. * }}
  18. */
  19. export function clearNotifications() {
  20. return {
  21. type: CLEAR_NOTIFICATIONS
  22. };
  23. }
  24. /**
  25. * Removes the notification with the passed in id.
  26. *
  27. * @param {string} uid - The unique identifier for the notification to be
  28. * removed.
  29. * @returns {{
  30. * type: HIDE_NOTIFICATION,
  31. * uid: string
  32. * }}
  33. */
  34. export function hideNotification(uid: string) {
  35. return {
  36. type: HIDE_NOTIFICATION,
  37. uid
  38. };
  39. }
  40. /**
  41. * Stops notifications from being displayed.
  42. *
  43. * @param {boolean} enabled - Whether or not notifications should display.
  44. * @returns {{
  45. * type: SET_NOTIFICATIONS_ENABLED,
  46. * enabled: boolean
  47. * }}
  48. */
  49. export function setNotificationsEnabled(enabled: boolean) {
  50. return {
  51. type: SET_NOTIFICATIONS_ENABLED,
  52. enabled
  53. };
  54. }
  55. /**
  56. * Queues an error notification for display.
  57. *
  58. * @param {Object} props - The props needed to show the notification component.
  59. * @returns {Object}
  60. */
  61. export function showErrorNotification(props: Object) {
  62. return showNotification({
  63. ...props,
  64. appearance: NOTIFICATION_TYPE.ERROR
  65. });
  66. }
  67. /**
  68. * Queues a notification for display.
  69. *
  70. * @param {Object} props - The props needed to show the notification component.
  71. * @param {number} timeout - How long the notification should display before
  72. * automatically being hidden.
  73. * @returns {Function}
  74. */
  75. export function showNotification(props: Object = {}, timeout: ?number) {
  76. return function(dispatch: Function, getState: Function) {
  77. const { notifications } = getState()['features/base/config'];
  78. const enabledFlag = getFeatureFlag(getState(), NOTIFICATIONS_ENABLED, true);
  79. const shouldDisplay = enabledFlag
  80. && (!notifications
  81. || notifications.includes(props.descriptionKey)
  82. || notifications.includes(props.titleKey));
  83. if (shouldDisplay) {
  84. return dispatch({
  85. type: SHOW_NOTIFICATION,
  86. props,
  87. timeout,
  88. uid: props.uid || window.Date.now().toString()
  89. });
  90. }
  91. };
  92. }
  93. /**
  94. * Queues a warning notification for display.
  95. *
  96. * @param {Object} props - The props needed to show the notification component.
  97. * @returns {Object}
  98. */
  99. export function showWarningNotification(props: Object) {
  100. return showNotification({
  101. ...props,
  102. appearance: NOTIFICATION_TYPE.WARNING
  103. });
  104. }
  105. /**
  106. * An array of names of participants that have joined the conference. The array
  107. * is replaced with an empty array as notifications are displayed.
  108. *
  109. * @private
  110. * @type {string[]}
  111. */
  112. let joinedParticipantsNames = [];
  113. /**
  114. * A throttled internal function that takes the internal list of participant
  115. * names, {@code joinedParticipantsNames}, and triggers the display of a
  116. * notification informing of their joining.
  117. *
  118. * @private
  119. * @type {Function}
  120. */
  121. const _throttledNotifyParticipantConnected = throttle((dispatch: Dispatch<any>) => {
  122. const joinedParticipantsCount = joinedParticipantsNames.length;
  123. let notificationProps;
  124. if (joinedParticipantsCount >= 3) {
  125. notificationProps = {
  126. titleArguments: {
  127. name: joinedParticipantsNames[0],
  128. count: joinedParticipantsCount - 1
  129. },
  130. titleKey: 'notify.connectedThreePlusMembers'
  131. };
  132. } else if (joinedParticipantsCount === 2) {
  133. notificationProps = {
  134. titleArguments: {
  135. first: joinedParticipantsNames[0],
  136. second: joinedParticipantsNames[1]
  137. },
  138. titleKey: 'notify.connectedTwoMembers'
  139. };
  140. } else if (joinedParticipantsCount) {
  141. notificationProps = {
  142. titleArguments: {
  143. name: joinedParticipantsNames[0]
  144. },
  145. titleKey: 'notify.connectedOneMember'
  146. };
  147. }
  148. if (notificationProps) {
  149. dispatch(
  150. showNotification(notificationProps, NOTIFICATION_TIMEOUT));
  151. }
  152. joinedParticipantsNames = [];
  153. }, 500, { leading: false });
  154. /**
  155. * Queues the display of a notification of a participant having connected to
  156. * the meeting. The notifications are batched so that quick consecutive
  157. * connection events are shown in one notification.
  158. *
  159. * @param {string} displayName - The name of the participant that connected.
  160. * @returns {Function}
  161. */
  162. export function showParticipantJoinedNotification(displayName: string) {
  163. joinedParticipantsNames.push(displayName);
  164. return (dispatch: Dispatch<any>) => _throttledNotifyParticipantConnected(dispatch);
  165. }