您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

NotificationsContainer.web.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import { FlagGroup } from '@atlaskit/flag';
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import { hideNotification } from '../actions';
  5. /**
  6. * Implements a React {@link Component} which displays notifications and handles
  7. * automatic dismissmal after a notification is shown for a defined timeout
  8. * period.
  9. *
  10. * @extends {Component}
  11. */
  12. class NotificationsContainer extends Component {
  13. /**
  14. * {@code NotificationsContainer} component's property types.
  15. *
  16. * @static
  17. */
  18. static propTypes = {
  19. /**
  20. * The notifications to be displayed, with the first index being the
  21. * notification at the top and the rest shown below it in order.
  22. */
  23. _notifications: React.PropTypes.array,
  24. /**
  25. * Whether or not notifications should be displayed at all. If not,
  26. * notifications will be dismissed immediately.
  27. */
  28. _showNotifications: React.PropTypes.bool,
  29. /**
  30. * Invoked to update the redux store in order to remove notifications.
  31. */
  32. dispatch: React.PropTypes.func
  33. };
  34. /**
  35. * Initializes a new {@code NotificationsContainer} instance.
  36. *
  37. * @param {Object} props - The read-only React Component props with which
  38. * the new instance is to be initialized.
  39. */
  40. constructor(props) {
  41. super(props);
  42. /**
  43. * The timeout set for automatically dismissing a displayed
  44. * notification. This value is set on the instance and not state to
  45. * avoid additional re-renders.
  46. *
  47. * @type {number|null}
  48. */
  49. this._notificationDismissTimeout = null;
  50. // Bind event handlers so they are only bound once for every instance.
  51. this._onDismissed = this._onDismissed.bind(this);
  52. }
  53. /**
  54. * Sets a timeout if the currently displayed notification has changed.
  55. *
  56. * @inheritdoc
  57. * returns {void}
  58. */
  59. componentDidUpdate() {
  60. const { _notifications, _showNotifications } = this.props;
  61. if (_notifications.length) {
  62. const notification = _notifications[0];
  63. if (!_showNotifications) {
  64. this._onDismissed(notification.uid);
  65. } else if (this._notificationDismissTimeout) {
  66. // No-op because there should already be a notification that
  67. // is waiting for dismissal.
  68. } else {
  69. const { timeout, uid } = notification;
  70. this._notificationDismissTimeout = setTimeout(() => {
  71. // Perform a no-op if a timeout is not specified.
  72. if (Number.isInteger(timeout)) {
  73. this._onDismissed(uid);
  74. }
  75. }, timeout);
  76. }
  77. }
  78. }
  79. /**
  80. * Clear any dismissal timeout that is still active.
  81. *
  82. * @inheritdoc
  83. * returns {void}
  84. */
  85. componentWillUnmount() {
  86. clearTimeout(this._notificationDismissTimeout);
  87. }
  88. /**
  89. * Implements React's {@link Component#render()}.
  90. *
  91. * @inheritdoc
  92. * @returns {ReactElement}
  93. */
  94. render() {
  95. return (
  96. <FlagGroup onDismissed = { this._onDismissed }>
  97. { this._renderFlags() }
  98. </FlagGroup>
  99. );
  100. }
  101. /**
  102. * Emits an action to remove the notification from the redux store so it
  103. * stops displaying.
  104. *
  105. * @param {number} flagUid - The id of the notification to be removed.
  106. * @private
  107. * @returns {void}
  108. */
  109. _onDismissed(flagUid) {
  110. clearTimeout(this._notificationDismissTimeout);
  111. this._notificationDismissTimeout = null;
  112. this.props.dispatch(hideNotification(flagUid));
  113. }
  114. /**
  115. * Renders notifications to display as ReactElements. An empty array will
  116. * be returned if notifications are disabled.
  117. *
  118. * @private
  119. * @returns {ReactElement[]}
  120. */
  121. _renderFlags() {
  122. const { _notifications, _showNotifications } = this.props;
  123. if (!_showNotifications) {
  124. return [];
  125. }
  126. return _notifications.map(notification => {
  127. const Notification = notification.component;
  128. const { props, uid } = notification;
  129. // The id attribute is necessary as {@code FlagGroup} looks for
  130. // either id or key to set a key on notifications, but accessing
  131. // props.key will cause React to print an error.
  132. return (
  133. <Notification
  134. { ...props }
  135. id = { uid }
  136. key = { uid }
  137. uid = { uid } />
  138. );
  139. });
  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: React.PropTypes.array
  150. * }}
  151. */
  152. function _mapStateToProps(state) {
  153. // TODO: Per existing behavior, notifications should not display when an
  154. // overlay is visible. This logic for checking overlay display can likely be
  155. // simplified.
  156. const {
  157. connectionEstablished,
  158. haveToReload,
  159. isMediaPermissionPromptVisible,
  160. suspendDetected
  161. } = state['features/overlay'];
  162. const isAnyOverlayVisible = (connectionEstablished && haveToReload)
  163. || isMediaPermissionPromptVisible
  164. || suspendDetected
  165. || state['features/jwt'].callOverlayVisible;
  166. const { enabled, notifications } = state['features/notifications'];
  167. return {
  168. _notifications: notifications,
  169. _showNotifications: enabled && !isAnyOverlayVisible
  170. };
  171. }
  172. export default connect(_mapStateToProps)(NotificationsContainer);