"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; slug: string; createdByName: string; customCreatorName: string; thumbnailUrl: string; categories: { title: string; }[]; files: { fileUrl: 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 HeaderCitizen() { const [page, setPage] = useState(1); const [totalPage, setTotalPage] = useState(1); const [articles, setArticles] = useState([]); const [showData, setShowData] = useState("100"); const [search, setSearch] = useState(""); const [selectedCategories, setSelectedCategories] = useState(""); const [startDateValue, setStartDateValue] = useState({ startDate: null, endDate: null, }); const pathname = usePathname(); const pathSegments = pathname.split("/").filter(Boolean); const categorySlug = pathSegments[1]; 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", isPublish: true, sortBy: "created_at", }; try { const res = await getListArticle(req); setArticles(res?.data?.data || []); setTotalPage(res?.data?.meta?.totalPage || 1); } finally { // close(); } } const citizenArticles = articles.filter((article) => article.categories?.some((category) => category.title?.toLowerCase().includes("berita warga") ) ); const mainArticle = citizenArticles[0]; const otherArticles = citizenArticles.slice(1, 3); return (

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

Berita Warga

{mainArticle && (
{mainArticle.title}
{mainArticle.categories?.[0]?.title || "TANPA KATEGORI"}

{mainArticle.title}

{mainArticle?.customCreatorName || mainArticle.createdByName}{" "} -{" "} {new Date(mainArticle.createdAt).toLocaleDateString( "id-ID", { day: "numeric", month: "long", year: "numeric", } )}

)}
{otherArticles.map((article, index) => (
{article.title}
{article.categories?.[0]?.title || "TANPA KATEGORI"}

{article.title}

))}
); }