oxen-website/pages/[page].tsx

148 lines
3.3 KiB
TypeScript
Raw Normal View History

2021-02-10 09:38:27 +01:00
// [slug].js
import Head from 'next/head';
import React, { useEffect } from 'react';
import { useDispatch } from 'react-redux';
2021-02-11 02:54:53 +01:00
import { Contained } from '../components/Contained';
import { RichBody } from '../components/RichBody';
2021-02-11 01:38:40 +01:00
import { NAVIGATION } from '../constants';
import { CmsApi, unslugify } from '../services/cms';
import { PageType, setPageType, SideMenuItem } from '../state/navigation';
2021-02-10 09:38:27 +01:00
import { ISplitPage } from '../types/cms';
import { generateTitle } from '../utils/metadata';
interface IPath {
2021-02-18 04:07:53 +01:00
params: { page: string; isRoadmap?: boolean };
2021-02-10 09:38:27 +01:00
}
export async function getStaticPaths() {
// Get paths to all pages
2021-02-11 01:38:40 +01:00
// Hardcoded in navigation constants.
// Contentful can edit entries but cannot add/remove
// without touching code.
const paths: IPath[] = Object.values(NAVIGATION.SIDE_MENU_ITEMS).map(
item => ({
params: { page: item.href },
}),
);
2021-02-10 09:38:27 +01:00
2021-02-11 02:54:53 +01:00
return { paths, fallback: true };
2021-02-10 09:38:27 +01:00
}
export async function getStaticProps({ params }) {
2021-02-11 01:38:40 +01:00
const id = unslugify(String(params?.page) ?? '');
2021-02-10 09:38:27 +01:00
2021-02-18 04:07:53 +01:00
// Roadmap page is special 👁️👄👁️
if (SideMenuItem[id] == [SideMenuItem.ROADMAP]) {
return {
props: {
page: null,
isRoadmap: true,
},
revalidate: 60,
};
}
2021-02-11 01:38:40 +01:00
const api = new CmsApi();
const page = await api.fetchPageById(SideMenuItem[id]);
2021-02-10 09:38:27 +01:00
if (!page) {
return { notFound: true };
}
return {
props: {
2021-02-11 01:38:40 +01:00
page,
2021-02-18 04:07:53 +01:00
isRoadmap: false,
2021-02-10 09:38:27 +01:00
},
revalidate: 60,
};
}
2021-02-18 04:07:53 +01:00
function Page({
page,
isRoadmap,
}: {
page: ISplitPage | null;
isRoadmap?: boolean;
}) {
console.log('[page] ➡️ isRoadmap:', isRoadmap);
2021-02-10 09:38:27 +01:00
2021-02-18 04:07:53 +01:00
const dispatch = useDispatch();
2021-02-10 09:38:27 +01:00
useEffect(() => {
dispatch(setPageType(PageType.NORMAL));
}, []);
return (
<>
<Head>
2021-02-18 04:07:53 +01:00
<title>
{generateTitle(
isRoadmap
? NAVIGATION.SIDE_MENU_ITEMS[SideMenuItem.ROADMAP].label
: page?.label,
)}
</title>
2021-02-10 09:38:27 +01:00
</Head>
2021-02-12 04:14:34 +01:00
<div className="bg-alt">
2021-02-18 04:07:53 +01:00
{isRoadmap ? (
<Roadmap />
) : (
<>
<div className="aspect-w-16 aspect-h-10">
2021-02-18 04:28:52 +01:00
<img
style={{ maxHeight: '90%' }}
src={page?.hero?.imageUrl}
className="object-cover w-full"
/>
2021-02-18 04:07:53 +01:00
</div>
<Contained>
<h1 className="w-10/12 mt-6 mb-4 text-4xl font-bold leading-none text-primary font-prompt">
{page?.title}
</h1>
<div className="mb-10">
<RichBody body={page?.body} />
</div>
</Contained>
</>
)}
</div>
</>
);
}
2021-02-11 02:54:53 +01:00
2021-02-18 04:07:53 +01:00
function Roadmap() {
return (
<>
<div className="aspect-w-16 aspect-h-10">
<div className="flex items-center justify-center">
<img
style={{ maxHeight: '90%' }}
src={'img/roadmap.png'}
className="w-full"
/>
</div>
</div>
2021-02-11 02:54:53 +01:00
2021-02-18 04:07:53 +01:00
<div className="flex flex-col px-6 space-y-10">
<img
style={{ maxHeight: '90%' }}
src={'img/session-roadmap.png'}
className="w-full rounded-md"
/>
<img
style={{ maxHeight: '90%' }}
src={'img/lokinet-roadmap.png'}
className="w-full rounded-md"
/>
2021-02-11 01:38:40 +01:00
</div>
2021-02-10 09:38:27 +01:00
</>
);
}
export default Page;