github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/webui/src/lib/hooks/storageConfig.tsx (about) 1 import React, { 2 FC, 3 useContext, 4 useState, 5 createContext, 6 useEffect, 7 } from "react"; 8 9 import { config } from "../api"; 10 import useUser from "./user"; 11 12 type StorageConfigContextType = { 13 error: Error | null; 14 loading: boolean; 15 blockstore_namespace_ValidityRegex: string | null; 16 blockstore_namespace_example: string | null; 17 blockstore_type: string | null; 18 default_namespace_prefix: string | null; 19 import_support: boolean; 20 import_validity_regex: string | null; 21 pre_sign_support: boolean; 22 pre_sign_support_ui: boolean; 23 }; 24 25 const storageConfigInitialState: StorageConfigContextType = { 26 error: null, 27 loading: true, 28 blockstore_namespace_ValidityRegex: null, 29 blockstore_namespace_example: null, 30 blockstore_type: null, 31 default_namespace_prefix: null, 32 import_support: false, 33 import_validity_regex: null, 34 pre_sign_support: false, 35 pre_sign_support_ui: false, 36 }; 37 38 export const fetchStorageConfig = async () => { 39 // we assume that we're actually getting a StorageConfigContextType 40 // but there's no real guarantee 41 // we'll need to valudate and fix this when the API client is typed 42 const storageConfig = await config.getStorageConfig(); 43 return storageConfig; 44 }; 45 46 const StorageConfigContext = createContext<StorageConfigContextType>( 47 storageConfigInitialState 48 ); 49 50 export const useStorageConfig = () => { 51 const config = useContext(StorageConfigContext); 52 return config; 53 }; 54 55 export const StorageConfigProvider: FC<{ children: React.ReactNode }> = ({ 56 children, 57 }) => { 58 const { user } = useUser(); 59 const [storageConfig, setStorageConfig] = useState<StorageConfigContextType>( 60 storageConfigInitialState 61 ); 62 63 useEffect(() => { 64 const fetchStorageConfigAndSetState = async () => { 65 const storageConfig = await fetchStorageConfig(); 66 setStorageConfig({ 67 ...storageConfig, 68 loading: false, 69 }); 70 }; 71 fetchStorageConfigAndSetState(); 72 }, [user]); 73 74 return ( 75 <StorageConfigContext.Provider value={storageConfig}> 76 {children} 77 </StorageConfigContext.Provider> 78 ); 79 };