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

NotificationsContainer.js 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. '& .ribbon': {
  72. width: '4px',
  73. height: 'calc(100% - 16px)',
  74. position: 'absolute',
  75. left: 0,
  76. top: '8px',
  77. borderRadius: '4px',
  78. '&.normal': {
  79. backgroundColor: theme.palette.link01Active
  80. },
  81. '&.error': {
  82. backgroundColor: theme.palette.iconError
  83. },
  84. '&.warning': {
  85. backgroundColor: theme.palette.warning01
  86. }
  87. }
  88. }
  89. };
  90. };
  91. /**
  92. * Implements a React {@link Component} which displays notifications and handles
  93. * automatic dismissal after a notification is shown for a defined timeout
  94. * period.
  95. *
  96. * @augments {Component}
  97. */
  98. class NotificationsContainer extends Component<Props> {
  99. _api: Object;
  100. _timeouts: Map<string, TimeoutID>;
  101. /**
  102. * Initializes a new {@code NotificationsContainer} instance.
  103. *
  104. * @inheritdoc
  105. */
  106. constructor(props: Props) {
  107. super(props);
  108. this._timeouts = new Map();
  109. // Bind event handlers so they are only bound once for every instance.
  110. this._onDismissed = this._onDismissed.bind(this);
  111. // HACK ALERT! We are rendering AtlasKit Flag elements outside of a FlagGroup container.
  112. // In order to hook-up the dismiss action we'll a fake context provider,
  113. // just like FlagGroup does.
  114. this._api = {
  115. onDismissed: this._onDismissed,
  116. dismissAllowed: () => true
  117. };
  118. }
  119. /**
  120. * Sets a timeout for each notification, where applicable.
  121. *
  122. * @inheritdoc
  123. */
  124. componentDidMount() {
  125. this._updateTimeouts();
  126. }
  127. /**
  128. * Sets a timeout for each notification, where applicable.
  129. *
  130. * @inheritdoc
  131. */
  132. componentDidUpdate() {
  133. this._updateTimeouts();
  134. }
  135. /**
  136. * Implements React's {@link Component#render()}.
  137. *
  138. * @inheritdoc
  139. * @returns {ReactElement}
  140. */
  141. render() {
  142. if (this.props._iAmSipGateway) {
  143. return null;
  144. }
  145. return (
  146. <AtlasKitThemeProvider mode = 'light'>
  147. <FlagGroupContext.Provider value = { this._api }>
  148. <div
  149. className = { `${this.props.classes.container} ${this.props.portal
  150. ? this.props.classes.containerPortal
  151. : this.props._isChatOpen
  152. ? this.props.classes.containerChatOpen
  153. : ''}`
  154. }
  155. id = 'notifications-container'>
  156. <TransitionGroup className = { this.props.classes.transitionGroup }>
  157. {this._renderFlags()}
  158. </TransitionGroup>
  159. </div>
  160. </FlagGroupContext.Provider>
  161. </AtlasKitThemeProvider>
  162. );
  163. }
  164. _onDismissed: number => void;
  165. /**
  166. * Emits an action to remove the notification from the redux store so it
  167. * stops displaying.
  168. *
  169. * @param {number} uid - The id of the notification to be removed.
  170. * @private
  171. * @returns {void}
  172. */
  173. _onDismissed(uid) {
  174. const timeout = this._timeouts.get(uid);
  175. if (timeout) {
  176. clearTimeout(timeout);
  177. this._timeouts.delete(uid);
  178. }
  179. this.props.dispatch(hideNotification(uid));
  180. }
  181. /**
  182. * Renders notifications to display as ReactElements. An empty array will
  183. * be returned if notifications are disabled.
  184. *
  185. * @private
  186. * @returns {ReactElement[]}
  187. */
  188. _renderFlags() {
  189. const { _notifications } = this.props;
  190. return _notifications.map(notification => {
  191. const { props, uid } = notification;
  192. // The id attribute is necessary as {@code FlagGroup} looks for
  193. // either id or key to set a key on notifications, but accessing
  194. // props.key will cause React to print an error.
  195. return (
  196. <CSSTransition
  197. appear = { true }
  198. classNames = 'notification'
  199. in = { true }
  200. key = { uid }
  201. timeout = { 200 }>
  202. <Notification
  203. { ...props }
  204. id = { uid }
  205. onDismissed = { this._onDismissed }
  206. uid = { uid } />
  207. </CSSTransition>
  208. );
  209. });
  210. }
  211. /**
  212. * Updates the timeouts for every notification.
  213. *
  214. * @returns {void}
  215. */
  216. _updateTimeouts() {
  217. const { _notifications } = this.props;
  218. for (const notification of _notifications) {
  219. if (notification.timeout
  220. && notification.props.isDismissAllowed !== false
  221. && !this._timeouts.has(notification.uid)) {
  222. const {
  223. timeout,
  224. uid
  225. } = notification;
  226. const timerID = setTimeout(() => {
  227. this._onDismissed(uid);
  228. }, timeout);
  229. this._timeouts.set(uid, timerID);
  230. }
  231. }
  232. }
  233. }
  234. /**
  235. * Maps (parts of) the Redux state to the associated props for this component.
  236. *
  237. * @param {Object} state - The Redux state.
  238. * @private
  239. * @returns {Props}
  240. */
  241. function _mapStateToProps(state) {
  242. const { notifications } = state['features/notifications'];
  243. const { iAmSipGateway } = state['features/base/config'];
  244. const { isOpen: isChatOpen } = state['features/chat'];
  245. const _visible = areThereNotifications(state);
  246. return {
  247. _iAmSipGateway: Boolean(iAmSipGateway),
  248. _isChatOpen: isChatOpen,
  249. _notifications: _visible ? notifications : []
  250. };
  251. }
  252. export default translate(connect(_mapStateToProps)(withStyles(useStyles)(NotificationsContainer)));