"use client"; import { getListArticle } from "@/service/article"; import { useEffect, useState } from "react"; import Image from "next/image"; import Link from "next/link"; import { usePathname } from "next/navigation"; type Article = { id: number; title: string; description: string; categoryName: string; createdAt: string; createdByName: string; thumbnailUrl: string; categories: { title: string; }[]; files: { file_url: string; file_alt: string; }[]; }; const slugToLabel = (slug: string) => { const mapping: Record = { development: "Pembangunan", health: "Kesehatan", "citizen-news": "Berita Warga", }; return mapping[slug] || slug.charAt(0).toUpperCase() + slug.slice(1); }; export default function HeaderDevelopment() { const [page, setPage] = useState(1); const [totalPage, setTotalPage] = useState(1); const [articles, setArticles] = useState([]); const [showData, setShowData] = useState("5"); const [search, setSearch] = useState(""); const [selectedCategories, setSelectedCategories] = useState(""); const [startDateValue, setStartDateValue] = useState({ startDate: null, endDate: null, }); const pathname = usePathname(); // e.g., "/category/development" const pathSegments = pathname.split("/").filter(Boolean); // ["category", "development"] const categorySlug = pathSegments[1]; // "development" const categoryLabel = slugToLabel(categorySlug); useEffect(() => { initState(); }, [page, showData, startDateValue, selectedCategories]); async function initState() { // loading(); const req = { limit: showData, page, search, categorySlug: Array.from(selectedCategories).join(","), sort: "desc", sortBy: "created_at", }; try { const res = await getListArticle(req); setArticles(res?.data?.data || []); setTotalPage(res?.data?.meta?.totalPage || 1); } finally { // close(); } } return (
Background

Home {" "} {">"}{" "} Category {" "} {">"} {categoryLabel}

Pembangunan

{articles.length > 0 && (
{articles[0].title}
{articles[0].categories?.[0]?.title || "TANPA KATEGORI"}

{articles[0].title}

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

)}
{articles.slice(1, 3).map((article, index) => (
{article.title}
{article.categoryName || "TANPA KATEGORI"}

{article.title}

))}
); }