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.4KB

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