memos/web/src/App.tsx

42 lines
1.1 KiB
TypeScript
Raw Normal View History

import { useEffect, useState } from "react";
import useI18n from "./hooks/useI18n";
2021-12-08 18:43:52 +03:00
import { appRouterSwitch } from "./routers";
import { globalService, locationService } from "./services";
2022-05-21 07:21:06 +03:00
import { useAppSelector } from "./store";
import * as storage from "./helpers/storage";
2021-12-08 18:43:52 +03:00
function App() {
const { setLocale } = useI18n();
2022-08-27 03:57:29 +03:00
const user = useAppSelector((state) => state.user.user);
const global = useAppSelector((state) => state.global);
2022-05-21 07:21:06 +03:00
const pathname = useAppSelector((state) => state.location.pathname);
const [isLoading, setLoading] = useState(true);
2021-12-08 18:43:52 +03:00
useEffect(() => {
locationService.updateStateWithLocation();
window.onpopstate = () => {
locationService.updateStateWithLocation();
};
globalService.initialState().then(() => {
setLoading(false);
});
}, []);
2022-08-27 03:57:29 +03:00
useEffect(() => {
if (user?.setting.locale) {
globalService.setLocale(user.setting.locale);
}
}, [user?.setting.locale]);
useEffect(() => {
setLocale(global.locale);
storage.set({
locale: global.locale,
});
2022-08-27 03:57:29 +03:00
}, [global.locale]);
return <>{isLoading ? null : appRouterSwitch(pathname)}</>;
2021-12-08 18:43:52 +03:00
}
export default App;