mirror of
https://github.com/ajnart/homarr.git
synced 2025-11-12 08:25:47 +01:00
🐛 Fix slug page and refactor server side translations code
This commit is contained in:
@@ -1,31 +1,27 @@
|
|||||||
import { getCookie } from 'cookies-next';
|
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { GetServerSidePropsContext } from 'next';
|
import { GetServerSidePropsContext } from 'next';
|
||||||
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
|
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { useEffect } from 'react';
|
|
||||||
import LoadConfigComponent from '../components/Config/LoadConfig';
|
import LoadConfigComponent from '../components/Config/LoadConfig';
|
||||||
import { Dashboard } from '../components/Dashboard/Dashboard';
|
import { Dashboard } from '../components/Dashboard/Dashboard';
|
||||||
import Layout from '../components/layout/Layout';
|
import Layout from '../components/layout/Layout';
|
||||||
import { useConfigContext } from '../config/provider';
|
import { useInitConfig } from '../config/init';
|
||||||
import { useConfigStore } from '../config/store';
|
import { getFrontendConfig } from '../tools/config/getFrontendConfig';
|
||||||
import { getConfig } from '../tools/getConfig';
|
import { getServerSideTranslations } from '../tools/getServerSideTranslations';
|
||||||
import { dashboardNamespaces } from '../tools/translation-namespaces';
|
import { dashboardNamespaces } from '../tools/translation-namespaces';
|
||||||
import { ConfigType } from '../types/config';
|
import { DashboardServerSideProps } from '../types/dashboardPageType';
|
||||||
|
|
||||||
type ServerSideProps = {
|
|
||||||
config: ConfigType;
|
|
||||||
};
|
|
||||||
|
|
||||||
export async function getServerSideProps({
|
export async function getServerSideProps({
|
||||||
req,
|
req,
|
||||||
res,
|
res,
|
||||||
locale,
|
locale,
|
||||||
query,
|
query,
|
||||||
}: GetServerSidePropsContext): Promise<{ props: ServerSideProps }> {
|
}: GetServerSidePropsContext): Promise<{ props: DashboardServerSideProps }> {
|
||||||
const configByUrl = query.slug;
|
const configName = query.slug as string;
|
||||||
const configPath = path.join(process.cwd(), 'data/configs', `${configByUrl}.json`);
|
const configPath = path.join(process.cwd(), 'data/configs', `${configName}.json`);
|
||||||
const configExists = fs.existsSync(configPath);
|
const configExists = fs.existsSync(configPath);
|
||||||
|
|
||||||
|
const translations = await getServerSideTranslations(req, res, dashboardNamespaces, locale);
|
||||||
|
|
||||||
if (!configExists) {
|
if (!configExists) {
|
||||||
// Redirect to 404
|
// Redirect to 404
|
||||||
res.writeHead(301, { Location: '/404' });
|
res.writeHead(301, { Location: '/404' });
|
||||||
@@ -64,26 +60,26 @@ export async function getServerSideProps({
|
|||||||
wrappers: [],
|
wrappers: [],
|
||||||
widgets: [],
|
widgets: [],
|
||||||
},
|
},
|
||||||
|
configName,
|
||||||
|
...translations,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const configLocale = getCookie('config-locale', { req, res });
|
const config = getFrontendConfig(configName as string);
|
||||||
const targetLanguage = (configLocale ?? locale) as string;
|
|
||||||
const translations = await serverSideTranslations(targetLanguage, dashboardNamespaces);
|
return {
|
||||||
return getConfig(configByUrl as string, translations);
|
props: {
|
||||||
|
configName,
|
||||||
|
config,
|
||||||
|
...translations,
|
||||||
|
},
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function HomePage(props: any) {
|
export default function HomePage({ config: initialConfig }: DashboardServerSideProps) {
|
||||||
const { config: initialConfig }: { config: ConfigType } = props;
|
useInitConfig(initialConfig);
|
||||||
const { name: configName } = useConfigContext();
|
|
||||||
const { updateConfig } = useConfigStore();
|
|
||||||
useEffect(() => {
|
|
||||||
if (!configName) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
updateConfig(configName, () => initialConfig);
|
|
||||||
}, [initialConfig]);
|
|
||||||
return (
|
return (
|
||||||
<Layout>
|
<Layout>
|
||||||
<Dashboard />
|
<Dashboard />
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
import { getCookie, setCookie } from 'cookies-next';
|
import { getCookie, setCookie } from 'cookies-next';
|
||||||
import { GetServerSidePropsContext } from 'next';
|
import { GetServerSidePropsContext } from 'next';
|
||||||
import { SSRConfig } from 'next-i18next';
|
|
||||||
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
|
|
||||||
|
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import LoadConfigComponent from '../components/Config/LoadConfig';
|
import LoadConfigComponent from '../components/Config/LoadConfig';
|
||||||
@@ -9,22 +7,15 @@ import { Dashboard } from '../components/Dashboard/Dashboard';
|
|||||||
import Layout from '../components/layout/Layout';
|
import Layout from '../components/layout/Layout';
|
||||||
import { useInitConfig } from '../config/init';
|
import { useInitConfig } from '../config/init';
|
||||||
import { getFrontendConfig } from '../tools/config/getFrontendConfig';
|
import { getFrontendConfig } from '../tools/config/getFrontendConfig';
|
||||||
|
import { getServerSideTranslations } from '../tools/getServerSideTranslations';
|
||||||
import { dashboardNamespaces } from '../tools/translation-namespaces';
|
import { dashboardNamespaces } from '../tools/translation-namespaces';
|
||||||
import { ConfigType } from '../types/config';
|
import { DashboardServerSideProps } from '../types/dashboardPageType';
|
||||||
|
|
||||||
type ServerSideProps = {
|
|
||||||
config: ConfigType;
|
|
||||||
// eslint-disable-next-line react/no-unused-prop-types
|
|
||||||
configName: string;
|
|
||||||
// eslint-disable-next-line react/no-unused-prop-types
|
|
||||||
_nextI18Next: SSRConfig['_nextI18Next'];
|
|
||||||
};
|
|
||||||
|
|
||||||
export async function getServerSideProps({
|
export async function getServerSideProps({
|
||||||
req,
|
req,
|
||||||
res,
|
res,
|
||||||
locale,
|
locale,
|
||||||
}: GetServerSidePropsContext): Promise<{ props: ServerSideProps }> {
|
}: GetServerSidePropsContext): Promise<{ props: DashboardServerSideProps }> {
|
||||||
// Check that all the json files in the /data/configs folder are migrated
|
// Check that all the json files in the /data/configs folder are migrated
|
||||||
// If not, redirect to the migrate page
|
// If not, redirect to the migrate page
|
||||||
const configs = await fs.readdirSync('./data/configs');
|
const configs = await fs.readdirSync('./data/configs');
|
||||||
@@ -40,11 +31,10 @@ export async function getServerSideProps({
|
|||||||
});
|
});
|
||||||
res.end();
|
res.end();
|
||||||
|
|
||||||
return { props: {} as ServerSideProps };
|
return { props: {} as DashboardServerSideProps };
|
||||||
}
|
}
|
||||||
|
|
||||||
let configName = getCookie('config-name', { req, res });
|
let configName = getCookie('config-name', { req, res });
|
||||||
const configLocale = getCookie('config-locale', { req, res });
|
|
||||||
if (!configName) {
|
if (!configName) {
|
||||||
setCookie('config-name', 'default', {
|
setCookie('config-name', 'default', {
|
||||||
req,
|
req,
|
||||||
@@ -55,10 +45,7 @@ export async function getServerSideProps({
|
|||||||
configName = 'default';
|
configName = 'default';
|
||||||
}
|
}
|
||||||
|
|
||||||
const translations = await serverSideTranslations(
|
const translations = await getServerSideTranslations(req, res, dashboardNamespaces, locale);
|
||||||
(configLocale ?? locale) as string,
|
|
||||||
dashboardNamespaces
|
|
||||||
);
|
|
||||||
const config = getFrontendConfig(configName as string);
|
const config = getFrontendConfig(configName as string);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -66,7 +53,7 @@ export async function getServerSideProps({
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function HomePage({ config: initialConfig }: ServerSideProps) {
|
export default function HomePage({ config: initialConfig }: DashboardServerSideProps) {
|
||||||
useInitConfig(initialConfig);
|
useInitConfig(initialConfig);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
19
src/tools/getServerSideTranslations.ts
Normal file
19
src/tools/getServerSideTranslations.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import { getCookie } from 'cookies-next';
|
||||||
|
import { IncomingMessage, ServerResponse } from 'http';
|
||||||
|
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
|
||||||
|
|
||||||
|
export const getServerSideTranslations = async (
|
||||||
|
req: IncomingMessage,
|
||||||
|
res: ServerResponse,
|
||||||
|
namespaces: string[],
|
||||||
|
requestLocale?: string
|
||||||
|
) => {
|
||||||
|
const configLocale = getCookie('config-locale', { req, res });
|
||||||
|
|
||||||
|
const translations = await serverSideTranslations(
|
||||||
|
(configLocale ?? requestLocale ?? 'en') as string,
|
||||||
|
namespaces
|
||||||
|
);
|
||||||
|
|
||||||
|
return translations;
|
||||||
|
};
|
||||||
10
src/types/dashboardPageType.ts
Normal file
10
src/types/dashboardPageType.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import { SSRConfig } from 'next-i18next';
|
||||||
|
import { ConfigType } from './config';
|
||||||
|
|
||||||
|
export type DashboardServerSideProps = {
|
||||||
|
config: ConfigType;
|
||||||
|
// eslint-disable-next-line react/no-unused-prop-types
|
||||||
|
configName: string;
|
||||||
|
// eslint-disable-next-line react/no-unused-prop-types
|
||||||
|
_nextI18Next: SSRConfig['_nextI18Next'];
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user