Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DmytroHryshyn committed Jan 11, 2024
1 parent 5950c5a commit 8393c8c
Show file tree
Hide file tree
Showing 48 changed files with 859 additions and 1,182 deletions.
17 changes: 0 additions & 17 deletions apps/web/app/AppDirSSRHOC.tsx

This file was deleted.

26 changes: 26 additions & 0 deletions apps/web/app/WithAppDirSsg.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { GetStaticProps } from "next";
import { notFound, redirect } from "next/navigation";

import type { buildLegacyCtx } from "@lib/buildLegacyCtx";

export const withAppDirSsg =
<T extends Record<string, any>>(getStaticProps: GetStaticProps<T>) =>
async (context: ReturnType<typeof buildLegacyCtx>) => {
const ssgResponse = await getStaticProps(context);

if ("redirect" in ssgResponse) {
redirect(ssgResponse.redirect.destination);
}

if ("notFound" in ssgResponse) {
notFound();
}

const props = await Promise.resolve(ssgResponse.props);

return {
...ssgResponse.props,
// includes dehydratedState required for future page trpcPropvider
...("trpcState" in props && { dehydratedState: props.trpcState }),
};
};
24 changes: 24 additions & 0 deletions apps/web/app/WithAppDirSsr.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { GetServerSideProps, GetServerSidePropsContext } from "next";
import { notFound, redirect } from "next/navigation";

export const withAppDirSsr =
<T extends Record<string, any>>(getServerSideProps: GetServerSideProps<T>) =>
async (context: GetServerSidePropsContext) => {
const ssrResponse = await getServerSideProps(context);

if ("redirect" in ssrResponse) {
redirect(ssrResponse.redirect.destination);
}

if ("notFound" in ssrResponse) {
notFound();
}

const props = await Promise.resolve(ssrResponse.props);

return {
...props,
// includes dehydratedState required for future page trpcPropvider
...("trpcState" in props && { dehydratedState: props.trpcState }),
};
};
3 changes: 0 additions & 3 deletions apps/web/app/future/apps/[slug]/layout.tsx

This file was deleted.

113 changes: 16 additions & 97 deletions apps/web/app/future/apps/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,27 @@
import AppPage from "@pages/apps/[slug]/index";
import Page from "@pages/apps/[slug]/index";
import { Prisma } from "@prisma/client";
import { withAppDirSsg } from "app/WithAppDirSsg";
import { _generateMetadata } from "app/_utils";
import fs from "fs";
import matter from "gray-matter";
import { notFound } from "next/navigation";
import path from "path";
import { z } from "zod";
import { WithLayout } from "app/layoutHOC";
import type { InferGetStaticPropsType } from "next";
import { cookies, headers } from "next/headers";

import { getAppWithMetadata } from "@calcom/app-store/_appRegistry";
import { getAppAssetFullPath } from "@calcom/app-store/getAppAssetFullPath";
import { APP_NAME, IS_PRODUCTION } from "@calcom/lib/constants";
import { APP_NAME } from "@calcom/lib/constants";
import prisma from "@calcom/prisma";

const sourceSchema = z.object({
content: z.string(),
data: z.object({
description: z.string().optional(),
items: z
.array(
z.union([
z.string(),
z.object({
iframe: z.object({ src: z.string() }),
}),
])
)
.optional(),
}),
});
import { getStaticProps } from "@lib/apps/[slug]/getStaticProps";
import { buildLegacyCtx } from "@lib/buildLegacyCtx";

type Y = InferGetStaticPropsType<typeof getStaticProps>;
const getData = withAppDirSsg<Y>(getStaticProps);

export const generateMetadata = async ({ params }: { params: Record<string, string | string[]> }) => {
const { data } = await getPageProps({ params });
const legacyContext = buildLegacyCtx(headers(), cookies(), params);
const res = await getData(legacyContext);

return await _generateMetadata(
() => `${data.name} | ${APP_NAME}`,
() => data.description
() => `${res?.data.name} | ${APP_NAME}`,
() => res?.data.description ?? ""
);
};

Expand All @@ -53,74 +40,6 @@ export const generateStaticParams = async () => {
return [];
};

const getPageProps = async ({ params }: { params: Record<string, string | string[]> }) => {
if (typeof params?.slug !== "string") {
notFound();
}

const appMeta = await getAppWithMetadata({
slug: params?.slug,
});

const appFromDb = await prisma.app.findUnique({
where: { slug: params.slug.toLowerCase() },
});

const isAppAvailableInFileSystem = appMeta;
const isAppDisabled = isAppAvailableInFileSystem && (!appFromDb || !appFromDb.enabled);

if (!IS_PRODUCTION && isAppDisabled) {
return {
isAppDisabled: true as const,
data: {
...appMeta,
},
};
}

if (!appFromDb || !appMeta || isAppDisabled) {
notFound();
}

const isTemplate = appMeta.isTemplate;
const appDirname = path.join(isTemplate ? "templates" : "", appFromDb.dirName);
const README_PATH = path.join(process.cwd(), "..", "..", `packages/app-store/${appDirname}/DESCRIPTION.md`);
const postFilePath = path.join(README_PATH);
let source = "";

try {
source = fs.readFileSync(postFilePath).toString();
source = source.replace(/{DESCRIPTION}/g, appMeta.description);
} catch (error) {
/* If the app doesn't have a README we fallback to the package description */
console.log(`No DESCRIPTION.md provided for: ${appDirname}`);
source = appMeta.description;
}

const result = matter(source);
const { content, data } = sourceSchema.parse({ content: result.content, data: result.data });
if (data.items) {
data.items = data.items.map((item) => {
if (typeof item === "string") {
return getAppAssetFullPath(item, {
dirName: appMeta.dirName,
isTemplate: appMeta.isTemplate,
});
}
return item;
});
}
return {
isAppDisabled: false as const,
source: { content, data },
data: appMeta,
};
};

