Skip to content

Commit

Permalink
fix PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
standeren committed Nov 5, 2024
1 parent f5bd8c3 commit 2fce3db
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,25 @@ export function ContentLibrary({ pages }: ContentLibraryProps): React.ReactEleme
return <ContentLibraryForPage pages={pages} currentPage={currentPage} />;
}

type ContentLibraryForPageProps<T extends PageName = 'landingPage'> = {
type ContentLibraryForPageProps<T extends PageName> = {
pages: PagesConfig;
currentPage: T;
};

function ContentLibraryForPage<T extends PageName = 'landingPage'>({
function ContentLibraryForPage<T extends PageName>({
pages,
currentPage,
}: ContentLibraryForPageProps<T>): React.ReactElement {
const router = new RouterRouteMapperImpl(pages);

const Component: PageComponent<Required<PagePropsMap<T>>> =
router.configuredRoutes.get(currentPage);
const Component: PageComponent<PagePropsMap<T>> = router.configuredRoutes.get(currentPage);
if (!Component) return <StudioHeading>404 Page Not Found</StudioHeading>; // Show the NotFound page from app-dev instead

return (
<div className={classes.libraryBackground}>
<div className={classes.libraryContainer}>
<LibraryHeader />
<LibraryBody Component={Component} pages={pages} currentPage={currentPage} />
<LibraryBody<T> Component={Component} pages={pages} currentPage={currentPage} />
</div>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,52 @@ import type { PagePropsMap, PagesConfig } from '../../types/PagesProps';
import type { PageName } from '../../types/PageName';
import type { PageComponent } from '../../utils/router/RouterRouteMapper';

type LibraryBodyProps<T extends PageName = 'landingPage'> = {
type LibraryBodyProps<T extends PageName> = {
Component: PageComponent<PagePropsMap<T>>;
pages: PagesConfig;
currentPage: PageName;
currentPage: T;
};

export function LibraryBody<T extends PageName = 'landingPage'>({
export function LibraryBody<T extends PageName>({
Component,
pages,
currentPage,
}: LibraryBodyProps) {
const componentProps: PagePropsMap<T> = pages[currentPage].props as PagePropsMap<T>;
}: LibraryBodyProps<T>) {
const componentProps: PagePropsMap<T> = getComponentProps(pages, currentPage);

return (
<div className={classes.libraryContent}>
<PagesRouter pageNames={getAllPageNamesFromPagesConfig(pages)} />
<Page<T> Component={Component} componentProps={componentProps} currentPage={currentPage} />
</div>
);
}

type PageProps<T extends PageName> = {
Component: PageComponent<PagePropsMap<T>>;
componentProps: PagePropsMap<T>;
currentPage: T;
};

function Page<T extends PageName>({ Component, componentProps, currentPage }: PageProps<T>) {
return (
<>
<div className={classes.component}>
<Component {...(componentProps ?? {})} />
<Component {...componentProps} />
</div>
<InfoBox pageName={currentPage} />
</div>
</>
);
}

const getComponentProps = <T extends PageName>(
pages: PagesConfig,
currentPage: T,
): PagePropsMap<T> => {
if (currentPage === 'landingPage') return {} as PagePropsMap<T>;
return pages[currentPage].props;
};

const getAllPageNamesFromPagesConfig = (pages: PagesConfig): PageName[] => {
const customPages = Object.keys(pages) as PageName[];
return ['landingPage', ...customPages];
Expand Down

0 comments on commit 2fce3db

Please sign in to comment.