From 69cba4e6c70025fef53dd2dbb021eb6f7cba9dcc Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Tue, 13 Feb 2024 14:22:18 +0100 Subject: [PATCH] Make alert only visible to admins and add reloading on completion --- .../UpgradeProjectAlert.tsx | 43 +++++++++++++------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/frontend/src/components/v2/UpgradeProjectAlert/UpgradeProjectAlert.tsx b/frontend/src/components/v2/UpgradeProjectAlert/UpgradeProjectAlert.tsx index c0d3b4d59..3aac7852e 100644 --- a/frontend/src/components/v2/UpgradeProjectAlert/UpgradeProjectAlert.tsx +++ b/frontend/src/components/v2/UpgradeProjectAlert/UpgradeProjectAlert.tsx @@ -1,6 +1,8 @@ import { useCallback, useEffect, useState } from "react"; +import { useRouter } from "next/router"; import { useNotificationContext } from "@app/components/context/Notifications/NotificationProvider"; +import { useProjectPermission } from "@app/context"; import { useGetUpgradeProjectStatus, useUpgradeProject } from "@app/hooks/api"; import { Workspace } from "@app/hooks/api/types"; @@ -13,6 +15,8 @@ export type UpgradeProjectAlertProps = { export const UpgradeProjectAlert = ({ project }: UpgradeProjectAlertProps): JSX.Element | null => { const { createNotification } = useNotificationContext(); + const router = useRouter(); + const { membership } = useProjectPermission(); const upgradeProject = useUpgradeProject(); const [currentStatus, setCurrentStatus] = useState(null); const { @@ -41,26 +45,36 @@ export const UpgradeProjectAlert = ({ project }: UpgradeProjectAlertProps): JSX. }, []); useEffect(() => { - if (project.version === "v1") { - getLatestProjectStatus(); - } + let interval: NodeJS.Timeout | null = null; - if (projectStatus && projectStatus?.status !== null) { - if (projectStatus.status === "IN_PROGRESS") { - setCurrentStatus("Your upgrade is being processed."); - } else if (projectStatus.status === "FAILED") { - setCurrentStatus("Upgrade failed, please try again."); - } - } - - const interval = setInterval(() => { + if (membership.role === "admin") { if (project.version === "v1") { getLatestProjectStatus(); } - }, 5_000); + + if (projectStatus && projectStatus?.status !== null) { + if (projectStatus.status === "IN_PROGRESS") { + setCurrentStatus("Your upgrade is being processed."); + } else if (projectStatus.status === "FAILED") { + setCurrentStatus("Upgrade failed, please try again."); + } + } + + if (currentStatus !== null && projectStatus?.status === null) { + router.reload(); + } + + interval = setInterval(() => { + if (project.version === "v1") { + getLatestProjectStatus(); + } + }, 5_000); + } return () => { - clearInterval(interval); + if (interval) { + clearInterval(interval); + } }; }, [projectStatus]); @@ -71,6 +85,7 @@ export const UpgradeProjectAlert = ({ project }: UpgradeProjectAlertProps): JSX. projectStatus?.status !== "FAILED"; if (project.version !== "v1") return null; + if (membership.role !== "admin") return null; return (