diff --git a/src/components/custom/pagination.tsx b/src/components/custom/pagination.tsx
index 5eeaaa4..345a00d 100644
--- a/src/components/custom/pagination.tsx
+++ b/src/components/custom/pagination.tsx
@@ -1,5 +1,6 @@
'use client';
+import { useEffect } from 'react';
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
import { Pagination, PaginationContent, PaginationItem, PaginationPrevious, PaginationLink, PaginationNext } from '../ui/pagination';
@@ -7,9 +8,25 @@ export default function CustomPagination({ totalPages }: { totalPages: number })
const pathname = usePathname();
const searchParams = useSearchParams();
const router = useRouter();
- const currentPage = Number(searchParams.get('page')) || 1;
- const createPageURL = (pageNumber: number | string) => {
+ const safeTotalPages = Math.max(totalPages, 1);
+ const rawPage = Number(searchParams.get('page')) || 1;
+ const currentPage = Math.min(Math.max(rawPage, 1), safeTotalPages);
+
+ // Keep the URL in sync with the clamped page so a stale/out-of-range
+ // ?page value never lingers after the invalid state is displayed once.
+ useEffect(() => {
+ if (rawPage === currentPage) return;
+ const params = new URLSearchParams(searchParams);
+ params.set('page', currentPage.toString());
+ router.replace(`${pathname}?${params.toString()}`);
+ }, [rawPage, currentPage, pathname, router, searchParams]);
+
+ const isFirstPage = currentPage <= 1;
+ const isLastPage = currentPage >= safeTotalPages;
+
+ const createPageURL = (pageNumber: number) => {
+ if (pageNumber < 1 || pageNumber > safeTotalPages) return;
const params = new URLSearchParams(searchParams);
params.set('page', pageNumber.toString());
router.push(`${pathname}?${params.toString()}`);
@@ -21,8 +38,8 @@ export default function CustomPagination({ totalPages }: { totalPages: number })
{/* Previous Button */}
createPageURL(currentPage - 1)}
/>
@@ -39,8 +56,8 @@ export default function CustomPagination({ totalPages }: { totalPages: number })
{/* Next Button */}
createPageURL(currentPage + 1)}
/>
diff --git a/src/components/ui/pagination.tsx b/src/components/ui/pagination.tsx
index 03e6386..11fd50e 100644
--- a/src/components/ui/pagination.tsx
+++ b/src/components/ui/pagination.tsx
@@ -36,24 +36,37 @@ PaginationItem.displayName = "PaginationItem"
type PaginationLinkProps = {
isActive?: boolean
+ disabled?: boolean
} & Pick &
React.ComponentProps<"a">
const PaginationLink = ({
className,
isActive,
+ disabled,
size = "icon",
+ onClick,
...props
}: PaginationLinkProps) => (
{
+ if (disabled) {
+ event.preventDefault()
+ return
+ }
+ onClick?.(event)
+ }}
{...props}
/>
)