Files
Frontend/src/app/(pages)/documentation/[[...slug]]/page.tsx

86 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-04-20 02:35:52 +01:00
import { getDocumentation } from "@/app/common/documentation";
import { CustomMDX } from "@/app/components/mdx-components";
import { Metadata } from "next";
2024-04-20 02:35:52 +01:00
import { generateEmbed } from "@/app/common/embed";
2024-04-20 03:22:10 +01:00
import { Title } from "@/app/components/title";
type DocumentationPageParams = {
params: {
slug?: string[];
};
};
2024-04-20 00:34:42 +01:00
export async function generateStaticParams() {
let documentationPages = getDocumentation();
return documentationPages.map(page => ({
slug: [page.slug],
}));
}
2024-04-20 03:22:10 +01:00
/**
* Gets a documentation page by its slug.
*
* @param slug The slug of the documentation page.
*/
function getPage(slug?: string) {
const documentationPages = getDocumentation();
2024-04-20 03:22:10 +01:00
let page = documentationPages.find(page => page.slug === slug);
2024-04-20 03:22:10 +01:00
// Fallback to the landing page
if (!page && !slug) {
page = documentationPages.find(page => page.slug === "landing");
}
2024-04-20 03:22:10 +01:00
// We still can't find the page, return undefined
if (!page) {
return undefined;
}
return page;
}
export async function generateMetadata({ params: { slug } }: DocumentationPageParams): Promise<Metadata> {
const page = getPage(slug?.join("/"));
// Fallback to page not found
if (!page) {
return generateEmbed({
title: "Page not found",
description: "The documentation page was not found",
});
}
return generateEmbed({
2024-04-20 03:04:23 +01:00
title: `${page.metadata.title} - Documentation`,
description: `${page.metadata.description}\n\nClick to view this page`,
});
}
2024-04-20 00:34:42 +01:00
export default function Page({ params: { slug } }: DocumentationPageParams) {
2024-04-20 03:22:10 +01:00
const page = getPage(slug?.join("/"));
2024-04-20 00:34:42 +01:00
// Page was not found, show an error page
2024-04-20 00:34:42 +01:00
if (!page) {
return (
<div className="text-center flex flex-col gap-2">
<h1 className="text-red-400 text-2xl">Not Found</h1>
<p>The page you are looking for was not found.</p>
</div>
);
2024-04-20 00:34:42 +01:00
}
return (
<div className="w-full px-4 flex flex-col gap-4">
{/* The documentation page title and description */}
<div className="text-center mb-4">
2024-04-20 03:22:10 +01:00
<Title title={page.metadata.title} subtitle={page.metadata.description} />
</div>
2024-04-20 00:34:42 +01:00
{/* The content of the documentation page */}
<div className="text-left w-full">
<CustomMDX source={page.content} />
</div>
</div>
);
}