Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions src/frontend/src/alerts/notice/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,32 @@ import { NoticeAlertType } from "../../types/alerts";

export default function NoticeAlert({
title,
link = "",
link,
id,
removeAlert,
}: NoticeAlertType): JSX.Element {
const [show, setShow] = useState(true);

useEffect(() => {
if (show) {
const timeoutId = setTimeout(() => {
setShow(false);
// Wait for the leave transition before calling removeAlert
setTimeout(() => {
setShow(false);
setTimeout(() => {
removeAlert(id);
}, 500);
}, 5000);
}
}, [id, removeAlert, show]);
removeAlert(id);
}, 500); // match the duration of the leave transition
}, 5000); // auto-dismiss alert after 5 seconds

return () => clearTimeout(timeoutId); // Cleanup timeout on component unmount or re-render
}, [id, removeAlert]);

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 (
<Transition
show={show}
Expand All @@ -32,10 +43,7 @@ export default function NoticeAlert({
leaveTo={"transform translate-x-[-100%]"}
>
<div
onClick={() => {
setShow(false);
removeAlert(id);
}}
onClick={handleClick}
className="noflow nowheel nopan nodelete nodrag mt-6 w-96 rounded-md bg-info-background p-4 shadow-xl"
>
<div className="flex">
Expand All @@ -51,15 +59,13 @@ export default function NoticeAlert({
{title}
</p>
<p className="mt-3 text-sm md:ml-6 md:mt-0">
{link !== "" ? (
{link && (
<CustomLink
to={link}
className="whitespace-nowrap font-medium text-info-foreground hover:text-accent-foreground"
>
Details
</CustomLink>
) : (
<></>
)}
</p>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/types/alerts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type ErrorAlertType = {
};
export type NoticeAlertType = {
title: string;
link: string | undefined;
link?: string;
id: string;
removeAlert: (id: string) => void;
};
Expand Down
Loading