"use client"; import { useEffect, useState } from "react"; import Image from "next/image"; import { getListArticle } from "@/service/article"; type Article = { id: number; title: string; description: string; createdAt: string; createdByName: string; thumbnailUrl: string; categories: { title: string }[]; }; export default function HeaderNews() { const [articles, setArticles] = useState([]); useEffect(() => { fetchArticles(); }, []); async function fetchArticles() { try { const req = { limit: "4", page: 1, search: "", categorySlug: "", sort: "desc", isPublish: true, sortBy: "created_at", }; const res = await getListArticle(req); setArticles(res?.data?.data || []); } catch (error) { console.error("Gagal memuat artikel:", error); } } if (articles.length === 0) { return

Belum ada artikel.

; } return (
{/* Kiri - berita utama */}
{articles[0]?.title
{articles[0]?.categories?.[0]?.title || "Uncategorized"}

{articles[0]?.title}

{articles[0]?.createdByName} {" "} •{" "} {articles[0]?.createdAt && new Date(articles[0].createdAt).toLocaleDateString("id-ID", { day: "numeric", month: "long", year: "numeric", })}

{/* Kanan - 3 berita */}
{/* Atas: 2 berita kecil */}
{[articles[1], articles[2]].map( (item, idx) => item && (
{item.title
{item.categories?.[0]?.title || "Uncategorized"}

{item.title}

{item.createdByName} {" "} •{" "} {item.createdAt && new Date(item.createdAt).toLocaleDateString("id-ID", { day: "numeric", month: "short", year: "numeric", })}

) )}
{/* Bawah: 1 berita besar */} {articles[3] && (
{articles[3]?.title
{articles[3]?.categories?.[0]?.title || "Uncategorized"}

{articles[3]?.title}

{articles[3]?.createdByName} {" "} •{" "} {articles[3]?.createdAt && new Date(articles[3].createdAt).toLocaleDateString( "id-ID", { day: "numeric", month: "long", year: "numeric", } )}

)}
{/* Banner bawah */}
Kolom PPS
); }