2021-08-27 00:07:16 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
2021-04-12 13:32:25 +00:00
|
|
|
import classnames from "classnames";
|
|
|
|
|
|
2022-04-01 06:42:26 +00:00
|
|
|
import { INotification } from "interfaces/notification";
|
2021-08-30 19:52:13 +00:00
|
|
|
// @ts-ignore
|
2023-06-02 13:48:17 +00:00
|
|
|
import Icon from "components/Icon/Icon";
|
2021-04-12 13:32:25 +00:00
|
|
|
|
|
|
|
|
const baseClass = "flash-message";
|
|
|
|
|
|
2021-11-07 06:41:09 +00:00
|
|
|
export interface IFlashMessage {
|
2021-08-30 19:52:13 +00:00
|
|
|
fullWidth: boolean;
|
2022-04-01 06:42:26 +00:00
|
|
|
notification: INotification | null;
|
2021-11-07 06:41:09 +00:00
|
|
|
isPersistent?: boolean;
|
2022-08-31 16:17:27 +00:00
|
|
|
className?: string;
|
2021-08-30 19:52:13 +00:00
|
|
|
onRemoveFlash: () => void;
|
2024-10-22 17:52:20 +00:00
|
|
|
pathname?: string;
|
2021-08-30 19:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
const FlashMessage = ({
|
|
|
|
|
fullWidth,
|
|
|
|
|
notification,
|
2021-11-07 06:41:09 +00:00
|
|
|
isPersistent,
|
2022-08-31 16:17:27 +00:00
|
|
|
className,
|
2021-04-12 13:32:25 +00:00
|
|
|
onRemoveFlash,
|
2024-10-22 17:52:20 +00:00
|
|
|
pathname,
|
2022-01-17 02:46:20 +00:00
|
|
|
}: IFlashMessage): JSX.Element | null => {
|
2024-10-22 17:52:20 +00:00
|
|
|
const { alertType, isVisible, message, persistOnPageChange } =
|
|
|
|
|
notification || {};
|
2022-08-31 16:17:27 +00:00
|
|
|
const baseClasses = classnames(
|
|
|
|
|
baseClass,
|
|
|
|
|
className,
|
|
|
|
|
`${baseClass}--${alertType}`,
|
|
|
|
|
{
|
|
|
|
|
[`${baseClass}--full-width`]: fullWidth,
|
|
|
|
|
}
|
|
|
|
|
);
|
2016-10-03 17:54:22 +00:00
|
|
|
|
2021-08-27 00:07:16 +00:00
|
|
|
const [hide, setHide] = useState(false);
|
|
|
|
|
|
|
|
|
|
// This useEffect handles hiding successful flash messages after a 4s timeout. By putting the
|
|
|
|
|
// notification in the dependency array, we can properly reset whenever a new flash message comes through.
|
2021-07-27 20:19:52 +00:00
|
|
|
useEffect(() => {
|
2021-08-27 00:07:16 +00:00
|
|
|
// Any time this hook runs, we reset the hide to false (so that subsequent messages that will be
|
|
|
|
|
// using this same component instance will be visible).
|
|
|
|
|
setHide(false);
|
|
|
|
|
|
2021-11-07 06:41:09 +00:00
|
|
|
if (!isPersistent && alertType === "success" && isVisible) {
|
2021-08-27 00:07:16 +00:00
|
|
|
// After 4 seconds, set hide to true.
|
2021-10-22 15:19:34 +00:00
|
|
|
const timer = setTimeout(() => {
|
|
|
|
|
setHide(true);
|
|
|
|
|
onRemoveFlash(); // This function resets notifications which allows CoreLayout reset of selected rows
|
|
|
|
|
}, 4000);
|
2021-08-27 00:07:16 +00:00
|
|
|
// Return a cleanup function that will clear this reset, in case another render happens
|
|
|
|
|
// after this. We want that render to set a new timeout (if needed).
|
|
|
|
|
return () => clearTimeout(timer);
|
2021-07-27 20:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
2021-08-27 00:07:16 +00:00
|
|
|
return undefined; // No cleanup when we don't set a timeout.
|
2024-04-03 16:25:16 +00:00
|
|
|
}, [notification, alertType, isVisible, setHide]);
|
2021-08-27 00:07:16 +00:00
|
|
|
|
2024-10-22 17:52:20 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!persistOnPageChange) {
|
|
|
|
|
setHide(true);
|
|
|
|
|
}
|
|
|
|
|
// intentionally omit persistOnPageChange from dependencies to prevent hiding during initial
|
|
|
|
|
// update of the notification prop from its default empty value
|
|
|
|
|
}, [pathname]);
|
|
|
|
|
|
2021-08-27 00:07:16 +00:00
|
|
|
if (hide || !isVisible) {
|
2022-04-01 06:42:26 +00:00
|
|
|
return null;
|
2016-10-19 20:22:18 +00:00
|
|
|
}
|
2016-10-03 17:54:22 +00:00
|
|
|
|
|
|
|
|
return (
|
2024-08-26 18:17:51 +00:00
|
|
|
<div className={"flash-message-container"}>
|
|
|
|
|
<div className={baseClasses} id={baseClasses}>
|
|
|
|
|
<div className={`${baseClass}__content`}>
|
|
|
|
|
<Icon
|
|
|
|
|
name={alertType === "success" ? "success" : "error"}
|
|
|
|
|
color="core-fleet-white"
|
|
|
|
|
/>
|
|
|
|
|
<span>{message}</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div className={`${baseClass}__action`}>
|
|
|
|
|
<div className={`${baseClass}__ex`}>
|
|
|
|
|
<button
|
|
|
|
|
className={`${baseClass}__remove ${baseClass}__remove--${alertType} button--unstyled`}
|
|
|
|
|
onClick={onRemoveFlash}
|
|
|
|
|
>
|
|
|
|
|
<Icon
|
|
|
|
|
name="close"
|
|
|
|
|
color={
|
|
|
|
|
alertType === "warning-filled"
|
|
|
|
|
? "core-fleet-black"
|
|
|
|
|
: "core-fleet-white"
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
2021-06-07 19:48:52 +00:00
|
|
|
</div>
|
2016-10-03 17:54:22 +00:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-03 19:40:54 +00:00
|
|
|
export default FlashMessage;
|