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

NotificationsContainer.js 7.0KB

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