'use client'; import { useEffect, useState } from 'react'; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card" import { Badge } from "@/components/ui/badge" import Link from "next/link" import strings from "@/strings.json" import { formatDistanceToNow, format } from 'date-fns' type Post = { id: number; title: string; description: string; category: string; date: number; slug: string; }; export default function Home() { const [posts, setPosts] = useState<{ id: number; title: string; description: string; category: string; date: number; slug: string; }[]>([]); const [error, setError] = useState(''); const [loading, setLoading] = useState(true); useEffect(() => { console.log(strings.logsFetchPostList); (async () => { try { const res = await fetch(`http://localhost:3001/api/posts/fetchList`, { method: 'GET', }); if (!res.ok) { throw new Error(`${strings.errorsFetchPostListFailCtx} ${res.status}`); } const data = await res.json(); if (data.success === false) { if (data.message) { throw new Error(data.message); } else { throw new Error(strings.errorsUnknownError); } } else { const sortedPosts: Post[] = data.posts.sort((a: Post, b: Post) => b.date - a.date); setPosts(sortedPosts); } } catch (error) { console.error(strings.errorsFetchPostListErrCtx, error); setError(strings.errorsFetchPostListErr); } finally { setLoading(false); } })(); }, []); const formatDate = (timestamp: number): string => { const date = new Date(timestamp * 1000); const now = new Date(); if (date.getFullYear() !== now.getFullYear()) { return format(date, 'd MMMM, yyyy'); } else { return formatDistanceToNow(date, { addSuffix: true }); } }; return (

{strings.latestPostsHeader}

{loading ? (
) : error ? (
{error}
) : (
{posts.map((post) => (
{post.title} {post.category}
{post.description}

{strings.recentPostsPublishedOnLabel} {formatDate(post.date)}

{strings.recentPostsReadMoreFromLinkText}
))}
)}
) }