1- import React , { useEffect , useState } from 'react' ;
1+ import React , { StrictMode , useEffect , useState } from 'react' ;
22import ReactDOM from 'react-dom/client' ;
33import './index.css' ;
44import App from './App' ;
55import reportWebVitals from './reportWebVitals' ;
66import { FluentProvider , teamsLightTheme , teamsDarkTheme } from "@fluentui/react-components" ;
7-
7+ import { setEnvData , setApiUrl , config as defaultConfig , toBoolean } from './api/config' ;
88const root = ReactDOM . createRoot ( document . getElementById ( "root" ) as HTMLElement ) ;
99
1010const AppWrapper = ( ) => {
1111 // State to store the current theme
12+ const [ isConfigLoaded , setIsConfigLoaded ] = useState ( false ) ;
1213 const [ isDarkMode , setIsDarkMode ] = useState (
1314 window . matchMedia ( "(prefers-color-scheme: dark)" ) . matches
1415 ) ;
16+ type ConfigType = typeof defaultConfig ;
17+ console . log ( "defaultConfig" , defaultConfig ) ;
18+ const [ config , setConfig ] = useState < ConfigType > ( defaultConfig ) ;
19+ useEffect ( ( ) => {
20+ const initConfig = async ( ) => {
21+ window . appConfig = config ;
22+ setEnvData ( config ) ;
23+ setApiUrl ( config . API_URL ) ;
24+
25+ try {
26+ const response = await fetch ( '/config' ) ;
27+ let config = defaultConfig ;
28+ if ( response . ok ) {
29+ config = await response . json ( ) ;
30+ config . ENABLE_AUTH = toBoolean ( config . ENABLE_AUTH ) ;
31+ }
32+
33+ window . appConfig = config ;
34+ setEnvData ( config ) ;
35+ setApiUrl ( config . API_URL ) ;
36+ setConfig ( config ) ;
1537
38+ } catch ( error ) {
39+ console . info ( "Error fetching config:" , error ) ;
40+ } finally {
41+ setIsConfigLoaded ( true ) ;
42+ }
43+ } ;
44+
45+ initConfig ( ) ; // Call the async function inside useEffect
46+ } , [ ] ) ;
1647 // Effect to listen for changes in the user's preferred color scheme
1748 useEffect ( ( ) => {
1849 const mediaQuery = window . matchMedia ( "(prefers-color-scheme: dark)" ) ;
@@ -28,11 +59,13 @@ const AppWrapper = () => {
2859 mediaQuery . addEventListener ( "change" , handleThemeChange ) ;
2960 return ( ) => mediaQuery . removeEventListener ( "change" , handleThemeChange ) ;
3061 } , [ ] ) ;
31-
62+ if ( ! isConfigLoaded ) return < div > Loading... </ div > ;
3263 return (
33- < FluentProvider theme = { isDarkMode ? teamsDarkTheme : teamsLightTheme } style = { { height : "100vh" } } >
34- < App />
35- </ FluentProvider >
64+ < StrictMode >
65+ < FluentProvider theme = { isDarkMode ? teamsDarkTheme : teamsLightTheme } style = { { height : "100vh" } } >
66+ < App />
67+ </ FluentProvider >
68+ </ StrictMode >
3669 ) ;
3770} ;
3871root . render ( < AppWrapper /> ) ;
0 commit comments