"use client"; import { useState, useEffect } from "react"; import Image from "next/image"; import { getListArticle } from "@/service/article"; import Link from "next/link"; import { usePathname } from "next/navigation"; import { getAdvertise } from "@/service/advertisement"; type Article = { id: number; title: string; description: string; categoryName: string; createdAt: string; createdByName: string; thumbnailUrl: string; categories: { title: string }[]; files: { fileUrl: string; file_alt: string }[]; }; type Advertise = { id: number; title: string; description: string; placement: string; contentFileUrl: string; redirectLink: string; }; export default function LatestNews() { const [articles, setArticles] = useState([]); const [showData, setShowData] = useState("5"); const pathname = usePathname(); const currentSlug = pathname.split("/")[2] || ""; 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 || [1]; // filter iklan dengan placement = "banner" const banner = data.find((ad) => ad.placement === "jumbotron"); if (banner) { setBannerAd(banner); } } catch (err) { console.error("Error fetching advertisement:", err); } } useEffect(() => { fetchArticles(); }, []); async function fetchArticles() { try { const req = { limit: showData, 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); } } const mainArticle = articles[0]; const secondArticle = articles[1]; return (

{" "} {currentSlug ? ` ${currentSlug}` : "Berita Terkini"}

Kolom ini berisi berita-berita yang saat ini sedang menjadi sorotan atau terkait dengan peristiwa terbaru.

{articles.slice(0, 5).map((article, index) => ( {/* Gambar */}
{article.title}
{/* Konten */}
BERITA TERKINI

{article.title}

by {article.createdByName} • {formatDate(article.createdAt)} • 💬 0

{article.description}

))}
{/* KANAN: Advertisement */}

Smart & Responsive

ADVERTISEMENT

{bannerAd ? (
{bannerAd.title
) : ( Berita Utama )}
); } function formatDate(dateStr: string): string { const options: Intl.DateTimeFormatOptions = { year: "numeric", month: "long", day: "numeric", }; return new Date(dateStr).toLocaleDateString("id-ID", options); }