github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/webui/src/pages/setup/index.jsx (about) 1 import React, {useCallback, useEffect} from "react"; 2 import {useState} from "react"; 3 import {API_ENDPOINT, setup, SETUP_STATE_INITIALIZED} from "../../lib/api"; 4 import {useRouter} from "../../lib/hooks/router"; 5 import {useAPI} from "../../lib/hooks/api"; 6 import {SetupComplete} from "./setupComplete"; 7 import {UserConfiguration} from "./userConfiguration"; 8 9 10 const SetupContents = () => { 11 const [setupError, setSetupError] = useState(null); 12 const [setupData, setSetupData] = useState(null); 13 const [disabled, setDisabled] = useState(false); 14 const [currentStep, setCurrentStep] = useState(null); 15 const [commPrefsMissing, setCommPrefsMissing] = useState(false); 16 const router = useRouter(); 17 const { response, error, loading } = useAPI(() => { 18 return setup.getState() 19 }); 20 21 useEffect(() => { 22 // Set initial state 23 if (!error && response) { 24 setCurrentStep(response.state); 25 setCommPrefsMissing(response.comm_prefs_missing === true); 26 } 27 }, [error, response]); 28 29 const onSubmitUserConfiguration = useCallback(async (adminUser, userEmail, checked) => { 30 if (!adminUser) { 31 setSetupError("Please enter your admin username."); 32 return; 33 } 34 if (commPrefsMissing && !userEmail) { 35 setSetupError("Please enter your email address."); 36 return; 37 } 38 39 setDisabled(true); 40 try { 41 if (currentStep !== SETUP_STATE_INITIALIZED) { 42 const response = await setup.lakeFS(adminUser); 43 setSetupData(response); 44 } 45 if (commPrefsMissing) { 46 await setup.commPrefs(userEmail, checked, checked); 47 setCommPrefsMissing(false); 48 } 49 setSetupError(null); 50 } catch (error) { 51 setSetupError(error); 52 } finally { 53 setDisabled(false); 54 } 55 }, [setDisabled, setSetupError, setup, currentStep, commPrefsMissing]); 56 57 if (error || loading) { 58 return null; 59 } 60 61 if (setupData && setupData.access_key_id) { 62 return ( 63 <SetupComplete 64 accessKeyId={setupData.access_key_id} 65 secretAccessKey={setupData.secret_access_key} 66 apiEndpoint={API_ENDPOINT} 67 /> 68 ); 69 } 70 71 const notInitialized = currentStep !== SETUP_STATE_INITIALIZED; 72 if (notInitialized || commPrefsMissing) { 73 return ( 74 <UserConfiguration 75 onSubmit={onSubmitUserConfiguration} 76 setupError={setupError} 77 disabled={disabled} 78 requireAdmin={notInitialized} 79 requireCommPrefs={commPrefsMissing} 80 /> 81 ); 82 } 83 84 return router.push({pathname: '/', query: router.query}); 85 }; 86 87 88 const SetupPage = () => <SetupContents/>; 89 90 export default SetupPage;