From 3b5b36600956cfcf48313cd7b5a7bada4e41ca55 Mon Sep 17 00:00:00 2001 From: Bilal ABBAD Date: Tue, 29 Oct 2024 23:56:41 +0100 Subject: [PATCH] Reload menu when switching branch (#4776) --- frontend/app/src/components/account-menu.tsx | 4 ++-- frontend/app/src/hooks/useAuth.tsx | 14 +++++++++++++- .../layout/menu-navigation/menu-navigation.tsx | 7 +++---- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/frontend/app/src/components/account-menu.tsx b/frontend/app/src/components/account-menu.tsx index c2ff0053fe..acc362f66d 100644 --- a/frontend/app/src/components/account-menu.tsx +++ b/frontend/app/src/components/account-menu.tsx @@ -91,7 +91,7 @@ const UnauthenticatedAccountMenu = () => { return ( @@ -206,7 +206,7 @@ const AuthenticatedAccountMenu = ({ const AccountMenuSkeleton = () => { return ( -
+
diff --git a/frontend/app/src/hooks/useAuth.tsx b/frontend/app/src/hooks/useAuth.tsx index f045b732c2..fa02a58d41 100644 --- a/frontend/app/src/hooks/useAuth.tsx +++ b/frontend/app/src/hooks/useAuth.tsx @@ -7,6 +7,7 @@ import { components } from "@/infraops"; import { configState } from "@/state/atoms/config.atom"; import { parseJwt } from "@/utils/common"; import { fetchUrl } from "@/utils/fetch"; +import { ObservableQuery } from "@apollo/client"; import { useAtom } from "jotai/index"; import { ReactElement, ReactNode, createContext, useContext, useState } from "react"; import { Navigate, useLocation } from "react-router-dom"; @@ -47,6 +48,12 @@ export const removeTokensInLocalStorage = () => { localStorage.removeItem(REFRESH_TOKEN_KEY); }; +const QUERY_TO_IGNORE = ["GET_PROFILE_DETAILS"]; + +const shouldIgnoreQuery = (observableQuery: ObservableQuery) => { + return !!observableQuery.queryName && QUERY_TO_IGNORE.includes(observableQuery.queryName); +}; + export const getNewToken = async () => { const refreshToken = localStorage.getItem(REFRESH_TOKEN_KEY); @@ -126,7 +133,12 @@ export function AuthProvider({ children }: { children: ReactNode }) { const signOut = () => { removeTokensInLocalStorage(); setAccessToken(null); - graphqlClient.refetchQueries({ include: "active" }); + graphqlClient.refetchQueries({ + include: "active", + onQueryUpdated(observableQuery) { + return !shouldIgnoreQuery(observableQuery); + }, + }); }; const data = parseJwt(accessToken); diff --git a/frontend/app/src/screens/layout/menu-navigation/menu-navigation.tsx b/frontend/app/src/screens/layout/menu-navigation/menu-navigation.tsx index dd359e8dc7..e37a4530a5 100644 --- a/frontend/app/src/screens/layout/menu-navigation/menu-navigation.tsx +++ b/frontend/app/src/screens/layout/menu-navigation/menu-navigation.tsx @@ -6,7 +6,7 @@ import { useAuth } from "@/hooks/useAuth"; import { MenuSectionInternal } from "@/screens/layout/menu-navigation/components/menu-section-internal"; import { MenuSectionObject } from "@/screens/layout/menu-navigation/components/menu-section-object"; import { currentBranchAtom } from "@/state/atoms/branches.atom"; -import { currentSchemaHashAtom, menuAtom } from "@/state/atoms/schema.atom"; +import { menuAtom } from "@/state/atoms/schema.atom"; import { fetchUrl } from "@/utils/fetch"; import { useAtom, useAtomValue } from "jotai"; import { useEffect, useState } from "react"; @@ -19,12 +19,11 @@ export interface MenuNavigationProps { export default function MenuNavigation({ isCollapsed }: MenuNavigationProps) { const { accessToken } = useAuth(); const currentBranch = useAtomValue(currentBranchAtom); - const currentSchemaHash = useAtomValue(currentSchemaHashAtom); const [menu, setMenu] = useAtom(menuAtom); const [isLoading, setIsLoading] = useState(false); useEffect(() => { - if (!currentSchemaHash) return; + if (!currentBranch) return; const headers = accessToken && { authorization: `Bearer ${accessToken}`, @@ -39,7 +38,7 @@ export default function MenuNavigation({ isCollapsed }: MenuNavigationProps) { } finally { setIsLoading(false); } - }, [currentSchemaHash, accessToken]); + }, [currentBranch, accessToken]); if (isLoading) return
Loading...
; if (!menu?.sections) return
;