"use client"; import { useEffect, useState } from "react"; import Image from "next/image"; import Link from "next/link"; import { getListArticle } from "@/service/article"; type Article = { id: number; title: string; description: string; categoryName: string; createdAt: string; slug: string; createdByName: string; customCreatorName?: string; thumbnailUrl?: string; categories?: { title: string }[]; }; export default function NewsTerkini() { const [articles, setArticles] = useState([]); const [popular, setPopular] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { loadData(); }, []); async function loadData() { setLoading(true); try { const res = await getListArticle({ limit: "20", page: 1, search: "", isPublish: true, sort: "desc", sortBy: "created_at", }); const data = res?.data?.data || []; setArticles(data.slice(0, 5)); setPopular(data.slice(0, 5)); // 5 teratas sebagai "TERBANYAK DIBAGIKAN" } catch (err) { console.log(err); } setLoading(false); } const formatDate = (d: string) => new Date(d).toLocaleDateString("id-ID", { day: "numeric", month: "long", year: "numeric", }); if (loading) return (

Memuat berita terbaru...

); return (

BERITA TERKINI

{articles.map((item) => (
{/* CATEGORY */}

{item.categoryName || "Kategori"}

{/* JUDUL */}

{item.title}

{/* DESKRIPSI */}

{item.description}

{/* AUTHOR + DATE */}

By {item.customCreatorName || item.createdByName} —{" "} {formatDate(item.createdAt)}

{item.title}
))}
{/* LOAD MORE */}
LOAD MORE ↓
{/* ========================== */} {/* KOLOM KANAN - POPULER */} {/* ========================== */}

TERBANYAK DIBAGIKAN

{popular.map((item, index) => ( {/* NOMOR */}
{(index + 1).toString().padStart(2, "0")}

{item.categories?.[0]?.title || "Kategori"}

{item.title}

{formatDate(item.createdAt)}

{/* THUMBNAIL KECIL */}
{item.title}
))}
{/* ========================== */} {/* BANNER KANAN PPS */} {/* ========================== */}
Kolom PPS
Kolom PPS
); }