Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

NotificationsContainer.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. type Props = {
  13. /**
  14. * Whether we are a SIP gateway or not.
  15. */
  16. _iAmSipGateway: boolean,
  17. /**
  18. * Whether or not the chat is open.
  19. */
  20. _isChatOpen: boolean,
  21. /**
  22. * The notifications to be displayed, with the first index being the
  23. * notification at the top and the rest shown below it in order.
  24. */
  25. _notifications: Array<Object>,
  26. /**
  27. * JSS classes object.
  28. */
  29. classes: Object,
  30. /**
  31. * Invoked to update the redux store in order to remove notifications.
  32. */
  33. dispatch: Function,
  34. /**
  35. * Whether or not the notifications are displayed in a portal.
  36. */
  37. portal?: boolean,
  38. /**
  39. * Invoked to obtain translated strings.
  40. */
  41. t: Function
  42. };
  43. const useStyles = theme => {
  44. return {
  45. container: {
  46. position: 'absolute',
  47. left: '16px',
  48. bottom: '90px',
  49. width: '400px',
  50. maxWidth: '100%',
  51. zIndex: 600
  52. },
  53. containerPortal: {
  54. maxWidth: 'calc(100% - 32px)'
  55. },
  56. containerChatOpen: {
  57. left: '331px'
  58. },
  59. transitionGroup: {
  60. '& > *': {
  61. marginBottom: '20px',
  62. borderRadius: '6px!important', // !important used to overwrite atlaskit style
  63. position: 'relative'
  64. },
  65. '& div > span > svg > path': {
  66. fill: 'inherit'
  67. },
  68. '& div > span, & div > p': {
  69. color: theme.palette.field01
  70. },
  71. '& div.message > span': {
  72. color: theme.palette.link01Active
  73. },
  74. '& .ribbon': {
  75. width: '4px',
  76. height: 'calc(100% - 16px)',
  77. position: 'absolute',
  78. left: 0,
  79. top: '8px',
  80. borderRadius: '4px',
  81. '&.normal': {
  82. backgroundColor: theme.palette.link01Active
  83. },
  84. '&.error': {
  85. backgroundColor: theme.palette.iconError
  86. },
  87. '&.success': {
  88. backgroundColor: theme.palette.success01
  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. * @augments {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. * Implements React's {@link Component#render()}.
  127. *
  128. * @inheritdoc
  129. * @returns {ReactElement}
  130. */
  131. render() {
  132. if (this.props._iAmSipGateway) {
  133. return null;
  134. }
  135. return (
  136. <AtlasKitThemeProvider mode = 'light'>
  137. <FlagGroupContext.Provider value = { this._api }>
  138. <div
  139. className = { `${this.props.classes.container} ${this.props.portal
  140. ? this.props.classes.containerPortal
  141. : this.props._isChatOpen
  142. ? this.props.classes.containerChatOpen
  143. : ''}`
  144. }
  145. id = 'notifications-container'>
  146. <TransitionGroup className = { this.props.classes.transitionGroup }>
  147. {this._renderFlags()}
  148. </TransitionGroup>
  149. </div>
  150. </FlagGroupContext.Provider>
  151. </AtlasKitThemeProvider>
  152. );
  153. }
  154. _onDismissed: number => void;
  155. /**
  156. * Emits an action to remove the notification from the redux store so it
  157. * stops displaying.
  158. *
  159. * @param {number} uid - The id of the notification to be removed.
  160. * @private
  161. * @returns {void}
  162. */
  163. _onDismissed(uid) {
  164. const timeout = this._timeouts.get(uid);
  165. if (timeout) {
  166. clearTimeout(timeout);
  167. this._timeouts.delete(uid);
  168. }
  169. this.props.dispatch(hideNotification(uid));
  170. }
  171. /**
  172. * Renders notifications to display as ReactElements. An empty array will
  173. * be returned if notifications are disabled.
  174. *
  175. * @private
  176. * @returns {ReactElement[]}
  177. */
  178. _renderFlags() {
  179. const { _notifications } = this.props;
  180. return _notifications.map(notification => {
  181. const { props, uid } = notification;
  182. // The id attribute is necessary as {@code FlagGroup} looks for
  183. // either id or key to set a key on notifications, but accessing
  184. // props.key will cause React to print an error.
  185. return (
  186. <CSSTransition
  187. appear = { true }
  188. classNames = 'notification'
  189. in = { true }
  190. key = { uid }
  191. timeout = { 200 }>
  192. <Notification
  193. { ...props }
  194. id = { uid }
  195. onDismissed = { this._onDismissed }
  196. uid = { uid } />
  197. </CSSTransition>
  198. );
  199. });
  200. }
  201. }
  202. /**
  203. * Maps (parts of) the Redux state to the associated props for this component.
  204. *
  205. * @param {Object} state - The Redux state.
  206. * @private
  207. * @returns {Props}
  208. */
  209. function _mapStateToProps(state) {
  210. const { notifications } = state['features/notifications'];
  211. const { iAmSipGateway } = state['features/base/config'];
  212. const { isOpen: isChatOpen } = state['features/chat'];
  213. const _visible = areThereNotifications(state);
  214. return {
  215. _iAmSipGateway: Boolean(iAmSipGateway),
  216. _isChatOpen: isChatOpen,
  217. _notifications: _visible ? notifications : []
  218. };
  219. }
  220. export default translate(connect(_mapStateToProps)(withStyles(useStyles)(NotificationsContainer)));