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 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. * @param {number} timeout - How long the notification should display before
  98. * automatically being hidden.
  99. * @returns {Object}
  100. */
  101. export function showWarningNotification(props: Object, timeout: ?number) {
  102. return showNotification({
  103. ...props,
  104. appearance: NOTIFICATION_TYPE.WARNING
  105. }, timeout);
  106. }
  107. /**
  108. * An array of names of participants that have joined the conference. The array
  109. * is replaced with an empty array as notifications are displayed.
  110. *
  111. * @private
  112. * @type {string[]}
  113. */
  114. let joinedParticipantsNames = [];
  115. /**
  116. * A throttled internal function that takes the internal list of participant
  117. * names, {@code joinedParticipantsNames}, and triggers the display of a
  118. * notification informing of their joining.
  119. *
  120. * @private
  121. * @type {Function}
  122. */
  123. const _throttledNotifyParticipantConnected = throttle((dispatch: Dispatch<any>) => {
  124. const joinedParticipantsCount = joinedParticipantsNames.length;
  125. let notificationProps;
  126. if (joinedParticipantsCount >= 3) {
  127. notificationProps = {
  128. titleArguments: {
  129. name: joinedParticipantsNames[0],
  130. count: joinedParticipantsCount - 1
  131. },
  132. titleKey: 'notify.connectedThreePlusMembers'
  133. };
  134. } else if (joinedParticipantsCount === 2) {
  135. notificationProps = {
  136. titleArguments: {
  137. first: joinedParticipantsNames[0],
  138. second: joinedParticipantsNames[1]
  139. },
  140. titleKey: 'notify.connectedTwoMembers'
  141. };
  142. } else if (joinedParticipantsCount) {
  143. notificationProps = {
  144. titleArguments: {
  145. name: joinedParticipantsNames[0]
  146. },
  147. titleKey: 'notify.connectedOneMember'
  148. };
  149. }
  150. if (notificationProps) {
  151. dispatch(
  152. showNotification(notificationProps, NOTIFICATION_TIMEOUT));
  153. }
  154. joinedParticipantsNames = [];
  155. }, 500, { leading: false });
  156. /**
  157. * Queues the display of a notification of a participant having connected to
  158. * the meeting. The notifications are batched so that quick consecutive
  159. * connection events are shown in one notification.
  160. *
  161. * @param {string} displayName - The name of the participant that connected.
  162. * @returns {Function}
  163. */
  164. export function showParticipantJoinedNotification(displayName: string) {
  165. joinedParticipantsNames.push(displayName);
  166. return (dispatch: Dispatch<any>) => _throttledNotifyParticipantConnected(dispatch);
  167. }