2017-10-21 03:09:01 +08:00
|
|
|
/* eslint react/jsx-filename-extension: 0 */
|
2017-10-13 02:53:33 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { toast } from 'react-toastify';
|
2023-02-23 21:27:16 +08:00
|
|
|
import { isEqual } from 'radash';
|
2024-09-09 19:51:34 +08:00
|
|
|
import Styled from './styles';
|
2022-02-16 01:57:50 +08:00
|
|
|
import Toast from '/imports/ui/components/common/toast/component';
|
2017-10-13 02:53:33 +08:00
|
|
|
|
2017-10-19 18:45:07 +08:00
|
|
|
let lastToast = {
|
|
|
|
id: null,
|
|
|
|
message: null,
|
|
|
|
type: null,
|
|
|
|
icon: null,
|
|
|
|
};
|
|
|
|
|
2018-06-13 01:21:31 +08:00
|
|
|
export function notify(message, type = 'default', icon, options, content, small) {
|
2017-10-13 02:53:33 +08:00
|
|
|
const settings = {
|
|
|
|
type,
|
|
|
|
...options,
|
|
|
|
};
|
|
|
|
|
2017-10-19 18:45:07 +08:00
|
|
|
const { id: lastToastId, ...lastToastProps } = lastToast;
|
2018-06-13 01:21:31 +08:00
|
|
|
const toastProps = {
|
|
|
|
message,
|
|
|
|
type,
|
|
|
|
icon,
|
|
|
|
content,
|
|
|
|
small,
|
|
|
|
};
|
2017-10-19 18:45:07 +08:00
|
|
|
|
2023-02-23 21:27:16 +08:00
|
|
|
if (!toast.isActive(lastToast.id) || !isEqual(lastToastProps, toastProps)) {
|
2024-09-09 19:51:34 +08:00
|
|
|
if (options?.helpLink != null && options?.helpLabel != null) {
|
|
|
|
const id = toast(
|
|
|
|
<div role="alert">
|
|
|
|
<Toast {...toastProps} />
|
|
|
|
<Styled.HelpLinkButton
|
|
|
|
label={options.helpLabel}
|
|
|
|
color="default"
|
|
|
|
size="sm"
|
|
|
|
onClick={() => { window.open(options.helpLink); }}
|
|
|
|
data-test="helpLinkToastButton"
|
|
|
|
/>
|
|
|
|
</div>, settings,
|
|
|
|
);
|
|
|
|
|
|
|
|
lastToast = { id, ...toastProps };
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
2021-05-28 02:12:49 +08:00
|
|
|
if (toast.isActive(lastToast.id)
|
2023-02-23 21:27:16 +08:00
|
|
|
&& isEqual(lastToastProps.key, toastProps.key) && options?.autoClose > 0) {
|
2021-05-28 02:12:49 +08:00
|
|
|
toast.update(
|
|
|
|
lastToast.id,
|
|
|
|
{
|
2021-08-30 07:43:20 +08:00
|
|
|
render: <div role="alert"><Toast {...toastProps} /></div>,
|
2021-05-28 02:12:49 +08:00
|
|
|
autoClose: options.autoClose,
|
|
|
|
...toastProps,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
2021-08-30 07:43:20 +08:00
|
|
|
const id = toast(<div role="alert"><Toast {...toastProps} /></div>, settings);
|
2017-10-19 18:45:07 +08:00
|
|
|
|
2021-05-28 01:46:27 +08:00
|
|
|
lastToast = { id, ...toastProps };
|
2019-06-13 02:03:23 +08:00
|
|
|
|
2021-05-28 01:46:27 +08:00
|
|
|
return id;
|
|
|
|
}
|
2017-10-13 02:53:33 +08:00
|
|
|
}
|
2019-06-13 02:03:23 +08:00
|
|
|
return null;
|
2017-10-21 03:27:00 +08:00
|
|
|
}
|
2017-10-13 02:53:33 +08:00
|
|
|
|
2017-10-21 03:27:00 +08:00
|
|
|
export default { notify };
|