export default async function Page({ params }: { params: Record<string, string | string[]> }) {
const pageProps = await getPageProps({ params });

return <AppPage {...pageProps} />;
}
export default WithLayout({ getLayout: null, Page, getData });

export const dynamic = "force-static";
28 changes: 4 additions & 24 deletions apps/web/app/future/apps/[slug]/setup/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import SetupPage from "@pages/apps/[slug]/setup";
import Page from "@pages/apps/[slug]/setup";
import { withAppDirSsr } from "app/WithAppDirSsr";
import { _generateMetadata } from "app/_utils";
import type { GetServerSidePropsContext } from "next";
import { cookies, headers } from "next/headers";
import { notFound, redirect } from "next/navigation";
import { WithLayout } from "app/layoutHOC";

import { getServerSideProps } from "@calcom/app-store/_pages/setup/_getServerSideProps";
import { APP_NAME } from "@calcom/lib/constants";
Expand All @@ -14,23 +13,4 @@ export const generateMetadata = async ({ params }: { params: Record<string, stri
);
};

const getPageProps = async ({ params }: { params: Record<string, string | string[]> }) => {
const req = { headers: headers(), cookies: cookies() };

const result = await getServerSideProps({ params, req } as unknown as GetServerSidePropsContext);

if (!result || "notFound" in result) {
notFound();
}

if ("redirect" in result) {
redirect(result.redirect.destination);
}

return result.props;
};

export default async function Page({ params }: { params: Record<string, string | string[]> }) {
const pageProps = await getPageProps({ params });
return <SetupPage {...pageProps} />;
}
export default WithLayout({ getLayout: null, Page, getData: withAppDirSsr(getServerSideProps) });
38 changes: 4 additions & 34 deletions apps/web/app/future/apps/categories/page.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import LegacyPage from "@pages/apps/categories/index";
import Page from "@pages/apps/categories/index";
import { withAppDirSsr } from "app/WithAppDirSsr";
import { _generateMetadata } from "app/_utils";
import { WithLayout } from "app/layoutHOC";

import { getAppRegistry, getAppRegistryWithCredentials } from "@calcom/app-store/_appRegistry";
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
import { APP_NAME } from "@calcom/lib/constants";

import type { buildLegacyCtx } from "@lib/buildLegacyCtx";

import { ssrInit } from "@server/lib/ssr";
import { getServerSideProps } from "@lib/apps/categories/getServerSideProps";

export const generateMetadata = async () => {
return await _generateMetadata(
Expand All @@ -17,31 +14,4 @@ export const generateMetadata = async () => {
);
};

const getData = async (ctx: ReturnType<typeof buildLegacyCtx>) => {
// @ts-expect-error Argument of type '{ query: Params; params: Params; req: { headers: ReadonlyHeaders; cookies: ReadonlyRequestCookies; }; }' is not assignable to parameter of type 'GetServerSidePropsContext'.
const ssr = await ssrInit(ctx);

// @ts-expect-error Type '{ headers: ReadonlyHeaders; cookies: ReadonlyRequestCookies; }' is not assignable to type 'NextApiRequest | IncomingMessage
const session = await getServerSession({ req: ctx.req });

let appStore;
if (session?.user?.id) {
appStore = await getAppRegistryWithCredentials(session.user.id);
} else {
appStore = await getAppRegistry();
}

const categories = appStore.reduce((c, app) => {
for (const category of app.categories) {
c[category] = c[category] ? c[category] + 1 : 1;
}
return c;
}, {} as Record<string, number>);

return {
categories: Object.entries(categories).map(([name, count]) => ({ name, count })),
dehydratedState: ssr.dehydrate(),
};
};

export default WithLayout({ getData, Page: LegacyPage, getLayout: null })<"P">;
export default WithLayout({ getData: withAppDirSsr(getServerSideProps), Page, getLayout: null })<"P">;
3 changes: 0 additions & 3 deletions apps/web/app/future/apps/installed/[category]/layout.tsx

This file was deleted.

29 changes: 6 additions & 23 deletions apps/web/app/future/apps/installed/[category]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import LegacyPage from "@pages/apps/installed/[category]";
import Page from "@pages/apps/installed/[category]";
import { withAppDirSsr } from "app/WithAppDirSsr";
import { _generateMetadata } from "app/_utils";
import { notFound } from "next/navigation";
import { z } from "zod";
import { WithLayout } from "app/layoutHOC";

import { APP_NAME } from "@calcom/lib/constants";
import { AppCategories } from "@calcom/prisma/enums";

const querySchema = z.object({
category: z.nativeEnum(AppCategories),
});
import { getServerSideProps } from "@lib/apps/installed/[category]/getServerSideProps";

export const generateMetadata = async () => {
return await _generateMetadata(
Expand All @@ -17,20 +14,6 @@ export const generateMetadata = async () => {
);
};

const getPageProps = async ({ params }: { params: Record<string, string | string[]> }) => {
const p = querySchema.safeParse(params);
const getData = withAppDirSsr(getServerSideProps);

if (!p.success) {
return notFound();
}

return {
category: p.data.category,
};
};

export default async function Page({ params }: { params: Record<string, string | string[]> }) {
await getPageProps({ params });

return <LegacyPage />;
}
export default WithLayout({ getLayout: null, getData, Page });
Loading

0 comments on commit 8393c8c

Please sign in to comment.