Refactor Docker integration in board page

This commit is contained in:
ajnart
2023-12-31 11:25:21 +01:00
parent f20c209c94
commit c83b04dfc0
2 changed files with 31 additions and 25 deletions

View File

@@ -1,41 +1,43 @@
import { GetServerSideProps, InferGetServerSidePropsType } from 'next';
import { SSRConfig } from 'next-i18next';
import { GetServerSidePropsContext, InferGetServerSidePropsType } from 'next';
import { z } from 'zod';
import { Dashboard } from '~/components/Dashboard/Dashboard';
import { BoardLayout } from '~/components/layout/Templates/BoardLayout';
import { useInitConfig } from '~/config/init';
import { env } from '~/env';
import { dockerRouter } from '~/server/api/routers/docker/router';
import { getServerAuthSession } from '~/server/auth';
import { configExists } from '~/tools/config/configExists';
import { getFrontendConfig } from '~/tools/config/getFrontendConfig';
import { getServerSideTranslations } from '~/tools/server/getServerSideTranslations';
import { checkForSessionOrAskForLogin } from '~/tools/server/loginBuilder';
import { boardNamespaces } from '~/tools/server/translation-namespaces';
import { ConfigType } from '~/types/config';
import { api } from '~/utils/api';
export default function BoardPage({
config: initialConfig,
isDockerEnabled,
initialContainers,
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
useInitConfig(initialConfig);
const { data } = api.docker.containers.useQuery(undefined, {
initialData: initialContainers ?? undefined,
enabled: isDockerEnabled,
cacheTime: 60 * 1000 * 5,
staleTime: 60 * 1000 * 1,
});
return (
<BoardLayout>
<BoardLayout isDockerEnabled={isDockerEnabled}>
<Dashboard />
</BoardLayout>
);
}
type BoardGetServerSideProps = {
config: ConfigType;
_nextI18Next?: SSRConfig['_nextI18Next'];
};
const routeParamsSchema = z.object({
slug: z.string(),
});
export const getServerSideProps: GetServerSideProps<BoardGetServerSideProps> = async (ctx) => {
const routeParams = routeParamsSchema.safeParse(ctx.params);
export const getServerSideProps = async (context: GetServerSidePropsContext) => {
const routeParams = routeParamsSchema.safeParse(context.params);
if (!routeParams.success) {
return {
notFound: true,
@@ -52,21 +54,30 @@ export const getServerSideProps: GetServerSideProps<BoardGetServerSideProps> = a
const config = await getFrontendConfig(routeParams.data.slug);
const translations = await getServerSideTranslations(
boardNamespaces,
ctx.locale,
ctx.req,
ctx.res
context.locale,
context.req,
context.res
);
const session = await getServerAuthSession({ req: ctx.req, res: ctx.res });
const session = await getServerAuthSession({ req: context.req, res: context.res });
const result = checkForSessionOrAskForLogin(
ctx,
context,
session,
() => config.settings.access.allowGuests || session?.user != undefined
);
if (result) {
return result;
}
const caller = dockerRouter.createCaller({
session: session,
cookies: context.req.cookies,
});
let containers = undefined;
// Fetch containers if user is admin, otherwise we don't need them
try {
if (session?.user.isAdmin == true) containers = await caller.containers();
} catch (error) {}
return {
props: {
@@ -74,6 +85,8 @@ export const getServerSideProps: GetServerSideProps<BoardGetServerSideProps> = a
primaryColor: config.settings.customization.colors.primary,
secondaryColor: config.settings.customization.colors.secondary,
primaryShade: config.settings.customization.colors.shade,
isDockerEnabled: containers != undefined,
initialContainers: containers ?? null,
...translations,
},
};

View File

@@ -33,11 +33,6 @@ export default function BoardPage({
);
}
type BoardGetServerSideProps = {
config: ConfigType;
_nextI18Next?: SSRConfig['_nextI18Next'];
};
export const getServerSideProps = async (context: GetServerSidePropsContext) => {
const session = await getServerAuthSession(context);
const boardName = await getDefaultBoardAsync(session?.user?.id, 'default');
@@ -66,9 +61,7 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
// Fetch containers if user is admin, otherwise we don't need them
try {
if (session?.user.isAdmin == true) containers = await caller.containers();
} catch (error) {
}
} catch (error) {}
return {
props: {
config,