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

View File

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