2022-05-12 21:38:21 +02:00
|
|
|
import { getCookie, setCookies } from 'cookies-next';
|
|
|
|
|
import { GetServerSidePropsContext } from 'next';
|
|
|
|
|
import { useEffect } from 'react';
|
2022-04-25 00:11:32 +02:00
|
|
|
import AppShelf from '../components/AppShelf/AppShelf';
|
2022-04-27 03:12:53 +02:00
|
|
|
import LoadConfigComponent from '../components/Config/LoadConfig';
|
2022-05-12 21:38:21 +02:00
|
|
|
import { Config } from '../tools/types';
|
|
|
|
|
import { useConfig } from '../tools/state';
|
2022-05-21 01:26:24 +02:00
|
|
|
import { migrateToIdConfig } from '../tools/migrate';
|
2022-05-29 09:11:46 +02:00
|
|
|
import { getConfig } from '../tools/getConfig';
|
2022-04-24 22:36:47 +02:00
|
|
|
|
2022-05-12 21:38:21 +02:00
|
|
|
export async function getServerSideProps({
|
|
|
|
|
req,
|
|
|
|
|
res,
|
|
|
|
|
}: GetServerSidePropsContext): Promise<{ props: { config: Config } }> {
|
|
|
|
|
let cookie = getCookie('config-name', { req, res });
|
|
|
|
|
if (!cookie) {
|
2022-05-29 09:11:46 +02:00
|
|
|
setCookies('config-name', 'default', {
|
|
|
|
|
req,
|
|
|
|
|
res,
|
|
|
|
|
maxAge: 60 * 60 * 24 * 30,
|
|
|
|
|
sameSite: 'strict',
|
|
|
|
|
});
|
2022-05-12 21:38:21 +02:00
|
|
|
cookie = 'default';
|
|
|
|
|
}
|
2022-05-29 09:11:46 +02:00
|
|
|
return getConfig(cookie as string);
|
2022-05-12 21:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function HomePage(props: any) {
|
|
|
|
|
const { config: initialConfig }: { config: Config } = props;
|
2022-05-29 09:11:46 +02:00
|
|
|
const { setConfig } = useConfig();
|
2022-05-12 21:38:21 +02:00
|
|
|
useEffect(() => {
|
2022-05-21 01:26:24 +02:00
|
|
|
const migratedConfig = migrateToIdConfig(initialConfig);
|
|
|
|
|
setConfig(migratedConfig);
|
2022-05-12 21:38:21 +02:00
|
|
|
}, [initialConfig]);
|
2022-04-24 22:36:47 +02:00
|
|
|
return (
|
2022-05-16 13:54:54 +02:00
|
|
|
<>
|
2022-05-16 15:55:22 +02:00
|
|
|
<AppShelf />
|
2022-05-16 13:54:54 +02:00
|
|
|
<LoadConfigComponent />
|
2022-04-24 22:36:47 +02:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|