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> = { getLayout?: ((page: React.ReactElement) => React.ReactNode) | null; getServerLayout?: (page: React.ReactElement) => Promise; Page?: (props: T) => React.ReactElement | null; ServerPage?: (props: T) => Promise | null; getData?: (arg: GetServerSidePropsContext) => Promise; isBookingPage?: boolean; requiresLicense?: boolean; }; export function WithLayout>({ getLayout, getServerLayout, getData, ServerPage, Page, isBookingPage, requiresLicense, }: WithLayoutParams) { // eslint-disable-next-line react/display-name return async

(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); } // `p.children` exists only for layout.tsx files const childrenFromLayoutFile = "children" in p ? p.children : null; const page = ServerPage ? ( await ServerPage({ ...props, ...p }) ) : Page ? ( ) : ( childrenFromLayoutFile ); const pageWithServerLayout = page ? (getServerLayout ? await getServerLayout(page) : page) : null; return ( {pageWithServerLayout} ); }; }