"use client"; import { useEffect, useState } from "react"; import Image from "next/image"; import { getListArticle } from "@/service/article"; import Link from "next/link"; import { getAdvertise } from "@/service/advertisement"; type Article = { id: number; title: string; description: string; createdAt: string; createdByName: string; customCreatorName: string; thumbnailUrl: string; categories: { title: string }[]; }; type Advertise = { id: number; title: string; description: string; placement: string; contentFileUrl: string; redirectLink: string; }; export default function HeaderNews() { const [articles, setArticles] = useState([]); const [bannerAd, setBannerAd] = useState(null); useEffect(() => { initStateAdver(); }, []); async function initStateAdver() { const req = { limit: 100, page: 1, sort: "desc", sortBy: "created_at", isPublish: true, }; try { const res = await getAdvertise(req); const data: Advertise[] = res?.data?.data || []; // filter iklan dengan placement = "banner" const banner = data.find((ad) => ad.placement === "banner"); if (banner) { setBannerAd(banner); } } catch (err) { console.error("Error fetching advertisement:", err); } } 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]?.customCreatorName || 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?.customCreatorName || 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 */}
{bannerAd ? (
{bannerAd.title
) : ( Berita Utama )}
); }