Files
calendar/apps/web/app/layoutHOC.tsx
T
Benny JooandGitHub d4b0516235 chore: App router migration - (/more, /maintenance, /connect-and-join, /upgrade) finish migration (#16497)
* more: extract client component into /modules folder

* maintenance: extract client component into /modules folder

* upgrade: extract client component into /modules folder

* improve WithLayout wrapper

* connect-and-join: extract client component into /modules folder

* add env vars
2024-09-06 17:22:05 +00:00

48 lines
1.5 KiB
TypeScript

import type { LayoutProps, PageProps } from "app/_types";
import { type GetServerSidePropsContext } from "next";
import { cookies, headers } from "next/headers";
import { buildLegacyCtx } from "@lib/buildLegacyCtx";
import PageWrapper from "@components/PageWrapperAppDir";
type WithLayoutParams<T extends Record<string, any>> = {
getLayout: ((page: React.ReactElement) => React.ReactNode) | null;
Page?: (props: T) => React.ReactElement | null;
getData?: (arg: GetServerSidePropsContext) => Promise<T | undefined>;
isBookingPage?: boolean;
requiresLicense?: boolean;
};
export function WithLayout<T extends Record<string, any>>({
getLayout,
getData,
Page,
isBookingPage,
requiresLicense,
}: WithLayoutParams<T>) {
return async <P extends "P" | "L">(p: P extends "P" ? PageProps : LayoutProps) => {
const h = headers();
const nonce = h.get("x-nonce") ?? undefined;
let props = {} as T;
if ("searchParams" in p && getData) {
props = (await getData(buildLegacyCtx(h, cookies(), p.params, p.searchParams))) ?? ({} as T);
}
const children = "children" in p ? p.children : null;
return (
<PageWrapper
getLayout={getLayout}
requiresLicense={requiresLicense || !!(Page && "requiresLicense" in Page && Page.requiresLicense)}
nonce={nonce}
themeBasis={null}
isBookingPage={isBookingPage || !!(Page && "isBookingPage" in Page && Page.isBookingPage)}
{...props}>
{Page ? <Page {...props} /> : children}
</PageWrapper>
);
};
}