2024-07-23 20:02:57 +02:00
|
|
|
"use client";
|
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
import ReactMarkdown from "react-markdown";
|
|
|
|
|
|
|
|
const Page = ({ params }: { params: { slug: string } }) => {
|
|
|
|
const { slug } = params;
|
|
|
|
const [page, setPage] = useState<{ title: string; content: string } | null>(
|
|
|
|
null
|
|
|
|
);
|
2024-07-25 20:29:21 +02:00
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
const [notFound, setNotFound] = useState(false);
|
2024-07-23 20:02:57 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
2024-07-25 20:29:21 +02:00
|
|
|
const fetchPage = async () => {
|
|
|
|
try {
|
|
|
|
const response = await fetch(`/api/mdx/pages/${slug}`);
|
|
|
|
if (response.ok) {
|
|
|
|
const data = await response.json();
|
|
|
|
setPage(data);
|
|
|
|
} else {
|
|
|
|
if (response.status === 404) {
|
|
|
|
setNotFound(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Failed to load page", error);
|
|
|
|
setNotFound(true);
|
|
|
|
} finally {
|
|
|
|
setLoading(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fetchPage();
|
2024-07-23 20:02:57 +02:00
|
|
|
}, [slug]);
|
|
|
|
|
2024-07-25 20:29:21 +02:00
|
|
|
if (loading) {
|
|
|
|
return (
|
|
|
|
<section className="flex-center flex-col wrapper container">
|
|
|
|
<p className="text-lg">Loading...</p>
|
|
|
|
</section>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (notFound) {
|
2024-07-23 20:02:57 +02:00
|
|
|
return (
|
|
|
|
<section id="404error" className="flex-center flex-col wrapper container">
|
|
|
|
<h1 className="text-3xl md:text-5xl text-center">
|
|
|
|
<span className="text-red-500">404</span> Page not Found
|
|
|
|
</h1>
|
|
|
|
<p className="text-lg mt-3 text-muted-foreground">
|
|
|
|
Please return back to Home
|
|
|
|
</p>
|
|
|
|
</section>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-25 20:29:21 +02:00
|
|
|
if (!page) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-07-23 20:02:57 +02:00
|
|
|
return (
|
2024-07-25 20:29:21 +02:00
|
|
|
<section className="wrapper container py-24 md:py-28 gap-4 flex flex-col">
|
|
|
|
<h1 className="text-3xl md:text-5xl font-bold text-black dark:bg-clip-text dark:text-transparent dark:bg-gradient-to-b dark:from-white dark:to-neutral-400">
|
|
|
|
{page.title}
|
|
|
|
</h1>
|
|
|
|
<ReactMarkdown className="prose max-w-full prose-lg dark:prose-invert">
|
|
|
|
{page.content}
|
|
|
|
</ReactMarkdown>
|
2024-07-23 20:02:57 +02:00
|
|
|
</section>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Page;
|