Files
Homarr/src/pages/index.tsx

64 lines
1.9 KiB
TypeScript
Raw Normal View History

import { getCookie, setCookies } from 'cookies-next';
import { GetServerSidePropsContext } from 'next';
import path from 'path';
import fs from 'fs';
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';
import { Config } from '../tools/types';
import { useConfig } from '../tools/state';
2022-05-21 01:26:24 +02:00
import { migrateToIdConfig } from '../tools/migrate';
import { ModuleWrapper } from '../components/modules/moduleWrapper';
import { DownloadsModule } from '../components/modules';
2022-04-24 22:36:47 +02:00
export async function getServerSideProps({
req,
res,
}: GetServerSidePropsContext): Promise<{ props: { config: Config } }> {
let cookie = getCookie('config-name', { req, res });
if (!cookie) {
setCookies('config-name', 'default', { req, res, maxAge: 60 * 60 * 24 * 30 });
cookie = 'default';
}
// Check if the config file exists
const configPath = path.join(process.cwd(), 'data/configs', `${cookie}.json`);
if (!fs.existsSync(configPath)) {
return {
props: {
config: {
name: cookie.toString(),
services: [],
settings: {
searchUrl: 'https://www.google.com/search?q=',
},
modules: {},
},
},
};
}
const config = fs.readFileSync(configPath, 'utf8');
// Print loaded config
return {
props: {
config: JSON.parse(config),
},
};
}
export default function HomePage(props: any) {
const { config: initialConfig }: { config: Config } = props;
const { config, loadConfig, setConfig, getConfigs } = useConfig();
useEffect(() => {
2022-05-21 01:26:24 +02:00
const migratedConfig = migrateToIdConfig(initialConfig);
setConfig(migratedConfig);
}, [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 />
<ModuleWrapper mt="xl" module={DownloadsModule} />
2022-04-24 22:36:47 +02:00
</>
);
}