'use client'; import { useState, useEffect } from 'react'; import ReactMarkdown from 'react-markdown'; import remarkGfm from 'remark-gfm'; import { useParams } from 'next/navigation'; import Image from 'next/image'; import { Tag } from 'lucide-react'; export default function PostPage() { const [markdown, setMarkdown] = useState(''); const [error, setError] = useState(''); const [loading, setLoading] = useState(true); const [postTitle, setPostTitle] = useState(''); const [postDate, setPostDate] = useState(''); const [postCategory, setPostCategory] = useState(''); const { slug } = useParams(); useEffect(() => { interface PostData { title?: string; date?: string; category?: string; message?: string; } function setPostData(postData: PostData) { setPostTitle(postData.title || 'Untitled Post'); if (postData.date) { console.log("[i] Date:", postData.date); const date = new Date(Number(postData.date) * 1000) console.log("[i] Date object:", date); const options: Intl.DateTimeFormatOptions = { year: 'numeric', month: 'long', day: 'numeric' }; console.log("[i] Date options:", options); console.log("[i] Formatted date:", date.toLocaleDateString(undefined, options)); setPostDate(date.toLocaleDateString(undefined, options)); } else { setPostDate('an unknown date'); } if (postData.category) { setPostCategory(postData.category); } else if (postData.message) { setPostCategory(postData.message); } else { setPostCategory("Error"); } } console.log("[i] Navigated to slug: ", slug); (async () => { try { const res = await fetch(`http://localhost:3001/api/posts/get/${slug}`, { cache: 'no-store', }); const contentType = res.headers.get('content-type') || ''; if (contentType.includes('application/json')) { const data = await res.json(); if (data.success === false) { if (data.message) { setLoading(false); setError(data.message); throw new Error(data.message); } else { setLoading(false); setError('Unknown error occurred'); throw new Error('Unknown error occurred'); } } } else { const text = await res.text(); const catRes = await fetch(`http://localhost:3001/api/posts/getPostDetails/${slug}`, { cache: 'no-store', }); const postData = await catRes.json(); if (postData.success) { setPostData(postData); } else { if (postData.message) { setPostData(postData); } else { setPostCategory("Error"); } } setMarkdown(text); setLoading(false); } } catch (error) { console.error('Error fetching post:', error); setError('Could not load the post.'); setMarkdown('# Error\n\nCould not load the post.'); } })(); }, [slug]); if (loading) { return (
); } if (error) { return (
{error}
); } return (

{postTitle}

Published on {postDate}

{postCategory}

, h2: ({...props}) =>

, h3: ({...props}) =>

, h4: ({...props}) =>

, h5: ({...props}) =>

, em: ({...props}) => , hr: ({...props}) =>
, p: ({...props}) =>

, a: ({...props}) => , img: ({src, alt, width, height, ...props}) => {alt, blockquote: ({...props}) =>

, code: ({...props}) => , pre: ({...props}) =>
,
        table: ({...props}) => ,
        thead: ({...props}) => ,
        th: ({...props}) => 
, td: ({...props}) => , ul: ({...props}) =>
    , ol: ({...props}) =>
      , li: ({...props}) =>
    1. , input: ({type, ...props}) => type === 'checkbox' ? : }}> {markdown} ); }