memos/web/src/App.tsx

103 lines
3.0 KiB
TypeScript
Raw Normal View History

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