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.

AbstractNotificationsContainer.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // @flow
  2. import { Component } from 'react';
  3. import { hideNotification } from '../actions';
  4. import { areThereNotifications } from '../functions';
  5. export type Props = {
  6. /**
  7. * The notifications to be displayed, with the first index being the
  8. * notification at the top and the rest shown below it in order.
  9. */
  10. _notifications: Array<Object>,
  11. /**
  12. * The length, in milliseconds, to use as a default timeout for all
  13. * dismissable timeouts that do not have a timeout specified.
  14. */
  15. autoDismissTimeout: number,
  16. /**
  17. * Invoked to update the redux store in order to remove notifications.
  18. */
  19. dispatch: Function
  20. };
  21. declare var interfaceConfig: Object;
  22. /**
  23. * Abstract class for {@code NotificationsContainer} component.
  24. */
  25. export default class AbstractNotificationsContainer<P: Props>
  26. extends Component<P> {
  27. /**
  28. * A timeout id returned by setTimeout.
  29. */
  30. _notificationDismissTimeout: ?TimeoutID;
  31. /**
  32. * Initializes a new {@code AbstractNotificationsContainer} instance.
  33. *
  34. * @inheritdoc
  35. */
  36. constructor(props: P) {
  37. super(props);
  38. /**
  39. * The timeout set for automatically dismissing a displayed
  40. * notification. This value is set on the instance and not state to
  41. * avoid additional re-renders.
  42. *
  43. * @type {number|null}
  44. */
  45. this._notificationDismissTimeout = null;
  46. // Bind event handlers so they are only bound once for every instance.
  47. this._onDismissed = this._onDismissed.bind(this);
  48. }
  49. /**
  50. * Sets a timeout for the first notification (if applicable).
  51. *
  52. * @inheritdoc
  53. */
  54. componentDidMount() {
  55. // Set the initial dismiss timeout (if any)
  56. this._manageDismissTimeout();
  57. }
  58. /**
  59. * Sets a timeout if the currently displayed notification has changed.
  60. *
  61. * @inheritdoc
  62. */
  63. componentDidUpdate(prevProps: P) {
  64. this._manageDismissTimeout(prevProps);
  65. }
  66. /**
  67. * Sets/clears the dismiss timeout for the top notification.
  68. *
  69. * @param {P} [prevProps] - The previous properties (if called from
  70. * {@code componentDidUpdate}).
  71. * @returns {void}
  72. * @private
  73. */
  74. _manageDismissTimeout(prevProps: ?P) {
  75. const { _notifications, autoDismissTimeout } = this.props;
  76. if (_notifications.length) {
  77. const notification = _notifications[0];
  78. const previousNotification
  79. = prevProps && prevProps._notifications.length
  80. ? prevProps._notifications[0]
  81. : undefined;
  82. if (notification !== previousNotification) {
  83. this._clearNotificationDismissTimeout();
  84. if (notification
  85. && (notification.timeout
  86. || typeof autoDismissTimeout === 'number')
  87. && notification.props.isDismissAllowed !== false) {
  88. const {
  89. timeout = autoDismissTimeout,
  90. uid
  91. } = notification;
  92. this._notificationDismissTimeout = setTimeout(() => {
  93. // Perform a no-op if a timeout is not specified.
  94. this._onDismissed(uid);
  95. }, timeout);
  96. }
  97. }
  98. } else if (this._notificationDismissTimeout) {
  99. // Clear timeout when all notifications are cleared (e.g external
  100. // call to clear them)
  101. this._clearNotificationDismissTimeout();
  102. }
  103. }
  104. /**
  105. * Clear any dismissal timeout that is still active.
  106. *
  107. * @inheritdoc
  108. * returns {void}
  109. */
  110. componentWillUnmount() {
  111. this._clearNotificationDismissTimeout();
  112. }
  113. _onDismissed: number => void;
  114. /**
  115. * Clears the running notification dismiss timeout, if any.
  116. *
  117. * @returns {void}
  118. */
  119. _clearNotificationDismissTimeout() {
  120. this._notificationDismissTimeout
  121. && clearTimeout(this._notificationDismissTimeout);
  122. this._notificationDismissTimeout = null;
  123. }
  124. /**
  125. * Emits an action to remove the notification from the redux store so it
  126. * stops displaying.
  127. *
  128. * @param {number} uid - The id of the notification to be removed.
  129. * @private
  130. * @returns {void}
  131. */
  132. _onDismissed(uid) {
  133. const { _notifications } = this.props;
  134. // Clear the timeout only if it's the top notification that's being
  135. // dismissed (the timeout is set only for the top one).
  136. if (!_notifications.length || _notifications[0].uid === uid) {
  137. this._clearNotificationDismissTimeout();
  138. }
  139. this.props.dispatch(hideNotification(uid));
  140. }
  141. }
  142. /**
  143. * Maps (parts of) the Redux state to the associated NotificationsContainer's
  144. * props.
  145. *
  146. * @param {Object} state - The Redux state.
  147. * @private
  148. * @returns {{
  149. * _notifications: Array
  150. * }}
  151. */
  152. export function _abstractMapStateToProps(state: Object) {
  153. const { notifications } = state['features/notifications'];
  154. const _visible = areThereNotifications(state);
  155. return {
  156. _notifications: _visible ? notifications : [],
  157. autoDismissTimeout: typeof interfaceConfig === 'undefined'
  158. ? undefined // Ignore for the case of mobile
  159. : interfaceConfig.ENFORCE_NOTIFICATION_AUTO_DISMISS_TIMEOUT
  160. };
  161. }