فهرست منبع

address comments

dgtown 2 سال پیش
والد
کامیت
8f7da3a1ad
2فایلهای تغییر یافته به همراه11 افزوده شده و 8 حذف شده
  1. 3 3
      dashboard/src/main/Main.tsx
  2. 8 5
      dashboard/src/shared/auth/AuthnContext.tsx

+ 3 - 3
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<PropsType> = () => {
     currentProject,
     currentCluster,
   } = useContext(Context);
-  const { handleLogOut } = useContext(AuthnContext);
+  const { handleLogOut } = useAuthn();
   const [version, setVersion] = useState("");
 
   useEffect(() => {
@@ -48,7 +48,7 @@ const Main: React.FC<PropsType> = () => {
   }, []);
 
   const renderMain = (): JSX.Element => {
-    if (!version || !currentProject || !currentCluster) {
+    if (!version) {
       return <Loading />;
     }
 

+ 8 - 5
dashboard/src/shared/auth/AuthnContext.tsx

@@ -17,12 +17,15 @@ type AuthnState = {
   handleLogOut: () => void;
 };
 
-const NilAuthnState = {
-    userId: -1,
-    handleLogOut: () => {},
-};
+export const AuthnContext = React.createContext<AuthnState | null>(null);
 
-export const AuthnContext = React.createContext<AuthnState>(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,