2017-11-16 18:26:14 +00:00
|
|
|
// @flow
|
|
|
|
|
2017-07-28 17:56:49 +00:00
|
|
|
import Flag from '@atlaskit/flag';
|
|
|
|
import EditorInfoIcon from '@atlaskit/icon/glyph/editor/info';
|
2017-11-03 19:05:03 +00:00
|
|
|
import ErrorIcon from '@atlaskit/icon/glyph/error';
|
|
|
|
import WarningIcon from '@atlaskit/icon/glyph/warning';
|
|
|
|
import { colors } from '@atlaskit/theme';
|
2017-11-16 18:26:14 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2017-07-28 17:56:49 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
|
|
|
|
import { translate } from '../../base/i18n';
|
|
|
|
|
2017-11-03 19:05:03 +00:00
|
|
|
import { NOTIFICATION_TYPE } from '../constants';
|
|
|
|
|
|
|
|
declare var interfaceConfig: Object;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Secondary colors for notification icons.
|
|
|
|
*
|
|
|
|
* @type {{error, info, normal, success, warning}}
|
|
|
|
*/
|
|
|
|
const ICON_COLOR = {
|
|
|
|
error: colors.R400,
|
|
|
|
info: colors.N500,
|
|
|
|
normal: colors.N0,
|
|
|
|
success: colors.G400,
|
|
|
|
warning: colors.Y200
|
|
|
|
};
|
|
|
|
|
2017-07-28 17:56:49 +00:00
|
|
|
/**
|
|
|
|
* Implements a React {@link Component} to display a notification.
|
|
|
|
*
|
|
|
|
* @extends Component
|
|
|
|
*/
|
2017-11-16 18:26:14 +00:00
|
|
|
class Notification extends Component<*> {
|
2017-11-03 19:05:03 +00:00
|
|
|
/**
|
|
|
|
* Default values for {@code Notification} component's properties.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
static defaultProps = {
|
|
|
|
appearance: NOTIFICATION_TYPE.NORMAL
|
|
|
|
};
|
|
|
|
|
2017-07-28 17:56:49 +00:00
|
|
|
/**
|
|
|
|
* {@code Notification} component's property types.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
static propTypes = {
|
|
|
|
/**
|
2017-11-03 19:05:03 +00:00
|
|
|
* Display appearance for the component, passed directly to
|
|
|
|
* {@code Flag}.
|
|
|
|
*/
|
|
|
|
appearance: PropTypes.string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The text to display in the body of the notification. If not passed
|
|
|
|
* in, the passed in descriptionKey will be used.
|
2017-07-28 17:56:49 +00:00
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
defaultTitleKey: PropTypes.string,
|
2017-07-28 17:56:49 +00:00
|
|
|
|
2017-11-03 19:05:03 +00:00
|
|
|
/**
|
2017-11-16 17:25:04 +00:00
|
|
|
* A description string that can be used in addition to the prop
|
|
|
|
* descriptionKey.
|
2017-11-03 19:05:03 +00:00
|
|
|
*/
|
|
|
|
description: PropTypes.string,
|
|
|
|
|
2017-07-28 17:56:49 +00:00
|
|
|
/**
|
|
|
|
* The translation arguments that may be necessary for the description.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
descriptionArguments: PropTypes.object,
|
2017-07-28 17:56:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The translation key to use as the body of the notification.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
descriptionKey: PropTypes.string,
|
2017-07-28 17:56:49 +00:00
|
|
|
|
2017-11-03 19:05:03 +00:00
|
|
|
/**
|
|
|
|
* Whether the support link should be hidden in the case of an error
|
|
|
|
* message.
|
|
|
|
*/
|
|
|
|
hideErrorSupportLink: PropTypes.bool,
|
|
|
|
|
2017-07-28 17:56:49 +00:00
|
|
|
/**
|
|
|
|
* Whether or not the dismiss button should be displayed. This is passed
|
|
|
|
* in by {@code FlagGroup}.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
isDismissAllowed: PropTypes.bool,
|
2017-07-28 17:56:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback invoked when the user clicks to dismiss the notification.
|
|
|
|
* this is passed in by {@code FlagGroup}.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
onDismissed: PropTypes.func,
|
2017-07-28 17:56:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoked to obtain translated strings.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
t: PropTypes.func,
|
2017-07-28 17:56:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The text to display at the top of the notification. If not passed in,
|
2017-11-03 19:05:03 +00:00
|
|
|
* the passed in titleKey will be used.
|
2017-07-28 17:56:49 +00:00
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
title: PropTypes.string,
|
2017-07-28 17:56:49 +00:00
|
|
|
|
2017-11-21 17:38:40 +00:00
|
|
|
/**
|
|
|
|
* The translation arguments that may be necessary for the title.
|
|
|
|
*/
|
|
|
|
titleArguments: PropTypes.object,
|
|
|
|
|
2017-11-03 19:05:03 +00:00
|
|
|
/**
|
|
|
|
* The translation key to display as the title of the notification if
|
|
|
|
* no title is provided.
|
|
|
|
*/
|
|
|
|
titleKey: PropTypes.string,
|
|
|
|
|
2017-07-28 17:56:49 +00:00
|
|
|
/**
|
|
|
|
* The unique identifier for the notification. Passed back by the
|
|
|
|
* {@code Flag} component in the onDismissed callback.
|
|
|
|
*/
|
2017-09-27 21:23:31 +00:00
|
|
|
uid: PropTypes.number
|
2017-07-28 17:56:49 +00:00
|
|
|
};
|
|
|
|
|
2017-11-03 19:05:03 +00:00
|
|
|
/**
|
|
|
|
* Initializes a new {@code Notification} instance.
|
|
|
|
*
|
|
|
|
* @param {Object} props - The read-only properties with which the new
|
|
|
|
* instance is to be initialized.
|
|
|
|
*/
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
// Bind event handler so it is only bound once for every instance.
|
|
|
|
this._onDismissed = this._onDismissed.bind(this);
|
|
|
|
}
|
|
|
|
|
2017-07-28 17:56:49 +00:00
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#render()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
render() {
|
|
|
|
const {
|
2017-11-03 19:05:03 +00:00
|
|
|
appearance,
|
2017-11-21 17:38:40 +00:00
|
|
|
hideErrorSupportLink,
|
2017-07-28 17:56:49 +00:00
|
|
|
isDismissAllowed,
|
|
|
|
onDismissed,
|
|
|
|
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) }
|
|
|
|
appearance = { appearance }
|
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 }
|
|
|
|
isDismissAllowed = { isDismissAllowed }
|
|
|
|
onDismissed = { onDismissed }
|
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
|
|
|
|
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() {
|
|
|
|
const {
|
|
|
|
description,
|
|
|
|
descriptionArguments,
|
|
|
|
descriptionKey,
|
|
|
|
t
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{ descriptionKey
|
|
|
|
? t(descriptionKey, descriptionArguments) : null }
|
|
|
|
{ description || null }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-03 19:05:03 +00:00
|
|
|
/**
|
|
|
|
* Calls back into {@code FlagGroup} to dismiss the notification.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onDismissed() {
|
|
|
|
this.props.onDismissed(this.props.uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
if (!hideErrorSupportLink) {
|
|
|
|
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:
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an icon component depending on the configured notification
|
|
|
|
* appearance.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
_mapAppearanceToIcon() {
|
|
|
|
const appearance = this.props.appearance;
|
|
|
|
const secIconColor = ICON_COLOR[this.props.appearance];
|
|
|
|
const iconSize = 'medium';
|
|
|
|
|
|
|
|
switch (appearance) {
|
|
|
|
case NOTIFICATION_TYPE.ERROR:
|
|
|
|
return (
|
|
|
|
<ErrorIcon
|
|
|
|
label = { appearance }
|
|
|
|
secondaryColor = { secIconColor }
|
|
|
|
size = { iconSize } />
|
|
|
|
);
|
|
|
|
|
|
|
|
case NOTIFICATION_TYPE.WARNING:
|
|
|
|
return (
|
|
|
|
<WarningIcon
|
|
|
|
label = { appearance }
|
|
|
|
secondaryColor = { secIconColor }
|
|
|
|
size = { iconSize } />
|
|
|
|
);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return (
|
|
|
|
<EditorInfoIcon
|
|
|
|
label = { appearance }
|
|
|
|
secondaryColor = { secIconColor }
|
|
|
|
size = { iconSize } />
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-07-28 17:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default translate(Notification);
|