From 8f7da3a1add086e6b78d05603fb597ac428cb0a7 Mon Sep 17 00:00:00 2001 From: dgtown Date: Thu, 2 May 2024 10:51:40 -0400 Subject: [PATCH] address comments --- dashboard/src/main/Main.tsx | 6 +++--- dashboard/src/shared/auth/AuthnContext.tsx | 13 ++++++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/dashboard/src/main/Main.tsx b/dashboard/src/main/Main.tsx index 45c6381d24..9f32e4fc98 100644 --- a/dashboard/src/main/Main.tsx +++ b/dashboard/src/main/Main.tsx @@ -7,7 +7,7 @@ import api from "shared/api"; import { Context } from "shared/Context"; import { PorterUrls, type PorterUrl } from "shared/routing"; -import { AuthnContext } from "../shared/auth/AuthnContext"; +import { useAuthn } from "../shared/auth/AuthnContext"; import CurrentError from "./CurrentError"; import Home from "./home/Home"; @@ -22,7 +22,7 @@ const Main: React.FC = () => { currentProject, currentCluster, } = useContext(Context); - const { handleLogOut } = useContext(AuthnContext); + const { handleLogOut } = useAuthn(); const [version, setVersion] = useState(""); useEffect(() => { @@ -48,7 +48,7 @@ const Main: React.FC = () => { }, []); const renderMain = (): JSX.Element => { - if (!version || !currentProject || !currentCluster) { + if (!version) { return ; } diff --git a/dashboard/src/shared/auth/AuthnContext.tsx b/dashboard/src/shared/auth/AuthnContext.tsx index af9e42bf89..ebf1161fe2 100644 --- a/dashboard/src/shared/auth/AuthnContext.tsx +++ b/dashboard/src/shared/auth/AuthnContext.tsx @@ -17,12 +17,15 @@ type AuthnState = { handleLogOut: () => void; }; -const NilAuthnState = { - userId: -1, - handleLogOut: () => {}, -}; +export const AuthnContext = React.createContext(null); -export const AuthnContext = React.createContext(NilAuthnState); +export const useAuthn = (): AuthnState => { + const context = useContext(AuthnContext); + if (context == null) { + throw new Error("useAuthn must be used within an AuthnContext"); + } + return context; +}; const AuthnProvider = ({ children,