cal/apps/web/app/layoutHOC.tsx
DmytroHryshyn 070ec326aa
chore: [app-router-migration 13] Migrate reschedule page group (#13030)
* app-boilerplate-codemod

* fin

* fix

* fix & test & move to new struct

* manual: fix type error

* manual: fix lint error

---------

Co-authored-by: Benny Joo <sldisek783@gmail.com>
2024-01-11 07:50:29 -03:00

29 lines
1.1 KiB
TypeScript

import type { LayoutProps, PageProps } from "app/_types";
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: ReturnType<typeof buildLegacyCtx>) => Promise<T>;
};
export function WithLayout<T extends Record<string, any>>({ getLayout, getData, Page }: WithLayoutParams<T>) {
return async <P extends "P" | "L">(p: P extends "P" ? PageProps : LayoutProps) => {
const h = headers();
const nonce = h.get("x-nonce") ?? undefined;
const props = getData ? await getData(buildLegacyCtx(h, cookies(), p.params)) : ({} as T);
const children = "children" in p ? p.children : null;
return (
<PageWrapper getLayout={getLayout} requiresLicense={false} nonce={nonce} themeBasis={null} {...props}>
{Page ? <Page {...props} /> : children}
</PageWrapper>
);
};
}