'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'; import strings from "@/strings.json" 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 || strings.blankPostTitlePlaceholder); if (postData.date) { const date = new Date(Number(postData.date) * 1000) const options: Intl.DateTimeFormatOptions = { year: 'numeric', month: 'long', day: 'numeric' }; setPostDate(date.toLocaleDateString(undefined, options)); } else { setPostDate(strings.blankPostDatePlaceholder); } if (postData.category) { setPostCategory(postData.category); } else if (postData.message) { setPostCategory(postData.message); } else { setPostCategory(strings.errorsSuperGeneric); } } console.log(strings.logsOnSlugNavigate, 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(strings.errorsUnknownError); throw new Error(strings.errorsUnknownError); } } } 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(strings.errorsSuperGeneric); } } setMarkdown(text); setLoading(false); } } catch (error) { console.error(strings.errorsFetchPostErr, ':', error); setError(strings.errorsFetchPostErr); setMarkdown(strings.errorsFetchPostErrMd); } })(); }, [slug]); if (loading) { return (
{strings.postPublishedOnLabel} {postDate}
,
pre: ({...props}) => ,
table: ({...props}) =>