2022-12-04 07:23:29 +03:00
|
|
|
import { useColorScheme } from "@mui/joy";
|
2022-11-30 01:13:22 +03:00
|
|
|
import { useEffect, Suspense } from "react";
|
2022-09-19 17:27:50 +03:00
|
|
|
import { useTranslation } from "react-i18next";
|
2022-09-19 16:53:27 +03:00
|
|
|
import { RouterProvider } from "react-router-dom";
|
|
|
|
import router from "./router";
|
2022-12-18 10:25:18 +03:00
|
|
|
import { useLocationStore, useGlobalStore } from "./store/module";
|
2022-08-13 09:35:33 +03:00
|
|
|
import * as storage from "./helpers/storage";
|
2022-12-04 07:23:29 +03:00
|
|
|
import { getSystemColorScheme } from "./helpers/utils";
|
2022-12-04 10:34:03 +03:00
|
|
|
import Loading from "./pages/Loading";
|
2021-12-08 18:43:52 +03:00
|
|
|
|
2022-12-18 10:25:18 +03:00
|
|
|
const App = () => {
|
2022-09-19 17:27:50 +03:00
|
|
|
const { i18n } = useTranslation();
|
2022-12-18 10:25:18 +03:00
|
|
|
const globalStore = useGlobalStore();
|
|
|
|
const locationStore = useLocationStore();
|
2022-12-04 07:23:29 +03:00
|
|
|
const { mode, setMode } = useColorScheme();
|
2022-12-18 10:25:18 +03:00
|
|
|
const { appearance, locale, systemStatus } = globalStore.state;
|
2021-12-08 18:43:52 +03:00
|
|
|
|
2022-07-25 16:50:25 +03:00
|
|
|
useEffect(() => {
|
2022-12-18 10:25:18 +03:00
|
|
|
locationStore.updateStateWithLocation();
|
2022-07-25 16:50:25 +03:00
|
|
|
window.onpopstate = () => {
|
2022-12-18 10:25:18 +03:00
|
|
|
locationStore.updateStateWithLocation();
|
2022-07-25 16:50:25 +03:00
|
|
|
};
|
2022-09-19 16:53:27 +03:00
|
|
|
}, []);
|
2022-08-27 03:57:29 +03:00
|
|
|
|
2022-12-04 07:23:29 +03:00
|
|
|
useEffect(() => {
|
2022-12-11 18:18:25 +03:00
|
|
|
const darkMediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
|
|
|
|
const handleColorSchemeChange = (e: MediaQueryListEvent) => {
|
2022-12-18 10:25:18 +03:00
|
|
|
if (globalStore.getState().appearance === "system") {
|
2022-12-04 07:23:29 +03:00
|
|
|
const mode = e.matches ? "dark" : "light";
|
|
|
|
setMode(mode);
|
|
|
|
}
|
2022-12-11 18:18:25 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (darkMediaQuery.addEventListener) {
|
|
|
|
darkMediaQuery.addEventListener("change", handleColorSchemeChange);
|
|
|
|
} else {
|
|
|
|
darkMediaQuery.addListener(handleColorSchemeChange);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error("failed to initial color scheme listener", error);
|
|
|
|
}
|
2022-12-04 07:23:29 +03:00
|
|
|
}, []);
|
|
|
|
|
2022-11-18 16:17:52 +03:00
|
|
|
// Inject additional style and script codes.
|
2022-11-11 18:42:44 +03:00
|
|
|
useEffect(() => {
|
2022-11-22 18:45:11 +03:00
|
|
|
if (systemStatus.additionalStyle) {
|
|
|
|
const styleEl = document.createElement("style");
|
|
|
|
styleEl.innerHTML = systemStatus.additionalStyle;
|
|
|
|
styleEl.setAttribute("type", "text/css");
|
2022-12-24 04:35:30 +03:00
|
|
|
document.body.insertAdjacentElement("beforeend", styleEl);
|
2022-11-22 18:45:11 +03:00
|
|
|
}
|
|
|
|
if (systemStatus.additionalScript) {
|
|
|
|
const scriptEl = document.createElement("script");
|
|
|
|
scriptEl.innerHTML = systemStatus.additionalScript;
|
|
|
|
document.head.appendChild(scriptEl);
|
|
|
|
}
|
2022-12-18 16:18:30 +03:00
|
|
|
|
|
|
|
// dynamic update metadata with customized profile.
|
|
|
|
document.title = systemStatus.customizedProfile.name;
|
|
|
|
const link = document.querySelector("link[rel~='icon']") as HTMLLinkElement;
|
2022-12-22 19:21:53 +03:00
|
|
|
link.href = systemStatus.customizedProfile.logoUrl || "/logo.png";
|
2022-11-22 18:45:11 +03:00
|
|
|
}, [systemStatus]);
|
2022-11-11 18:42:44 +03:00
|
|
|
|
2022-08-13 09:35:33 +03:00
|
|
|
useEffect(() => {
|
2022-12-10 05:42:10 +03:00
|
|
|
document.documentElement.setAttribute("lang", locale);
|
2022-11-22 18:45:11 +03:00
|
|
|
i18n.changeLanguage(locale);
|
2022-08-13 09:35:33 +03:00
|
|
|
storage.set({
|
2022-11-22 18:45:11 +03:00
|
|
|
locale: locale,
|
2022-08-13 09:35:33 +03:00
|
|
|
});
|
2022-11-22 18:45:11 +03:00
|
|
|
}, [locale]);
|
2022-08-13 09:35:33 +03:00
|
|
|
|
2022-12-02 15:00:34 +03:00
|
|
|
useEffect(() => {
|
|
|
|
storage.set({
|
|
|
|
appearance: appearance,
|
|
|
|
});
|
2022-12-04 07:23:29 +03:00
|
|
|
|
|
|
|
let currentAppearance = appearance;
|
|
|
|
if (appearance === "system") {
|
|
|
|
currentAppearance = getSystemColorScheme();
|
|
|
|
}
|
|
|
|
|
|
|
|
setMode(currentAppearance);
|
2022-12-02 15:00:34 +03:00
|
|
|
}, [appearance]);
|
|
|
|
|
2022-12-04 07:23:29 +03:00
|
|
|
useEffect(() => {
|
|
|
|
const root = document.documentElement;
|
|
|
|
if (mode === "light") {
|
|
|
|
root.classList.remove("dark");
|
2022-12-04 10:34:03 +03:00
|
|
|
} else if (mode === "dark") {
|
2022-12-04 07:23:29 +03:00
|
|
|
root.classList.add("dark");
|
|
|
|
}
|
|
|
|
}, [mode]);
|
|
|
|
|
2022-11-03 16:47:36 +03:00
|
|
|
return (
|
2022-11-30 15:34:16 +03:00
|
|
|
<Suspense fallback={<Loading />}>
|
|
|
|
<RouterProvider router={router} />
|
|
|
|
</Suspense>
|
2022-11-03 16:47:36 +03:00
|
|
|
);
|
2022-12-18 10:25:18 +03:00
|
|
|
};
|
2021-12-08 18:43:52 +03:00
|
|
|
|
|
|
|
export default App;
|