-
Notifications
You must be signed in to change notification settings - Fork 834
Expand file tree
/
Copy pathindex.tsx
More file actions
80 lines (74 loc) · 3.1 KB
/
Copy pathindex.tsx
File metadata and controls
80 lines (74 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import React, { StrictMode, useEffect, useState } from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { FluentProvider, teamsLightTheme, teamsDarkTheme } from "@fluentui/react-components";
import { setEnvData, setApiUrl, config as defaultConfig, toBoolean, getUserInfo, setUserInfoGlobal } from './api/config';
import { UserInfo } from './models';
import { apiService } from './api';
const root = ReactDOM.createRoot(document.getElementById("root") as HTMLElement);
const AppWrapper = () => {
// State to store the current theme
const [isConfigLoaded, setIsConfigLoaded] = useState(false);
const [isUserInfoLoaded, setIsUserInfoLoaded] = useState(false);
const [isDarkMode, setIsDarkMode] = useState(
window.matchMedia("(prefers-color-scheme: dark)").matches
);
type ConfigType = typeof defaultConfig;
const [config, setConfig] = useState<ConfigType>(defaultConfig);
useEffect(() => {
const initConfig = async () => {
window.appConfig = config;
setEnvData(config);
setApiUrl(config.API_URL);
try {
const response = await fetch('/config');
let config = defaultConfig;
if (response.ok) {
config = await response.json();
config.ENABLE_AUTH = toBoolean(config.ENABLE_AUTH);
}
window.appConfig = config;
setEnvData(config);
setApiUrl(config.API_URL);
setConfig(config);
let defaultUserInfo = config.ENABLE_AUTH ? await getUserInfo() : ({} as UserInfo);
window.userInfo = defaultUserInfo;
setUserInfoGlobal(defaultUserInfo);
const browserLanguage = await apiService.sendUserBrowserLanguage();
} catch (error) {
console.info("frontend config did not load from python", error);
} finally {
setIsConfigLoaded(true);
setIsUserInfoLoaded(true);
}
};
initConfig(); // Call the async function inside useEffect
}, []);
// Effect to listen for changes in the user's preferred color scheme
useEffect(() => {
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
const handleThemeChange = (event: MediaQueryListEvent) => {
setIsDarkMode(event.matches);
document.body.classList.toggle("dark-mode", event.matches); // ✅ Add this
};
// Apply dark-mode class initially
document.body.classList.toggle("dark-mode", isDarkMode);
mediaQuery.addEventListener("change", handleThemeChange);
return () => mediaQuery.removeEventListener("change", handleThemeChange);
}, []);
if (!isConfigLoaded || !isUserInfoLoaded) return <div>Loading...</div>;
return (
<StrictMode>
<FluentProvider theme={isDarkMode ? teamsDarkTheme : teamsLightTheme} style={{ height: "100vh" }}>
<App />
</FluentProvider>
</StrictMode>
);
};
root.render(<AppWrapper />);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();