"use client"; import { useEffect, useState } from "react"; import Image from "next/image"; import Link from "next/link"; import { getListArticle } from "@/service/article"; import { title } from "process"; import { usePathname } from "next/navigation"; type Article = { id: number; title: string; description: string; categoryName: string; slug: string; createdAt: string; createdByName: string; thumbnailUrl: string; categories: { title: string }[]; files: { fileUrl: string; file_alt: string }[]; }; export default function HeaderLatest() { const [articles, setArticles] = useState([]); const [page, setPage] = useState(1); const [totalPage, setTotalPage] = useState(1); const [showData, setShowData] = useState("5"); const [search, setSearch] = useState(""); const [selectedCategories, setSelectedCategories] = useState([]); const [startDateValue, setStartDateValue] = useState({ startDate: null, endDate: null, }); const pathname = usePathname(); // mapping route ke judul const titles: Record = { "/category/latest-news": "Berita Terkini", "/category/popular-news": "Berita Populer", "/category/peace-indonesia": "Damai Indonesiaku", "/category/protect": "Jaga Negeri", "/category/opinion-news": "Berita Opini", }; // fallback kalau route tidak ada di mapping const title = titles[pathname] || "Berita"; useEffect(() => { initState(); }, [page, showData, startDateValue, selectedCategories]); async function initState() { const req = { limit: showData, page, search, categorySlug: Array.from(selectedCategories).join(","), sort: "desc", isPublish: true, sortBy: "created_at", }; try { const res = await getListArticle(req); setArticles(res?.data?.data || []); setTotalPage(res?.data?.meta?.totalPage || 1); } catch (error) { console.error("Gagal memuat artikel:", error); } } if (articles.length === 0) return null; const featuredArticle = articles[0]; const recentPosts = articles.slice(1, 5); // Ambil 4 artikel berikutnya return (

{title}

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

{articles.map((article) => (
{article.title}

{article.categories?.[0]?.title || article.categoryName || "BERITA"}

{article.title}

{article.description}

By {article.createdByName} —{" "} {new Date(article.createdAt).toLocaleDateString("en-US", { month: "long", day: "numeric", year: "numeric", })}

READ MORE >
))}
{Array.from({ length: totalPage }).map((_, i) => ( ))}

Recent Posts

{recentPosts.map((post, index) => (
{post.title}

{post.categories?.[0]?.title || post.categoryName || "BERITA"}

{post.title}

{new Date(post.createdAt).toLocaleDateString("en-US", { month: "long", day: "numeric", year: "numeric", })}

{String(index + 1).padStart(2, "0")}
))}
Berita Utama

Subscribe us to get the latest news!

); }