"use client"; import { useEffect, useRef } from "react"; import { BlogCategoryOption, BlogGridProps } from "./types"; import { usePathname, useRouter, useSearchParams } from "next/navigation"; import { Pagination } from "@shared/components/pagination"; import { Select } from "@shared/components/select"; import { Text } from "@shared/components/text"; import { BlogCard } from "@shared/contentful/blocks/cards/blog-card"; export function BlogGrid({ paginatedArticles, totalArticles, currentPage, totalPages, selectedCategory, categoryOptions, }: BlogGridProps) { const router = useRouter(); const pathname = usePathname(); const searchParams = useSearchParams(); const isInitialMount = useRef(true); useEffect(() => { if (isInitialMount.current) { isInitialMount.current = false; return; } window.scrollTo({ top: 0, behavior: "smooth" }); }, [currentPage]); function updateQueryParams(key: string, value: string) { const params = new URLSearchParams(searchParams.toString()); if (value) { params.set(key, value); } else { params.delete(key); } // Delete page when category changes if (key === "category") { params.delete("page"); } router.push(`${pathname}?${params.toString()}`, { scroll: false }); } function handleCategoryChange(value: unknown) { if (!value || Array.isArray(value)) return; const cat = value as BlogCategoryOption; updateQueryParams("category", cat.value === "all" ? "" : cat.value); } function handlePageChange(_page: number) { // Scroll is handled by the useEffect on currentPage change } function getPageHref(page: number): string { const params = new URLSearchParams(searchParams.toString()); if (page === 1) { params.delete("page"); } else { params.set("page", page.toString()); } const qs = params.toString(); return qs ? `${pathname}?${qs}` : pathname; } return (
{/* Controls bar */}
recent articles
{/* Showing count */} showing showing{" "} {paginatedArticles.length} of {totalArticles} articles {/* Filter dropdown */}