github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/webui/src/lib/hooks/conf.tsx (about)

     1  import React, {ReactNode, createContext, useContext, useMemo} from "react";
     2  
     3  import {useAPI} from "./api";
     4  import {setup} from "../api";
     5  
     6  type LoginConfig = {
     7    RBAC: string|null
     8    FallbackLoginLabel: string|null
     9    FallbackLoginUrl: string|null
    10    LoginCookieNames: string[]
    11    LoginFailedMessage: string|null
    12    LoginUrl: string
    13    LogoutUrl: string
    14  };
    15  
    16  const initValue = {
    17    RBAC: null,
    18    FallbackLoginLabel: null,
    19    FallbackLoginUrl: null,
    20    LoginCookieNames: [],
    21    LoginFailedMessage: null,
    22    LoginUrl: "",
    23    LogoutUrl: "",
    24  };
    25  
    26  export const LoginConfigContext = createContext<LoginConfig>(initValue);
    27  
    28  export const WithLoginConfigContext = ({children}: {children: ReactNode}) => {
    29      const { response, error, loading } = useAPI(() => setup.getState());
    30      // this will be fixed when we have proper types for the API
    31      // eslint-disable-next-line @typescript-eslint/no-explicit-any
    32      const lc = useMemo(() => (error || loading || !("login_config" in (response as any))) ? initValue : (response as any).login_config as LoginConfig || {}, [response]);
    33      return <LoginConfigContext.Provider value={lc}>
    34               {children}
    35             </LoginConfigContext.Provider>;
    36  };
    37  
    38  export const useLoginConfigContext = () => useContext(LoginConfigContext);