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

NotificationsContainer.tsx 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. /**
  107. * Initializes a new {@code NotificationsContainer} instance.
  108. *
  109. * @inheritdoc
  110. */
  111. constructor(props: IProps) {
  112. super(props);
  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. {/* @ts-ignore */}
  136. <FlagGroupContext.Provider value = { this._api }>
  137. <div
  138. className = { clsx(this.props.classes.container, {
  139. [this.props.classes.containerPortal]: this.props.portal
  140. }) }
  141. id = 'notifications-container'>
  142. <TransitionGroup className = { this.props.classes.transitionGroup }>
  143. {this._renderFlags()}
  144. </TransitionGroup>
  145. </div>
  146. </FlagGroupContext.Provider>
  147. </AtlasKitThemeProvider>
  148. );
  149. }
  150. /**
  151. * Emits an action to remove the notification from the redux store so it
  152. * stops displaying.
  153. *
  154. * @param {string} uid - The id of the notification to be removed.
  155. * @private
  156. * @returns {void}
  157. */
  158. _onDismissed(uid: string) {
  159. this.props.dispatch(hideNotification(uid));
  160. }
  161. /**
  162. * Renders notifications to display as ReactElements. An empty array will
  163. * be returned if notifications are disabled.
  164. *
  165. * @private
  166. * @returns {ReactElement[]}
  167. */
  168. _renderFlags() {
  169. const { _notifications } = this.props;
  170. return _notifications.map(notification => {
  171. const { props, uid } = notification;
  172. // The id attribute is necessary as {@code FlagGroup} looks for
  173. // either id or key to set a key on notifications, but accessing
  174. // props.key will cause React to print an error.
  175. return (
  176. <CSSTransition
  177. appear = { true }
  178. classNames = 'notification'
  179. in = { true }
  180. key = { uid }
  181. timeout = { 200 }>
  182. <Notification
  183. { ...props }
  184. id = { uid }
  185. onDismissed = { this._onDismissed }
  186. uid = { uid } />
  187. </CSSTransition>
  188. );
  189. });
  190. }
  191. }
  192. /**
  193. * Maps (parts of) the Redux state to the associated props for this component.
  194. *
  195. * @param {Object} state - The Redux state.
  196. * @private
  197. * @returns {IProps}
  198. */
  199. function _mapStateToProps(state: IReduxState) {
  200. const { notifications } = state['features/notifications'];
  201. const { iAmSipGateway } = state['features/base/config'];
  202. const { isOpen: isChatOpen } = state['features/chat'];
  203. const _visible = areThereNotifications(state);
  204. return {
  205. _iAmSipGateway: Boolean(iAmSipGateway),
  206. _isChatOpen: isChatOpen,
  207. _notifications: _visible ? notifications : []
  208. };
  209. }
  210. export default translate(connect(_mapStateToProps)(withStyles(useStyles)(NotificationsContainer)));