From 8f2f8f697a512c36a977522e365dbcbaa3e77d5e Mon Sep 17 00:00:00 2001 From: Yash Raj Date: Sun, 19 Oct 2025 13:08:25 +0530 Subject: [PATCH] Delta function instead of separate button management refactor `pagination.tsx` by: - adding a cleaner delta logic suggested by @max-programming - making sure the state resets after page is loaded --- .../(public)/repos/_components/pagination.tsx | 67 +++++++++++++++---- 1 file changed, 53 insertions(+), 14 deletions(-) diff --git a/src/app/(public)/repos/_components/pagination.tsx b/src/app/(public)/repos/_components/pagination.tsx index 1329d7d..5d105a0 100644 --- a/src/app/(public)/repos/_components/pagination.tsx +++ b/src/app/(public)/repos/_components/pagination.tsx @@ -1,6 +1,9 @@ +'use client'; + import { Button } from '@/app/(public)/_components/button'; -import { ArrowLeft, ArrowRight } from 'lucide-react'; -import Link from 'next/link'; +import { ArrowLeft, ArrowRight, Loader2 } from 'lucide-react'; +import { useRouter } from 'next/navigation'; +import { useTransition } from 'react'; import type { SearchParams } from '@/types'; const MAX_PER_PAGE = 21; @@ -15,24 +18,60 @@ export function Pagination({ totalCount, searchParams }: PaginationProps) { + const router = useRouter(); + const [isPending, startTransition] = useTransition(); + + function changePage(delta: number) { + const params = new URLSearchParams( + Object.entries(searchParams).map(([k, v]) => [k, String(v)]) + ); + params.set('p', String(page + delta)); + + startTransition(() => { + router.push(`?${params.toString()}`); + }); + } + return (
{page > 1 && ( - - - + )} {totalCount >= MAX_PER_PAGE && page < Math.ceil(totalCount / MAX_PER_PAGE) && ( - - - + )}
);