1
0
mirror of https://github.com/stonith404/pingvin-share.git synced 2024-10-01 09:00:12 +02:00
pingvin-share/frontend/src/pages/_app.tsx

93 lines
3.0 KiB
TypeScript
Raw Normal View History

2022-04-25 15:15:17 +02:00
import {
ColorScheme,
Container,
LoadingOverlay,
MantineProvider,
} from "@mantine/core";
2022-10-10 22:30:04 +02:00
import { useColorScheme } from "@mantine/hooks";
2022-04-25 15:15:17 +02:00
import { ModalsProvider } from "@mantine/modals";
import { NotificationsProvider } from "@mantine/notifications";
import type { AppProps } from "next/app";
2022-12-01 23:07:49 +01:00
import { useRouter } from "next/router";
2022-04-25 15:15:17 +02:00
import { useEffect, useState } from "react";
import Header from "../components/navBar/NavBar";
2022-12-01 23:07:49 +01:00
import useConfig, { ConfigContext } from "../hooks/config.hook";
import { UserContext } from "../hooks/user.hook";
import authService from "../services/auth.service";
import configService from "../services/config.service";
import userService from "../services/user.service";
2022-05-16 13:11:51 +02:00
import GlobalStyle from "../styles/global.style";
import globalStyle from "../styles/mantine.style";
import Config from "../types/config.type";
import { CurrentUser } from "../types/user.type";
2022-04-25 15:15:17 +02:00
import { GlobalLoadingContext } from "../utils/loading.util";
function App({ Component, pageProps }: AppProps) {
2022-10-10 22:30:04 +02:00
const systemTheme = useColorScheme();
2022-12-01 23:07:49 +01:00
const router = useRouter();
const config = useConfig();
2022-10-10 22:30:04 +02:00
const [colorScheme, setColorScheme] = useState<ColorScheme>();
2022-04-25 15:15:17 +02:00
const [isLoading, setIsLoading] = useState(true);
const [user, setUser] = useState<CurrentUser | null>(null);
2022-12-01 23:07:49 +01:00
const [configVariables, setConfigVariables] = useState<Config[] | null>(null);
2022-05-02 08:22:53 +02:00
const getInitalData = async () => {
2022-04-25 15:15:17 +02:00
setIsLoading(true);
2022-12-01 23:07:49 +01:00
setConfigVariables(await configService.list());
await authService.refreshAccessToken();
setUser(await userService.getCurrentUser());
2022-04-25 15:15:17 +02:00
setIsLoading(false);
};
2022-10-10 22:30:04 +02:00
2022-04-25 15:15:17 +02:00
useEffect(() => {
setInterval(async () => await authService.refreshAccessToken(), 30 * 1000);
2022-05-02 08:22:53 +02:00
getInitalData();
2022-04-25 15:15:17 +02:00
}, []);
2022-10-10 22:30:04 +02:00
2022-12-01 23:07:49 +01:00
useEffect(() => {
if (
configVariables &&
configVariables.filter((variable) => variable.key)[0].value == "false" &&
!["/auth/signUp", "/admin/setup"].includes(router.asPath)
) {
2022-12-02 15:10:49 +01:00
router.push(!user ? "/auth/signUp" : "/admin/setup");
2022-12-01 23:07:49 +01:00
}
}, [router.asPath]);
2022-10-10 22:30:04 +02:00
useEffect(() => {
setColorScheme(systemTheme);
}, [systemTheme]);
2022-04-25 15:15:17 +02:00
return (
2022-10-10 22:30:04 +02:00
<MantineProvider
withGlobalStyles
withNormalizeCSS
theme={{ colorScheme, ...globalStyle }}
>
<GlobalStyle />
<NotificationsProvider>
<ModalsProvider>
<GlobalLoadingContext.Provider value={{ isLoading, setIsLoading }}>
{isLoading ? (
<LoadingOverlay visible overlayOpacity={1} />
) : (
2022-12-01 23:07:49 +01:00
<ConfigContext.Provider value={configVariables}>
<UserContext.Provider value={user}>
<LoadingOverlay visible={isLoading} overlayOpacity={1} />
<Header />
<Container>
<Component {...pageProps} />
</Container>
</UserContext.Provider>{" "}
</ConfigContext.Provider>
2022-10-10 22:30:04 +02:00
)}
</GlobalLoadingContext.Provider>
</ModalsProvider>
</NotificationsProvider>
2022-04-25 15:15:17 +02:00
</MantineProvider>
);
}
export default App;