Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

NotificationsContainer.tsx 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* eslint-disable lines-around-comment */
  2. import { FlagGroupContext } from '@atlaskit/flag/flag-group';
  3. import { AtlasKitThemeProvider } from '@atlaskit/theme';
  4. import { Theme } from '@mui/material';
  5. import { withStyles } from '@mui/styles';
  6. import clsx from 'clsx';
  7. import React, { Component } from 'react';
  8. import { WithTranslation } from 'react-i18next';
  9. import { CSSTransition, TransitionGroup } from 'react-transition-group';
  10. import { IState } from '../../../app/types';
  11. import { translate } from '../../../base/i18n/functions';
  12. import { connect } from '../../../base/redux/functions';
  13. // @ts-ignore
  14. import { hideNotification } from '../../actions';
  15. // @ts-ignore
  16. import { areThereNotifications } from '../../functions';
  17. // @ts-ignore
  18. import Notification from './Notification';
  19. interface Props extends WithTranslation {
  20. /**
  21. * Whether we are a SIP gateway or not.
  22. */
  23. _iAmSipGateway: boolean;
  24. /**
  25. * Whether or not the chat is open.
  26. */
  27. _isChatOpen: boolean;
  28. /**
  29. * The notifications to be displayed, with the first index being the
  30. * notification at the top and the rest shown below it in order.
  31. */
  32. _notifications: Array<{
  33. props: Object;
  34. uid: number;
  35. }>;
  36. /**
  37. * JSS classes object.
  38. */
  39. classes: any;
  40. /**
  41. * Invoked to update the redux store in order to remove notifications.
  42. */
  43. dispatch: Function;
  44. /**
  45. * Whether or not the notifications are displayed in a portal.
  46. */
  47. portal?: boolean;
  48. }
  49. const useStyles = (theme: Theme) => {
  50. return {
  51. container: {
  52. position: 'absolute' as const,
  53. left: '16px',
  54. bottom: '90px',
  55. width: '400px',
  56. maxWidth: '100%',
  57. zIndex: 600
  58. },
  59. containerPortal: {
  60. maxWidth: 'calc(100% - 32px)'
  61. },
  62. transitionGroup: {
  63. '& > *': {
  64. marginBottom: '20px',
  65. borderRadius: '6px!important', // !important used to overwrite atlaskit style
  66. position: 'relative'
  67. },
  68. '& div > span > svg > path': {
  69. fill: 'inherit'
  70. },
  71. '& div > span, & div > p': {
  72. color: theme.palette.field01
  73. },
  74. '& div.message > span': {
  75. color: theme.palette.link01Active
  76. },
  77. '& .ribbon': {
  78. width: '4px',
  79. height: 'calc(100% - 16px)',
  80. position: 'absolute',
  81. left: 0,
  82. top: '8px',
  83. borderRadius: '4px',
  84. '&.normal': {
  85. backgroundColor: theme.palette.link01Active
  86. },
  87. '&.error': {
  88. backgroundColor: theme.palette.iconError
  89. },
  90. '&.success': {
  91. backgroundColor: theme.palette.success01
  92. },
  93. '&.warning': {
  94. backgroundColor: theme.palette.warning01
  95. }
  96. }
  97. }
  98. };
  99. };
  100. /**
  101. * Implements a React {@link Component} which displays notifications and handles
  102. * automatic dismissal after a notification is shown for a defined timeout
  103. * period.
  104. *
  105. * @augments {Component}
  106. */
  107. class NotificationsContainer extends Component<Props> {
  108. _api: Object;
  109. _timeouts: Map<string, number>;
  110. /**
  111. * Initializes a new {@code NotificationsContainer} instance.
  112. *
  113. * @inheritdoc
  114. */
  115. constructor(props: Props) {
  116. super(props);
  117. this._timeouts = new Map();
  118. // Bind event handlers so they are only bound once for every instance.
  119. this._onDismissed = this._onDismissed.bind(this);
  120. // HACK ALERT! We are rendering AtlasKit Flag elements outside of a FlagGroup container.
  121. // In order to hook-up the dismiss action we'll a fake context provider,
  122. // just like FlagGroup does.
  123. this._api = {
  124. onDismissed: this._onDismissed,
  125. dismissAllowed: () => true
  126. };
  127. }
  128. /**
  129. * Implements React's {@link Component#render()}.
  130. *
  131. * @inheritdoc
  132. * @returns {ReactElement}
  133. */
  134. render() {
  135. if (this.props._iAmSipGateway) {
  136. return null;
  137. }
  138. return (
  139. <AtlasKitThemeProvider mode = 'light'>
  140. {/* @ts-ignore */}
  141. <FlagGroupContext.Provider value = { this._api }>
  142. <div
  143. className = { clsx(this.props.classes.container, {
  144. [this.props.classes.containerPortal]: this.props.portal
  145. }) }
  146. id = 'notifications-container'>
  147. <TransitionGroup className = { this.props.classes.transitionGroup }>
  148. {this._renderFlags()}
  149. </TransitionGroup>
  150. </div>
  151. </FlagGroupContext.Provider>
  152. </AtlasKitThemeProvider>
  153. );
  154. }
  155. /**
  156. * Emits an action to remove the notification from the redux store so it
  157. * stops displaying.
  158. *
  159. * @param {string} uid - The id of the notification to be removed.
  160. * @private
  161. * @returns {void}
  162. */
  163. _onDismissed(uid: string) {
  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: IState) {
  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)));