Files
freeCodeCamp/client/utils/gatsby/layout-selector.tsx
Oliver Eyton-Williams e955bccfcf refactor: replace isChallenge (#50033)
* refactor: replace isChallenge

Determining if a path is a challenge by the number of path segments is
brittle and we ended up writing bizarre things like
isChallenge(nextChallengePath).

This should be a little more robust. i.e. if we need to know if a page
is a challenge, we can check the challengeMeta

* test: update tests with new logic
2023-04-13 15:25:12 +00:00

54 lines
1.4 KiB
TypeScript

import React from 'react';
import CertificationLayout from '../../src/components/layouts/certification';
import DefaultLayout from '../../src/components/layouts/default';
import FourOhFourPage from '../../src/pages/404';
interface LayoutSelectorProps {
element: JSX.Element;
props: {
location: { pathname: string };
pageContext?: { challengeMeta?: { block?: string; superBlock?: string } };
};
}
export default function layoutSelector({
element,
props
}: LayoutSelectorProps): JSX.Element {
const {
location: { pathname }
} = props;
const isChallenge = !!props.pageContext?.challengeMeta;
if (element.type === FourOhFourPage) {
return (
<DefaultLayout pathname={pathname} showFooter={true}>
{element}
</DefaultLayout>
);
} else if (/\/certification\//.test(pathname)) {
return (
<CertificationLayout pathname={pathname}>{element}</CertificationLayout>
);
} else if (isChallenge) {
return (
<DefaultLayout
pathname={pathname}
showFooter={false}
isChallenge={true}
block={props.pageContext?.challengeMeta?.block}
superBlock={props.pageContext?.challengeMeta?.superBlock}
>
{element}
</DefaultLayout>
);
} else {
return (
<DefaultLayout pathname={pathname} showFooter={true}>
{element}
</DefaultLayout>
);
}
}