"use client"; import { getListArticle } from "@/service/article"; import Image from "next/image"; import Link from "next/link"; import { useEffect, useState } from "react"; type Article = { id: number; title: string; description: string; categoryName: string; createdAt: string; slug: string; createdByName: string; customCreatorName: string; thumbnailUrl: string; categories: { title: string }[]; files: { fileUrl: string; file_alt: string }[]; }; export default function Header() { const [articles, setArticles] = useState([]); useEffect(() => { const fetchArticles = async () => { try { const req = { limit: "2", page: 1, search: "", categorySlug: "", sort: "desc", isPublish: true, sortBy: "created_at", }; const res = await getListArticle(req); setArticles(res?.data?.data || []); } catch (err) { console.error("Error fetching articles:", err); } }; fetchArticles(); }, []); return (
{/* Header Banner */}
Berita Utama

BERITA UTAMA

{/* Grid Artikel */}
{articles.length > 0 ? ( articles.map((item, index) => (
{item.files?.[0]?.file_alt {/* Overlay gradient */}
{/* Text content */}
{item.categoryName || item.categories?.[0]?.title || "Berita"}

{item.title}

{new Date(item.createdAt).toLocaleDateString("id-ID", { day: "2-digit", month: "long", year: "numeric", })}

)) ) : (

Tidak ada artikel tersedia.

)}
); }