import React from 'react';
import { notification } from 'antd';
import PropTypes from 'prop-types';
import {
InfoCircleOutlined,
CheckCircleOutlined,
CloseCircleOutlined,
LoadingOutlined,
} from '@ant-design/icons';
import CodeEditor from 'components/CodeEditor';
import ModalButton from 'components/ModalButton';
import globalRootStore from 'stores/root';
import { unescapeHtml } from 'utils/index';
import { statusMap } from 'src/utils/code';
import { isEmpty, isString } from 'lodash';
import { decode } from 'html-entities';
import styles from './index.less';
const open = (args) => {
const {
title = t('Error'),
type = 'error',
description = '',
onClose,
top = 48,
} = args;
let iconColor = '#F5222D';
let icon = null;
if (type === 'info') {
iconColor = globalCSS.primaryColor;
icon = <InfoCircleOutlined theme="filled" style={{ color: iconColor }} />;
} else if (type === 'success') {
iconColor = globalCSS.successColor;
icon = <CheckCircleOutlined theme="filled" style={{ color: iconColor }} />;
} else if (type === 'error') {
iconColor = globalCSS.errorColor;
icon = <CloseCircleOutlined theme="filled" style={{ color: iconColor }} />;
} else if (type === 'process') {
iconColor = globalCSS.primaryColor;
icon = <LoadingOutlined style={{ color: iconColor }} />;
} else if (type === 'warn') {
iconColor = globalCSS.warnColor;
icon = <InfoCircleOutlined theme="filled" style={{ color: iconColor }} />;
}
const duration = type === 'error' || type === 'warn' ? 0 : 4.5;
notification.open({
message: unescapeHtml(title),
duration,
icon,
description: unescapeHtml(description),
className: styles.notify,
onClose,
top,
style: {
whiteSpace: 'pre-line',
},
});
};
open.propTypes = {
title: PropTypes.string,
type: PropTypes.string,
description: PropTypes.string,
};
const success = (title, description) => {
open({
title,
description,
type: 'success',
});
};
const info = (title, description) => {
open({
title,
description,
type: 'info',
});
};
const error = (title, description) => {
globalRootStore.addNoticeCount();
open({
title,
description,
type: 'error',
onClose: () => {
globalRootStore.removeNoticeCount();
},
});
};
const warn = (title, description) => {
open({
title,
description,
type: 'warn',
});
};
const process = (title, description) => {
open({
title,
description,
type: 'process',
});
};
const errorWithDetail = (err, title) => {
const { status, message } = err || {};
let nTitle = title;
let description;
if (status && parseInt(status, 10) >= 500) {
if (!isEmpty(message) && !statusMap[status]) {
if (isString(message)) {
nTitle += `${t('Message')}${t('.')}`;
} else if (message.reason) {
nTitle += `${t('Message reason')}${t('.')}`;
}
nTitle += `${t('Status Code')}: ${status}`;
} else {
nTitle += statusMap[status];
}
} else {
const decodeErr =
err && isString(err) ? decode(err, { level: 'html5' }) : err;
description = err
? <ModalButton
style={{
float: 'right',
}}
buttonType="link"
buttonText={t('Click to show detail')}
component={
<CodeEditor
className={styles['code-editor']}
value={decodeErr}
mode="json"
options={{
readOnly: true,
}}
/>
}
/>
: ''
}
error(nTitle, description);
};
const Notify = {
open,
success,
error,
warn,
info,
process,
errorWithDetail,
};
export default Notify;