2022-08-25 11:35:19 +00:00
|
|
|
/* eslint-disable lines-around-comment */
|
2021-10-04 14:07:05 +00:00
|
|
|
import { FlagGroupContext } from '@atlaskit/flag/flag-group';
|
|
|
|
import { AtlasKitThemeProvider } from '@atlaskit/theme';
|
2022-09-13 07:36:00 +00:00
|
|
|
import { Theme } from '@mui/material';
|
|
|
|
import { withStyles } from '@mui/styles';
|
2022-03-29 09:01:48 +00:00
|
|
|
import clsx from 'clsx';
|
2021-10-04 14:07:05 +00:00
|
|
|
import React, { Component } from 'react';
|
2022-08-25 11:35:19 +00:00
|
|
|
import { WithTranslation } from 'react-i18next';
|
2021-10-04 14:07:05 +00:00
|
|
|
import { CSSTransition, TransitionGroup } from 'react-transition-group';
|
2019-03-21 16:38:29 +00:00
|
|
|
|
2022-09-05 11:24:13 +00:00
|
|
|
import { IState } from '../../../app/types';
|
2022-08-25 11:35:19 +00:00
|
|
|
import { translate } from '../../../base/i18n/functions';
|
|
|
|
import { connect } from '../../../base/redux/functions';
|
|
|
|
// @ts-ignore
|
2021-10-04 14:07:05 +00:00
|
|
|
import { hideNotification } from '../../actions';
|
2022-08-25 11:35:19 +00:00
|
|
|
// @ts-ignore
|
2021-10-04 14:07:05 +00:00
|
|
|
import { areThereNotifications } from '../../functions';
|
2019-03-27 14:00:10 +00:00
|
|
|
|
2022-08-25 11:35:19 +00:00
|
|
|
// @ts-ignore
|
2018-06-14 09:14:32 +00:00
|
|
|
import Notification from './Notification';
|
2017-12-11 18:33:09 +00:00
|
|
|
|
2022-08-25 11:35:19 +00:00
|
|
|
interface Props extends WithTranslation {
|
2020-02-25 10:47:37 +00:00
|
|
|
|
|
|
|
/**
|
2021-03-16 15:59:33 +00:00
|
|
|
* Whether we are a SIP gateway or not.
|
2020-02-25 10:47:37 +00:00
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
_iAmSipGateway: boolean;
|
2021-10-04 14:07:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not the chat is open.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
_isChatOpen: boolean;
|
2021-10-04 14:07:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The notifications to be displayed, with the first index being the
|
|
|
|
* notification at the top and the rest shown below it in order.
|
|
|
|
*/
|
2022-08-25 11:35:19 +00:00
|
|
|
_notifications: Array<{
|
|
|
|
props: Object;
|
|
|
|
uid: number;
|
2022-09-08 09:52:36 +00:00
|
|
|
}>;
|
2021-10-04 14:07:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* JSS classes object.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
classes: any;
|
2021-10-04 14:07:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoked to update the redux store in order to remove notifications.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
dispatch: Function;
|
2021-10-04 14:07:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not the notifications are displayed in a portal.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
portal?: boolean;
|
2022-08-25 11:35:19 +00:00
|
|
|
}
|
2021-10-04 14:07:05 +00:00
|
|
|
|
2022-09-13 07:36:00 +00:00
|
|
|
const useStyles = (theme: Theme) => {
|
2021-10-04 14:07:05 +00:00
|
|
|
return {
|
|
|
|
container: {
|
2022-09-13 07:36:00 +00:00
|
|
|
position: 'absolute' as const,
|
2021-10-04 14:07:05 +00:00
|
|
|
left: '16px',
|
|
|
|
bottom: '90px',
|
|
|
|
width: '400px',
|
|
|
|
maxWidth: '100%',
|
|
|
|
zIndex: 600
|
|
|
|
},
|
|
|
|
|
|
|
|
containerPortal: {
|
|
|
|
maxWidth: 'calc(100% - 32px)'
|
|
|
|
},
|
|
|
|
|
|
|
|
transitionGroup: {
|
|
|
|
'& > *': {
|
|
|
|
marginBottom: '20px',
|
|
|
|
borderRadius: '6px!important', // !important used to overwrite atlaskit style
|
|
|
|
position: 'relative'
|
|
|
|
},
|
|
|
|
|
|
|
|
'& div > span > svg > path': {
|
|
|
|
fill: 'inherit'
|
|
|
|
},
|
|
|
|
|
|
|
|
'& div > span, & div > p': {
|
|
|
|
color: theme.palette.field01
|
|
|
|
},
|
|
|
|
|
2021-12-17 12:39:15 +00:00
|
|
|
'& div.message > span': {
|
|
|
|
color: theme.palette.link01Active
|
|
|
|
},
|
|
|
|
|
2021-10-04 14:07:05 +00:00
|
|
|
'& .ribbon': {
|
|
|
|
width: '4px',
|
|
|
|
height: 'calc(100% - 16px)',
|
|
|
|
position: 'absolute',
|
|
|
|
left: 0,
|
|
|
|
top: '8px',
|
|
|
|
borderRadius: '4px',
|
|
|
|
|
|
|
|
'&.normal': {
|
|
|
|
backgroundColor: theme.palette.link01Active
|
|
|
|
},
|
|
|
|
|
|
|
|
'&.error': {
|
|
|
|
backgroundColor: theme.palette.iconError
|
|
|
|
},
|
|
|
|
|
2022-03-08 09:10:30 +00:00
|
|
|
'&.success': {
|
|
|
|
backgroundColor: theme.palette.success01
|
|
|
|
},
|
|
|
|
|
2021-10-04 14:07:05 +00:00
|
|
|
'&.warning': {
|
|
|
|
backgroundColor: theme.palette.warning01
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-02-25 10:47:37 +00:00
|
|
|
};
|
|
|
|
|
2017-07-28 17:56:49 +00:00
|
|
|
/**
|
|
|
|
* Implements a React {@link Component} which displays notifications and handles
|
2021-10-04 14:07:05 +00:00
|
|
|
* automatic dismissal after a notification is shown for a defined timeout
|
2017-07-28 17:56:49 +00:00
|
|
|
* period.
|
|
|
|
*
|
2021-11-04 21:10:43 +00:00
|
|
|
* @augments {Component}
|
2017-07-28 17:56:49 +00:00
|
|
|
*/
|
2021-10-04 14:07:05 +00:00
|
|
|
class NotificationsContainer extends Component<Props> {
|
|
|
|
_api: Object;
|
2022-08-25 11:35:19 +00:00
|
|
|
_timeouts: Map<string, number>;
|
2021-10-04 14:07:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new {@code NotificationsContainer} instance.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this._timeouts = new Map();
|
|
|
|
|
|
|
|
// Bind event handlers so they are only bound once for every instance.
|
|
|
|
this._onDismissed = this._onDismissed.bind(this);
|
|
|
|
|
|
|
|
// HACK ALERT! We are rendering AtlasKit Flag elements outside of a FlagGroup container.
|
|
|
|
// In order to hook-up the dismiss action we'll a fake context provider,
|
|
|
|
// just like FlagGroup does.
|
|
|
|
this._api = {
|
|
|
|
onDismissed: this._onDismissed,
|
|
|
|
dismissAllowed: () => true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-28 17:56:49 +00:00
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#render()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
render() {
|
2020-02-25 10:47:37 +00:00
|
|
|
if (this.props._iAmSipGateway) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-07-28 17:56:49 +00:00
|
|
|
return (
|
2021-10-04 14:07:05 +00:00
|
|
|
<AtlasKitThemeProvider mode = 'light'>
|
2022-08-25 11:35:19 +00:00
|
|
|
{/* @ts-ignore */}
|
2021-10-04 14:07:05 +00:00
|
|
|
<FlagGroupContext.Provider value = { this._api }>
|
|
|
|
<div
|
2022-03-29 09:01:48 +00:00
|
|
|
className = { clsx(this.props.classes.container, {
|
|
|
|
[this.props.classes.containerPortal]: this.props.portal
|
|
|
|
}) }
|
2021-10-04 14:07:05 +00:00
|
|
|
id = 'notifications-container'>
|
|
|
|
<TransitionGroup className = { this.props.classes.transitionGroup }>
|
|
|
|
{this._renderFlags()}
|
|
|
|
</TransitionGroup>
|
|
|
|
</div>
|
|
|
|
</FlagGroupContext.Provider>
|
|
|
|
</AtlasKitThemeProvider>
|
2017-07-28 17:56:49 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-04 14:07:05 +00:00
|
|
|
/**
|
|
|
|
* Emits an action to remove the notification from the redux store so it
|
|
|
|
* stops displaying.
|
|
|
|
*
|
2022-09-21 08:32:50 +00:00
|
|
|
* @param {string} uid - The id of the notification to be removed.
|
2021-10-04 14:07:05 +00:00
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-09-21 08:32:50 +00:00
|
|
|
_onDismissed(uid: string) {
|
2022-08-25 11:35:19 +00:00
|
|
|
const timeout = this._timeouts.get(`${uid}`);
|
2021-10-04 14:07:05 +00:00
|
|
|
|
|
|
|
if (timeout) {
|
|
|
|
clearTimeout(timeout);
|
2022-08-25 11:35:19 +00:00
|
|
|
this._timeouts.delete(`${uid}`);
|
2021-10-04 14:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.props.dispatch(hideNotification(uid));
|
|
|
|
}
|
|
|
|
|
2017-08-02 18:15:55 +00:00
|
|
|
/**
|
|
|
|
* Renders notifications to display as ReactElements. An empty array will
|
|
|
|
* be returned if notifications are disabled.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {ReactElement[]}
|
|
|
|
*/
|
|
|
|
_renderFlags() {
|
2018-06-14 09:14:32 +00:00
|
|
|
const { _notifications } = this.props;
|
2017-08-02 18:15:55 +00:00
|
|
|
|
|
|
|
return _notifications.map(notification => {
|
|
|
|
const { props, uid } = notification;
|
|
|
|
|
|
|
|
// The id attribute is necessary as {@code FlagGroup} looks for
|
|
|
|
// either id or key to set a key on notifications, but accessing
|
|
|
|
// props.key will cause React to print an error.
|
|
|
|
return (
|
2021-10-04 14:07:05 +00:00
|
|
|
<CSSTransition
|
|
|
|
appear = { true }
|
|
|
|
classNames = 'notification'
|
|
|
|
in = { true }
|
2017-08-02 18:15:55 +00:00
|
|
|
key = { uid }
|
2021-10-04 14:07:05 +00:00
|
|
|
timeout = { 200 }>
|
|
|
|
<Notification
|
|
|
|
{ ...props }
|
|
|
|
id = { uid }
|
|
|
|
onDismissed = { this._onDismissed }
|
|
|
|
uid = { uid } />
|
|
|
|
</CSSTransition>
|
2017-08-02 18:15:55 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2017-07-28 17:56:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 10:47:37 +00:00
|
|
|
/**
|
|
|
|
* Maps (parts of) the Redux state to the associated props for this component.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The Redux state.
|
|
|
|
* @private
|
|
|
|
* @returns {Props}
|
|
|
|
*/
|
2022-09-05 11:24:13 +00:00
|
|
|
function _mapStateToProps(state: IState) {
|
2021-10-04 14:07:05 +00:00
|
|
|
const { notifications } = state['features/notifications'];
|
2020-02-25 10:47:37 +00:00
|
|
|
const { iAmSipGateway } = state['features/base/config'];
|
2021-10-04 14:07:05 +00:00
|
|
|
const { isOpen: isChatOpen } = state['features/chat'];
|
|
|
|
const _visible = areThereNotifications(state);
|
2020-02-25 10:47:37 +00:00
|
|
|
|
|
|
|
return {
|
2021-10-04 14:07:05 +00:00
|
|
|
_iAmSipGateway: Boolean(iAmSipGateway),
|
|
|
|
_isChatOpen: isChatOpen,
|
2021-11-24 11:05:27 +00:00
|
|
|
_notifications: _visible ? notifications : []
|
2020-02-25 10:47:37 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-04 14:07:05 +00:00
|
|
|
export default translate(connect(_mapStateToProps)(withStyles(useStyles)(NotificationsContainer)));
|