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.tsx 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import { FlagGroupContext } from '@atlaskit/flag/flag-group';
  2. import { AtlasKitThemeProvider } from '@atlaskit/theme';
  3. import { Theme } from '@mui/material/styles';
  4. import { withStyles } from '@mui/styles';
  5. import clsx from 'clsx';
  6. import React, { Component } from 'react';
  7. import { WithTranslation } from 'react-i18next';
  8. import { CSSTransition, TransitionGroup } from 'react-transition-group';
  9. import { IReduxState } from '../../../app/types';
  10. import { translate } from '../../../base/i18n/functions';
  11. import { connect } from '../../../base/redux/functions';
  12. import { hideNotification } from '../../actions';
  13. import { areThereNotifications } from '../../functions';
  14. // @ts-ignore
  15. import Notification from './Notification';
  16. interface IProps extends WithTranslation {
  17. /**
  18. * Whether we are a SIP gateway or not.
  19. */
  20. _iAmSipGateway: boolean;
  21. /**
  22. * Whether or not the chat is open.
  23. */
  24. _isChatOpen: boolean;
  25. /**
  26. * The notifications to be displayed, with the first index being the
  27. * notification at the top and the rest shown below it in order.
  28. */
  29. _notifications: Array<{
  30. props: Object;
  31. uid: number;
  32. }>;
  33. /**
  34. * JSS classes object.
  35. */
  36. classes: any;
  37. /**
  38. * Invoked to update the redux store in order to remove notifications.
  39. */
  40. dispatch: Function;
  41. /**
  42. * Whether or not the notifications are displayed in a portal.
  43. */
  44. portal?: boolean;
  45. }
  46. const useStyles = (theme: Theme) => {
  47. return {
  48. container: {
  49. position: 'absolute' as const,
  50. left: '16px',
  51. bottom: '90px',
  52. width: '400px',
  53. maxWidth: '100%',
  54. zIndex: 600
  55. },
  56. containerPortal: {
  57. maxWidth: 'calc(100% - 32px)'
  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<IProps> {
  105. _api: Object;
  106. _timeouts: Map<string, number>;
  107. /**
  108. * Initializes a new {@code NotificationsContainer} instance.
  109. *
  110. * @inheritdoc
  111. */
  112. constructor(props: IProps) {
  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. {/* @ts-ignore */}
  138. <FlagGroupContext.Provider value = { this._api }>
  139. <div
  140. className = { clsx(this.props.classes.container, {
  141. [this.props.classes.containerPortal]: this.props.portal
  142. }) }
  143. id = 'notifications-container'>
  144. <TransitionGroup className = { this.props.classes.transitionGroup }>
  145. {this._renderFlags()}
  146. </TransitionGroup>
  147. </div>
  148. </FlagGroupContext.Provider>
  149. </AtlasKitThemeProvider>
  150. );
  151. }
  152. /**
  153. * Emits an action to remove the notification from the redux store so it
  154. * stops displaying.
  155. *
  156. * @param {string} uid - The id of the notification to be removed.
  157. * @private
  158. * @returns {void}
  159. */
  160. _onDismissed(uid: string) {
  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 {IProps}
  205. */
  206. function _mapStateToProps(state: IReduxState) {
  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)));