refactor: Improve Timeout Handling, Simplify Link Logic, and Enhance Cleanup (#4744)

* Refactor NoticeAlert: Improve Timeout Handling, Simplify Link Logic, and Enhance Cleanup

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
yabi90 2024-12-03 08:32:36 -06:00 • committed by GitHub
commit 4b08696947
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 23 additions and 17 deletions

View file

@ -6,21 +6,32 @@ import { NoticeAlertType } from "../../types/alerts";
export default function NoticeAlert({ export default function NoticeAlert({
title, title,
link = "", link,
id, id,
removeAlert, removeAlert,
}: NoticeAlertType): JSX.Element { }: NoticeAlertType): JSX.Element {
const [show, setShow] = useState(true); const [show, setShow] = useState(true);
useEffect(() => { useEffect(() => {
if (show) { const timeoutId = setTimeout(() => {
setShow(false);
// Wait for the leave transition before calling removeAlert
setTimeout(() => { setTimeout(() => {
setShow(false); removeAlert(id);
setTimeout(() => { }, 500); // match the duration of the leave transition
removeAlert(id); }, 5000); // auto-dismiss alert after 5 seconds
}, 500);
}, 5000); return () => clearTimeout(timeoutId); // Cleanup timeout on component unmount or re-render
} }, [id, removeAlert]);
}, [id, removeAlert, show]);
const handleClick = () => {
setShow(false);
// Wait for the leave transition before calling removeAlert
setTimeout(() => {
removeAlert(id);
}, 500); // Ensure the alert is removed after the animation
};
return ( return (
<Transition <Transition
show={show} show={show}
@ -32,10 +43,7 @@ export default function NoticeAlert({
leaveTo={"transform translate-x-[-100%]"} leaveTo={"transform translate-x-[-100%]"}
> >
<div <div
onClick={() => { onClick={handleClick}
setShow(false);
removeAlert(id);
}}
className="noflow nowheel nopan nodelete nodrag mt-6 w-96 rounded-md bg-info-background p-4 shadow-xl" className="noflow nowheel nopan nodelete nodrag mt-6 w-96 rounded-md bg-info-background p-4 shadow-xl"
> >
<div className="flex"> <div className="flex">
@ -51,15 +59,13 @@ export default function NoticeAlert({
{title} {title}
</p> </p>
<p className="mt-3 text-sm md:ml-6 md:mt-0"> <p className="mt-3 text-sm md:ml-6 md:mt-0">
{link !== "" ? ( {link && (
<CustomLink <CustomLink
to={link} to={link}
className="whitespace-nowrap font-medium text-info-foreground hover:text-accent-foreground" className="whitespace-nowrap font-medium text-info-foreground hover:text-accent-foreground"
> >
Details Details
</CustomLink> </CustomLink>
) : (
<></>
)} )}
</p> </p>
</div> </div>

View file

@ -6,7 +6,7 @@ export type ErrorAlertType = {
}; };
export type NoticeAlertType = { export type NoticeAlertType = {
title: string; title: string;
link: string | undefined; link?: string;
id: string; id: string;
removeAlert: (id: string) => void; removeAlert: (id: string) => void;
}; };