import { useQuery } from "@tanstack/react-query"; import { useParams, Link, useLocation, useSearch } from "wouter"; import { type Article } from "@shared/schema"; import { format } from "date-fns"; import { de } from "date-fns/locale"; import { usePageMeta } from "@/hooks/use-page-meta"; import { Eye, ArrowLeft, ChevronLeft, ChevronRight } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Skeleton } from "@/components/ui/skeleton"; import Header from "@/components/header"; import Footer from "@/components/footer"; import { ArticleCardAd } from "@/components/adsense"; import { useEffect } from "react"; interface PaginatedResponse { articles: Article[]; total: number; page: number; totalPages: number; hasMore: boolean; } export default function CategoryPage() { const { category } = useParams<{ category: string }>(); const searchString = useSearch(); const [, setLocation] = useLocation(); const searchParams = new URLSearchParams(searchString); const currentPage = Math.max(1, parseInt(searchParams.get("page") || "1")); usePageMeta( `${category}${currentPage > 1 ? ` – Seite ${currentPage}` : ""} - Volksmusik & Schlager`, `Aktuelle ${category}-Beiträge aus der Volksmusik- und Schlagerszene bei FOLX TV.` ); const { data, isLoading } = useQuery({ queryKey: ["/api/articles/category", category, { page: currentPage }], queryFn: async () => { const res = await fetch(`/api/articles/category/${category}?page=${currentPage}&limit=12`); if (!res.ok) throw new Error("Failed to fetch articles"); return res.json(); }, }); useEffect(() => { window.scrollTo({ top: 0, behavior: "smooth" }); }, [currentPage]); const goToPage = (page: number) => { if (page === 1) { setLocation(`/category/${category}`); } else { setLocation(`/category/${category}?page=${page}`); } }; const renderPagination = () => { if (!data || data.totalPages <= 1) return null; const pages: (number | string)[] = []; const total = data.totalPages; const current = data.page; pages.push(1); if (current > 3) pages.push("..."); for (let i = Math.max(2, current - 1); i <= Math.min(total - 1, current + 1); i++) { pages.push(i); } if (current < total - 2) pages.push("..."); if (total > 1) pages.push(total); return ( ); }; return (

{category}

{data && data.total > 0 && ( {data.total} Beiträge {data.totalPages > 1 && ` · Seite ${data.page} von ${data.totalPages}`} )}
{isLoading ? (
{Array.from({ length: 6 }).map((_, i) => (
))}
) : data && data.articles.length > 0 ? ( <>
{data.articles.flatMap((article, index) => { const items = [
{article.title}
{article.author} · {format(new Date(article.publishedAt), "d. MMMM yyyy", { locale: de })}

{article.title}

{article.excerpt}

{article.views.toLocaleString()}
, ]; if ((index + 1) % 3 === 0) { items.push(); } return items; })}
{renderPagination()} ) : (

Keine Artikel in dieser Kategorie gefunden.

)}
); }