2017-11-16 18:26:14 +00:00
|
|
|
// @flow
|
|
|
|
|
2017-07-28 17:56:49 +00:00
|
|
|
import Flag from '@atlaskit/flag';
|
2021-12-17 12:39:15 +00:00
|
|
|
import EditorErrorIcon from '@atlaskit/icon/glyph/editor/error';
|
2017-07-28 17:56:49 +00:00
|
|
|
import EditorInfoIcon from '@atlaskit/icon/glyph/editor/info';
|
2021-12-17 12:39:15 +00:00
|
|
|
import EditorSuccessIcon from '@atlaskit/icon/glyph/editor/success';
|
|
|
|
import EditorWarningIcon from '@atlaskit/icon/glyph/editor/warning';
|
2022-01-20 14:26:03 +00:00
|
|
|
import PeopleIcon from '@atlaskit/icon/glyph/people';
|
|
|
|
import PersonIcon from '@atlaskit/icon/glyph/person';
|
2021-12-17 12:39:15 +00:00
|
|
|
import QuestionsIcon from '@atlaskit/icon/glyph/questions';
|
2022-01-26 10:10:48 +00:00
|
|
|
import React, { isValidElement } from 'react';
|
2017-07-28 17:56:49 +00:00
|
|
|
|
2019-03-27 14:00:10 +00:00
|
|
|
import { translate } from '../../../base/i18n';
|
2021-12-17 12:39:15 +00:00
|
|
|
import Message from '../../../base/react/components/web/Message';
|
2021-10-04 14:07:05 +00:00
|
|
|
import { colors } from '../../../base/ui/Tokens';
|
2021-12-17 12:39:15 +00:00
|
|
|
import { NOTIFICATION_ICON, NOTIFICATION_TYPE } from '../../constants';
|
2018-06-14 09:14:32 +00:00
|
|
|
import AbstractNotification, {
|
|
|
|
type Props
|
2019-03-27 14:00:10 +00:00
|
|
|
} from '../AbstractNotification';
|
2018-06-14 09:14:32 +00:00
|
|
|
|
2017-11-03 19:05:03 +00:00
|
|
|
declare var interfaceConfig: Object;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Secondary colors for notification icons.
|
|
|
|
*
|
|
|
|
* @type {{error, info, normal, success, warning}}
|
|
|
|
*/
|
|
|
|
const ICON_COLOR = {
|
2021-10-04 14:07:05 +00:00
|
|
|
error: colors.error06,
|
|
|
|
normal: colors.primary06,
|
2022-03-08 09:10:30 +00:00
|
|
|
success: colors.success05,
|
2021-10-04 14:07:05 +00:00
|
|
|
warning: colors.warning05
|
2017-11-03 19:05:03 +00:00
|
|
|
};
|
|
|
|
|
2017-07-28 17:56:49 +00:00
|
|
|
/**
|
|
|
|
* Implements a React {@link Component} to display a notification.
|
|
|
|
*
|
2021-11-04 21:10:43 +00:00
|
|
|
* @augments Component
|
2017-07-28 17:56:49 +00:00
|
|
|
*/
|
2018-06-14 09:14:32 +00:00
|
|
|
class Notification extends AbstractNotification<Props> {
|
2017-07-28 17:56:49 +00:00
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#render()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
render() {
|
|
|
|
const {
|
2017-11-21 17:38:40 +00:00
|
|
|
hideErrorSupportLink,
|
2017-07-28 17:56:49 +00:00
|
|
|
t,
|
|
|
|
title,
|
2017-11-21 17:38:40 +00:00
|
|
|
titleArguments,
|
|
|
|
titleKey,
|
2017-07-28 17:56:49 +00:00
|
|
|
uid
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Flag
|
2017-11-03 19:05:03 +00:00
|
|
|
actions = { this._mapAppearanceToButtons(hideErrorSupportLink) }
|
2017-11-16 17:25:04 +00:00
|
|
|
description = { this._renderDescription() }
|
2017-11-03 19:05:03 +00:00
|
|
|
icon = { this._mapAppearanceToIcon() }
|
2017-07-28 17:56:49 +00:00
|
|
|
id = { uid }
|
2022-04-08 19:11:37 +00:00
|
|
|
testId = { titleKey || this._getDescriptionKey() }
|
2017-11-21 17:38:40 +00:00
|
|
|
title = { title || t(titleKey, titleArguments) } />
|
2017-07-28 17:56:49 +00:00
|
|
|
);
|
|
|
|
}
|
2017-11-03 19:05:03 +00:00
|
|
|
|
2021-11-04 21:10:43 +00:00
|
|
|
_getDescription: () => Array<string>;
|
2018-06-14 09:14:32 +00:00
|
|
|
|
2021-11-04 21:10:43 +00:00
|
|
|
_getDescriptionKey: () => string;
|
2020-07-10 15:28:57 +00:00
|
|
|
|
2017-11-16 18:26:14 +00:00
|
|
|
_onDismissed: () => void;
|
|
|
|
|
2017-11-16 17:25:04 +00:00
|
|
|
/**
|
|
|
|
* Creates a {@code ReactElement} for displaying the contents of the
|
|
|
|
* notification.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
_renderDescription() {
|
2022-01-26 10:10:48 +00:00
|
|
|
const description = this._getDescription();
|
|
|
|
|
|
|
|
// Keeping in mind that:
|
|
|
|
// - Notifications that use the `translateToHtml` function get an element-based description array with one entry
|
|
|
|
// - Message notifications receive string-based description arrays that might need additional parsing
|
|
|
|
// We look for ready-to-render elements, and if present, we roll with them
|
|
|
|
// Otherwise, we use the Message component that accepts a string `text` prop
|
|
|
|
const shouldRenderHtml = description.length === 1 && isValidElement(description[0]);
|
2020-07-10 15:28:57 +00:00
|
|
|
|
|
|
|
// the id is used for testing the UI
|
2017-11-16 17:25:04 +00:00
|
|
|
return (
|
2021-06-10 12:48:44 +00:00
|
|
|
<p data-testid = { this._getDescriptionKey() } >
|
2022-01-26 10:10:48 +00:00
|
|
|
{ shouldRenderHtml ? description : <Message text = { description.join(' ') } /> }
|
2021-06-10 12:48:44 +00:00
|
|
|
</p>
|
2017-11-16 17:25:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-03 19:05:03 +00:00
|
|
|
/**
|
|
|
|
* Opens the support page.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_onOpenSupportLink() {
|
|
|
|
window.open(interfaceConfig.SUPPORT_URL, '_blank', 'noopener');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates action button configurations for the notification based on
|
|
|
|
* notification appearance.
|
|
|
|
*
|
|
|
|
* @param {boolean} hideErrorSupportLink - Indicates if the support link
|
|
|
|
* should be hidden in the error messages.
|
|
|
|
* @private
|
|
|
|
* @returns {Object[]}
|
|
|
|
*/
|
|
|
|
_mapAppearanceToButtons(hideErrorSupportLink) {
|
|
|
|
switch (this.props.appearance) {
|
|
|
|
case NOTIFICATION_TYPE.ERROR: {
|
|
|
|
const buttons = [
|
|
|
|
{
|
|
|
|
content: this.props.t('dialog.dismiss'),
|
|
|
|
onClick: this._onDismissed
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2021-07-16 11:00:11 +00:00
|
|
|
if (!hideErrorSupportLink && interfaceConfig.SUPPORT_URL) {
|
2017-11-03 19:05:03 +00:00
|
|
|
buttons.push({
|
|
|
|
content: this.props.t('dialog.contactSupport'),
|
|
|
|
onClick: this._onOpenSupportLink
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return buttons;
|
|
|
|
}
|
|
|
|
case NOTIFICATION_TYPE.WARNING:
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
content: this.props.t('dialog.Ok'),
|
|
|
|
onClick: this._onDismissed
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
default:
|
2021-11-10 11:19:40 +00:00
|
|
|
if (this.props.customActionNameKey?.length && this.props.customActionHandler?.length) {
|
|
|
|
return this.props.customActionNameKey.map((customAction: string, customActionIndex: number) => {
|
|
|
|
return {
|
|
|
|
content: this.props.t(customAction),
|
2019-05-03 17:25:33 +00:00
|
|
|
onClick: () => {
|
2021-11-10 11:19:40 +00:00
|
|
|
if (this.props.customActionHandler[customActionIndex]()) {
|
2019-05-03 17:25:33 +00:00
|
|
|
this._onDismissed();
|
|
|
|
}
|
2022-01-23 11:02:33 +00:00
|
|
|
},
|
2022-01-25 07:45:39 +00:00
|
|
|
testId: customAction
|
2021-11-10 11:19:40 +00:00
|
|
|
};
|
|
|
|
});
|
2019-05-03 17:25:33 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 19:05:03 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-17 12:39:15 +00:00
|
|
|
/**
|
|
|
|
* Returns the Icon type component to be used, based on icon or appearance.
|
|
|
|
*
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
_getIcon() {
|
|
|
|
let Icon;
|
|
|
|
|
|
|
|
switch (this.props.icon || this.props.appearance) {
|
|
|
|
case NOTIFICATION_ICON.ERROR:
|
|
|
|
Icon = EditorErrorIcon;
|
|
|
|
break;
|
|
|
|
case NOTIFICATION_ICON.WARNING:
|
|
|
|
Icon = EditorWarningIcon;
|
|
|
|
break;
|
|
|
|
case NOTIFICATION_ICON.SUCCESS:
|
|
|
|
Icon = EditorSuccessIcon;
|
|
|
|
break;
|
|
|
|
case NOTIFICATION_ICON.MESSAGE:
|
|
|
|
Icon = QuestionsIcon;
|
|
|
|
break;
|
2022-01-20 14:26:03 +00:00
|
|
|
case NOTIFICATION_ICON.PARTICIPANT:
|
|
|
|
Icon = PersonIcon;
|
|
|
|
break;
|
|
|
|
case NOTIFICATION_ICON.PARTICIPANTS:
|
|
|
|
Icon = PeopleIcon;
|
|
|
|
break;
|
2021-12-17 12:39:15 +00:00
|
|
|
default:
|
|
|
|
Icon = EditorInfoIcon;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Icon;
|
|
|
|
}
|
|
|
|
|
2017-11-03 19:05:03 +00:00
|
|
|
/**
|
|
|
|
* Creates an icon component depending on the configured notification
|
|
|
|
* appearance.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
_mapAppearanceToIcon() {
|
2021-12-17 12:39:15 +00:00
|
|
|
const { appearance, icon } = this.props;
|
|
|
|
const secIconColor = ICON_COLOR[appearance];
|
2017-11-03 19:05:03 +00:00
|
|
|
const iconSize = 'medium';
|
2021-12-17 12:39:15 +00:00
|
|
|
const Icon = this._getIcon();
|
2017-11-03 19:05:03 +00:00
|
|
|
|
2021-12-17 12:39:15 +00:00
|
|
|
return (<div className = { icon }>
|
2021-10-04 14:07:05 +00:00
|
|
|
<div className = { `ribbon ${appearance}` } />
|
2021-12-17 12:39:15 +00:00
|
|
|
<Icon
|
2021-10-04 14:07:05 +00:00
|
|
|
label = { appearance }
|
|
|
|
secondaryColor = { secIconColor }
|
|
|
|
size = { iconSize } />
|
2021-12-17 12:39:15 +00:00
|
|
|
</div>);
|
2017-11-03 19:05:03 +00:00
|
|
|
}
|
2017-07-28 17:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default translate(Notification);
|