import { HttpSetup } from 'opensearch-dashboards/public' ; import React , { createContext , useContext } from 'react' ; import { useRequest } from '../../../public' ; import { Error as CustomError } from './section_error' ; import { Privileges } from '../types' ; interface Authorization { isLoading : boolean ; apiError : CustomError | null ; privileges : Privileges ; } const initialValue = { isLoading : true , apiError : null , privileges : { hasAllPrivileges : true , missingPrivileges : { } , } , } ; export const AuthorizationContext = createContext < Authorization > ( initialValue ) ; export const useAuthorizationContext = ( ) => { const ctx = useContext ( AuthorizationContext ) ; if ( ! ctx ) { throw new Error ( 'AuthorizationContext can only be used inside of AuthorizationProvider!' ) ; } return ctx ; } ; interface Props { privilegesEndpoint : string ; children : React . ReactNode ; httpClient : HttpSetup ; } export const AuthorizationProvider = ( { privilegesEndpoint , httpClient , children } ) => { const { isLoading , error , data : privilegesData } = useRequest < any , CustomError > ( httpClient , { path : privilegesEndpoint , method : 'get' , } ) ; const value = { isLoading , privileges : isLoading ? { hasAllPrivileges : true , missingPrivileges : { } } : privilegesData , apiError : error ? error : null , } as Authorization ; return < AuthorizationContext . Provider value = { value } > { children } < /AuthorizationContext.Provider>; } ;