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

NotificationsContainer.js 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // @flow
  2. import { FlagGroupContext } from '@atlaskit/flag/flag-group';
  3. import { AtlasKitThemeProvider } from '@atlaskit/theme';
  4. import { withStyles } from '@material-ui/styles';
  5. import React, { Component } from 'react';
  6. import { CSSTransition, TransitionGroup } from 'react-transition-group';
  7. import { translate } from '../../../base/i18n';
  8. import { connect } from '../../../base/redux';
  9. import { hideNotification } from '../../actions';
  10. import { areThereNotifications } from '../../functions';
  11. import Notification from './Notification';
  12. declare var interfaceConfig: Object;
  13. type Props = {
  14. /**
  15. * Whether we are a SIP gateway or not.
  16. */
  17. _iAmSipGateway: boolean,
  18. /**
  19. * Whether or not the chat is open.
  20. */
  21. _isChatOpen: boolean,
  22. /**
  23. * The notifications to be displayed, with the first index being the
  24. * notification at the top and the rest shown below it in order.
  25. */
  26. _notifications: Array<Object>,
  27. /**
  28. * The length, in milliseconds, to use as a default timeout for all
  29. * dismissible timeouts that do not have a timeout specified.
  30. */
  31. autoDismissTimeout: number,
  32. /**
  33. * JSS classes object.
  34. */
  35. classes: Object,
  36. /**
  37. * Invoked to update the redux store in order to remove notifications.
  38. */
  39. dispatch: Function,
  40. /**
  41. * Whether or not the notifications are displayed in a portal.
  42. */
  43. portal?: boolean,
  44. /**
  45. * Invoked to obtain translated strings.
  46. */
  47. t: Function
  48. };
  49. const useStyles = theme => {
  50. return {
  51. container: {
  52. position: 'absolute',
  53. left: '16px',
  54. bottom: '90px',
  55. width: '400px',
  56. maxWidth: '100%',
  57. zIndex: 600
  58. },
  59. containerPortal: {
  60. maxWidth: 'calc(100% - 32px)'
  61. },
  62. containerChatOpen: {
  63. left: '331px'
  64. },
  65. transitionGroup: {
  66. '& > *': {
  67. marginBottom: '20px',
  68. borderRadius: '6px!important', // !important used to overwrite atlaskit style
  69. position: 'relative'
  70. },
  71. '& div > span > svg > path': {
  72. fill: 'inherit'
  73. },
  74. '& div > span, & div > p': {
  75. color: theme.palette.field01
  76. },
  77. '& .ribbon': {
  78. width: '4px',
  79. height: 'calc(100% - 16px)',
  80. position: 'absolute',
  81. left: 0,
  82. top: '8px',
  83. borderRadius: '4px',
  84. '&.normal': {
  85. backgroundColor: theme.palette.link01Active
  86. },
  87. '&.error': {
  88. backgroundColor: theme.palette.iconError
  89. },
  90. '&.warning': {
  91. backgroundColor: theme.palette.warning01
  92. }
  93. }
  94. }
  95. };
  96. };
  97. /**
  98. * Implements a React {@link Component} which displays notifications and handles
  99. * automatic dismissal after a notification is shown for a defined timeout
  100. * period.
  101. *
  102. * @extends {Component}
  103. */
  104. class NotificationsContainer extends Component<Props> {
  105. _api: Object;
  106. _timeouts: Map<string, TimeoutID>;
  107. /**
  108. * Initializes a new {@code NotificationsContainer} instance.
  109. *
  110. * @inheritdoc
  111. */
  112. constructor(props: Props) {
  113. super(props);
  114. this._timeouts = new Map();
  115. // Bind event handlers so they are only bound once for every instance.
  116. this._onDismissed = this._onDismissed.bind(this);
  117. // HACK ALERT! We are rendering AtlasKit Flag elements outside of a FlagGroup container.
  118. // In order to hook-up the dismiss action we'll a fake context provider,
  119. // just like FlagGroup does.
  120. this._api = {
  121. onDismissed: this._onDismissed,
  122. dismissAllowed: () => true
  123. };
  124. }
  125. /**
  126. * Sets a timeout for each notification, where applicable.
  127. *
  128. * @inheritdoc
  129. */
  130. componentDidMount() {
  131. this._updateTimeouts();
  132. }
  133. /**
  134. * Sets a timeout for each notification, where applicable.
  135. *
  136. * @inheritdoc
  137. */
  138. componentDidUpdate() {
  139. this._updateTimeouts();
  140. }
  141. /**
  142. * Implements React's {@link Component#render()}.
  143. *
  144. * @inheritdoc
  145. * @returns {ReactElement}
  146. */
  147. render() {
  148. if (this.props._iAmSipGateway) {
  149. return null;
  150. }
  151. return (
  152. <AtlasKitThemeProvider mode = 'light'>
  153. <FlagGroupContext.Provider value = { this._api }>
  154. <div
  155. className = { `${this.props.classes.container} ${this.props.portal
  156. ? this.props.classes.containerPortal
  157. : this.props._isChatOpen
  158. ? this.props.classes.containerChatOpen
  159. : ''}`
  160. }
  161. id = 'notifications-container'>
  162. <TransitionGroup className = { this.props.classes.transitionGroup }>
  163. {this._renderFlags()}
  164. </TransitionGroup>
  165. </div>
  166. </FlagGroupContext.Provider>
  167. </AtlasKitThemeProvider>
  168. );
  169. }
  170. _onDismissed: number => void;
  171. /**
  172. * Emits an action to remove the notification from the redux store so it
  173. * stops displaying.
  174. *
  175. * @param {number} uid - The id of the notification to be removed.
  176. * @private
  177. * @returns {void}
  178. */
  179. _onDismissed(uid) {
  180. const timeout = this._timeouts.get(uid);
  181. if (timeout) {
  182. clearTimeout(timeout);
  183. this._timeouts.delete(uid);
  184. }
  185. this.props.dispatch(hideNotification(uid));
  186. }
  187. /**
  188. * Renders notifications to display as ReactElements. An empty array will
  189. * be returned if notifications are disabled.
  190. *
  191. * @private
  192. * @returns {ReactElement[]}
  193. */
  194. _renderFlags() {
  195. const { _notifications } = this.props;
  196. return _notifications.map(notification => {
  197. const { props, uid } = notification;
  198. // The id attribute is necessary as {@code FlagGroup} looks for
  199. // either id or key to set a key on notifications, but accessing
  200. // props.key will cause React to print an error.
  201. return (
  202. <CSSTransition
  203. appear = { true }
  204. classNames = 'notification'
  205. in = { true }
  206. key = { uid }
  207. timeout = { 200 }>
  208. <Notification
  209. { ...props }
  210. id = { uid }
  211. onDismissed = { this._onDismissed }
  212. uid = { uid } />
  213. </CSSTransition>
  214. );
  215. });
  216. }
  217. /**
  218. * Updates the timeouts for every notification.
  219. *
  220. * @returns {void}
  221. */
  222. _updateTimeouts() {
  223. const { _notifications, autoDismissTimeout } = this.props;
  224. for (const notification of _notifications) {
  225. if ((notification.timeout || typeof autoDismissTimeout === 'number')
  226. && notification.props.isDismissAllowed !== false
  227. && !this._timeouts.has(notification.uid)) {
  228. const {
  229. timeout = autoDismissTimeout,
  230. uid
  231. } = notification;
  232. const timerID = setTimeout(() => {
  233. this._onDismissed(uid);
  234. }, timeout);
  235. this._timeouts.set(uid, timerID);
  236. }
  237. }
  238. }
  239. }
  240. /**
  241. * Maps (parts of) the Redux state to the associated props for this component.
  242. *
  243. * @param {Object} state - The Redux state.
  244. * @private
  245. * @returns {Props}
  246. */
  247. function _mapStateToProps(state) {
  248. const { notifications } = state['features/notifications'];
  249. const { iAmSipGateway } = state['features/base/config'];
  250. const { isOpen: isChatOpen } = state['features/chat'];
  251. const _visible = areThereNotifications(state);
  252. return {
  253. _iAmSipGateway: Boolean(iAmSipGateway),
  254. _isChatOpen: isChatOpen,
  255. _notifications: _visible ? notifications : [],
  256. autoDismissTimeout: interfaceConfig.ENFORCE_NOTIFICATION_AUTO_DISMISS_TIMEOUT
  257. };
  258. }
  259. export default translate(connect(_mapStateToProps)(withStyles(useStyles)(NotificationsContainer)));