1
0
mirror of https://github.com/stonith404/pingvin-share.git synced 2024-11-19 05:40:12 +01:00
pingvin-share/frontend/src/pages/_app.tsx

97 lines
2.9 KiB
TypeScript
Raw Normal View History

2022-04-25 15:15:17 +02:00
import {
ColorScheme,
Container,
LoadingOverlay,
MantineProvider,
2022-05-11 14:18:36 +02:00
Stack,
2022-04-25 15:15:17 +02:00
} 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";
2022-10-10 22:30:04 +02:00
import { getCookie, setCookies } from "cookies-next";
2022-04-25 15:15:17 +02:00
import { GetServerSidePropsContext } from "next";
import type { AppProps } from "next/app";
import { useEffect, useState } from "react";
2022-05-11 14:18:36 +02:00
import Footer from "../components/Footer";
2022-04-25 15:15:17 +02:00
import Header from "../components/navBar/NavBar";
import { UserContext } from "../hooks/user.hook";
import authService from "../services/auth.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 { CurrentUser } from "../types/user.type";
2022-04-25 15:15:17 +02:00
import { GlobalLoadingContext } from "../utils/loading.util";
2022-05-02 22:42:27 +02:00
function App(
props: AppProps & { colorScheme: ColorScheme; environmentVariables: any }
) {
const { Component, pageProps } = props;
2022-10-10 22:30:04 +02:00
const systemTheme = useColorScheme();
2022-04-25 15:15:17 +02:00
const [colorScheme, setColorScheme] = useState<ColorScheme>(
props.colorScheme
);
const [isLoading, setIsLoading] = useState(true);
const [user, setUser] = useState<CurrentUser | null>(null);
2022-05-02 08:22:53 +02:00
const getInitalData = async () => {
2022-04-25 15:15:17 +02:00
setIsLoading(true);
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
useEffect(() => {
setCookies("color-schema", systemTheme, {
2022-10-10 22:30:04 +02:00
maxAge: 60 * 60 * 24 * 30,
});
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} />
) : (
<UserContext.Provider value={user}>
<LoadingOverlay visible={isLoading} overlayOpacity={1} />
<Stack justify="space-between" sx={{ minHeight: "100vh" }}>
<div>
<Header />
<Container>
<Component {...pageProps} />
</Container>
</div>
<Footer />
</Stack>
</UserContext.Provider>
)}
</GlobalLoadingContext.Provider>
</ModalsProvider>
</NotificationsProvider>
2022-04-25 15:15:17 +02:00
</MantineProvider>
);
}
export default App;
2022-05-02 22:42:27 +02:00
App.getInitialProps = ({ ctx }: { ctx: GetServerSidePropsContext }) => {
return {
colorScheme: getCookie("color-schema", ctx) || "light",
2022-05-02 22:42:27 +02:00
};
};