From f49f3c926c8027486636d8652fd7aed8a5813929 Mon Sep 17 00:00:00 2001 From: Sheen Capadngan Date: Mon, 16 Sep 2024 20:00:54 +0800 Subject: [PATCH 1/2] misc: added handling of no project access for redirects --- .../WorkspaceContext/WorkspaceContext.tsx | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/frontend/src/context/WorkspaceContext/WorkspaceContext.tsx b/frontend/src/context/WorkspaceContext/WorkspaceContext.tsx index 4cf8c585f..55040a68e 100644 --- a/frontend/src/context/WorkspaceContext/WorkspaceContext.tsx +++ b/frontend/src/context/WorkspaceContext/WorkspaceContext.tsx @@ -1,6 +1,7 @@ import { createContext, ReactNode, useContext, useMemo } from "react"; import { useRouter } from "next/router"; +import { createNotification } from "@app/components/notifications"; import { useGetUserWorkspaces } from "@app/hooks/api"; import { Workspace } from "@app/hooks/api/workspace/types"; @@ -31,6 +32,29 @@ export const WorkspaceProvider = ({ children }: Props): JSX.Element => { }; }, [ws, workspaceId, isLoading]); + // handle redirects to project-specific routes + if ( + !value.isLoading && + !value.currentWorkspace && + router.pathname.startsWith("/project") && + workspaceId + ) { + createNotification({ + text: "You are not a member of this project.", + type: "info" + }); + + setTimeout(() => { + router.push("/"); + }, 5000); + + return ( +
+ You do not have sufficient access to this project. +
+ ); + } + return {children}; }; From 37a204e49e5d07ae89e45b4e492cf2447688d33f Mon Sep 17 00:00:00 2001 From: Sheen Capadngan Date: Mon, 16 Sep 2024 23:27:10 +0800 Subject: [PATCH 2/2] misc: addressed review comment --- .../WorkspaceContext/WorkspaceContext.tsx | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/frontend/src/context/WorkspaceContext/WorkspaceContext.tsx b/frontend/src/context/WorkspaceContext/WorkspaceContext.tsx index 55040a68e..29ecacaa5 100644 --- a/frontend/src/context/WorkspaceContext/WorkspaceContext.tsx +++ b/frontend/src/context/WorkspaceContext/WorkspaceContext.tsx @@ -1,4 +1,4 @@ -import { createContext, ReactNode, useContext, useMemo } from "react"; +import { createContext, ReactNode, useContext, useEffect, useMemo } from "react"; import { useRouter } from "next/router"; import { createNotification } from "@app/components/notifications"; @@ -32,22 +32,27 @@ export const WorkspaceProvider = ({ children }: Props): JSX.Element => { }; }, [ws, workspaceId, isLoading]); - // handle redirects to project-specific routes - if ( + const shouldTriggerNoProjectAccess = !value.isLoading && !value.currentWorkspace && router.pathname.startsWith("/project") && - workspaceId - ) { - createNotification({ - text: "You are not a member of this project.", - type: "info" - }); + workspaceId; - setTimeout(() => { - router.push("/"); - }, 5000); + // handle redirects for project-specific routes + useEffect(() => { + if (shouldTriggerNoProjectAccess) { + createNotification({ + text: "You are not a member of this project.", + type: "info" + }); + setTimeout(() => { + router.push("/"); + }, 5000); + } + }, [shouldTriggerNoProjectAccess, router]); + + if (shouldTriggerNoProjectAccess) { return (
You do not have sufficient access to this project.