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.

NotificationsContainer.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 clsx from 'clsx';
  6. import React, { Component } from 'react';
  7. import { CSSTransition, TransitionGroup } from 'react-transition-group';
  8. import { translate } from '../../../base/i18n';
  9. import { connect } from '../../../base/redux';
  10. import { hideNotification } from '../../actions';
  11. import { areThereNotifications } from '../../functions';
  12. import Notification from './Notification';
  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. * JSS classes object.
  29. */
  30. classes: Object,
  31. /**
  32. * Invoked to update the redux store in order to remove notifications.
  33. */
  34. dispatch: Function,
  35. /**
  36. * Whether or not the notifications are displayed in a portal.
  37. */
  38. portal?: boolean,
  39. /**
  40. * Invoked to obtain translated strings.
  41. */
  42. t: Function
  43. };
  44. const useStyles = theme => {
  45. return {
  46. container: {
  47. position: 'absolute',
  48. left: '16px',
  49. bottom: '90px',
  50. width: '400px',
  51. maxWidth: '100%',
  52. zIndex: 600
  53. },
  54. containerPortal: {
  55. maxWidth: 'calc(100% - 32px)'
  56. },
  57. transitionGroup: {
  58. '& > *': {
  59. marginBottom: '20px',
  60. borderRadius: '6px!important', // !important used to overwrite atlaskit style
  61. position: 'relative'
  62. },
  63. '& div > span > svg > path': {
  64. fill: 'inherit'
  65. },
  66. '& div > span, & div > p': {
  67. color: theme.palette.field01
  68. },
  69. '& div.message > span': {
  70. color: theme.palette.link01Active
  71. },
  72. '& .ribbon': {
  73. width: '4px',
  74. height: 'calc(100% - 16px)',
  75. position: 'absolute',
  76. left: 0,
  77. top: '8px',
  78. borderRadius: '4px',
  79. '&.normal': {
  80. backgroundColor: theme.palette.link01Active
  81. },
  82. '&.error': {
  83. backgroundColor: theme.palette.iconError
  84. },
  85. '&.success': {
  86. backgroundColor: theme.palette.success01
  87. },
  88. '&.warning': {
  89. backgroundColor: theme.palette.warning01
  90. }
  91. }
  92. }
  93. };
  94. };
  95. /**
  96. * Implements a React {@link Component} which displays notifications and handles
  97. * automatic dismissal after a notification is shown for a defined timeout
  98. * period.
  99. *
  100. * @augments {Component}
  101. */
  102. class NotificationsContainer extends Component<Props> {
  103. _api: Object;
  104. _timeouts: Map<string, TimeoutID>;
  105. /**
  106. * Initializes a new {@code NotificationsContainer} instance.
  107. *
  108. * @inheritdoc
  109. */
  110. constructor(props: Props) {
  111. super(props);
  112. this._timeouts = new Map();
  113. // Bind event handlers so they are only bound once for every instance.
  114. this._onDismissed = this._onDismissed.bind(this);
  115. // HACK ALERT! We are rendering AtlasKit Flag elements outside of a FlagGroup container.
  116. // In order to hook-up the dismiss action we'll a fake context provider,
  117. // just like FlagGroup does.
  118. this._api = {
  119. onDismissed: this._onDismissed,
  120. dismissAllowed: () => true
  121. };
  122. }
  123. /**
  124. * Implements React's {@link Component#render()}.
  125. *
  126. * @inheritdoc
  127. * @returns {ReactElement}
  128. */
  129. render() {
  130. if (this.props._iAmSipGateway) {
  131. return null;
  132. }
  133. return (
  134. <AtlasKitThemeProvider mode = 'light'>
  135. <FlagGroupContext.Provider value = { this._api }>
  136. <div
  137. className = { clsx(this.props.classes.container, {
  138. [this.props.classes.containerPortal]: this.props.portal
  139. }) }
  140. id = 'notifications-container'>
  141. <TransitionGroup className = { this.props.classes.transitionGroup }>
  142. {this._renderFlags()}
  143. </TransitionGroup>
  144. </div>
  145. </FlagGroupContext.Provider>
  146. </AtlasKitThemeProvider>
  147. );
  148. }
  149. _onDismissed: number => void;
  150. /**
  151. * Emits an action to remove the notification from the redux store so it
  152. * stops displaying.
  153. *
  154. * @param {number} uid - The id of the notification to be removed.
  155. * @private
  156. * @returns {void}
  157. */
  158. _onDismissed(uid) {
  159. const timeout = this._timeouts.get(uid);
  160. if (timeout) {
  161. clearTimeout(timeout);
  162. this._timeouts.delete(uid);
  163. }
  164. this.props.dispatch(hideNotification(uid));
  165. }
  166. /**
  167. * Renders notifications to display as ReactElements. An empty array will
  168. * be returned if notifications are disabled.
  169. *
  170. * @private
  171. * @returns {ReactElement[]}
  172. */
  173. _renderFlags() {
  174. const { _notifications } = this.props;
  175. return _notifications.map(notification => {
  176. const { props, uid } = notification;
  177. // The id attribute is necessary as {@code FlagGroup} looks for
  178. // either id or key to set a key on notifications, but accessing
  179. // props.key will cause React to print an error.
  180. return (
  181. <CSSTransition
  182. appear = { true }
  183. classNames = 'notification'
  184. in = { true }
  185. key = { uid }
  186. timeout = { 200 }>
  187. <Notification
  188. { ...props }
  189. id = { uid }
  190. onDismissed = { this._onDismissed }
  191. uid = { uid } />
  192. </CSSTransition>
  193. );
  194. });
  195. }
  196. }
  197. /**
  198. * Maps (parts of) the Redux state to the associated props for this component.
  199. *
  200. * @param {Object} state - The Redux state.
  201. * @private
  202. * @returns {Props}
  203. */
  204. function _mapStateToProps(state) {
  205. const { notifications } = state['features/notifications'];
  206. const { iAmSipGateway } = state['features/base/config'];
  207. const { isOpen: isChatOpen } = state['features/chat'];
  208. const _visible = areThereNotifications(state);
  209. return {
  210. _iAmSipGateway: Boolean(iAmSipGateway),
  211. _isChatOpen: isChatOpen,
  212. _notifications: _visible ? notifications : []
  213. };
  214. }
  215. export default translate(connect(_mapStateToProps)(withStyles(useStyles)(NotificationsContainer)));