Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions src/components/custom/pagination.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
'use client';

import { useEffect } from 'react';
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
import { Pagination, PaginationContent, PaginationItem, PaginationPrevious, PaginationLink, PaginationNext } from '../ui/pagination';

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;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use ?? instead of || to make sure rawPage can keep its original 0 value, so the URL param can be changed and updated to 1 (currently ?page=0 is allowed in the URL)

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()}`);
Expand All @@ -21,8 +38,8 @@ export default function CustomPagination({ totalPages }: { totalPages: number })
{/* Previous Button */}
<PaginationItem>
<PaginationPrevious
aria-disabled={currentPage === 1}
className='text-xs text-foreground aria-disabled:text-muted-foreground aria-disabled:cursor-not-allowed cursor-pointer'
disabled={isFirstPage}
className='text-xs text-foreground aria-disabled:text-muted-foreground cursor-pointer'
onClick={() => createPageURL(currentPage - 1)}
/>
</PaginationItem>
Expand All @@ -39,8 +56,8 @@ export default function CustomPagination({ totalPages }: { totalPages: number })
{/* Next Button */}
<PaginationItem>
<PaginationNext
aria-disabled={currentPage === totalPages}
className='text-xs text-foreground aria-disabled:text-muted-foreground aria-disabled:cursor-not-allowed cursor-pointer'
disabled={isLastPage}
className='text-xs text-foreground aria-disabled:text-muted-foreground cursor-pointer'
onClick={() => createPageURL(currentPage + 1)}
/>
</PaginationItem>
Expand Down
13 changes: 13 additions & 0 deletions src/components/ui/pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,37 @@ PaginationItem.displayName = "PaginationItem"

type PaginationLinkProps = {
isActive?: boolean
disabled?: boolean
} & Pick<ButtonProps, "size"> &
React.ComponentProps<"a">

const PaginationLink = ({
className,
isActive,
disabled,
size = "icon",
onClick,
...props
}: PaginationLinkProps) => (
<a
aria-current={isActive ? "page" : undefined}
aria-disabled={disabled}
tabIndex={disabled ? -1 : undefined}
className={cn(
buttonVariants({
variant: isActive ? "ghost" : "link",
size,
}),
"aria-disabled:pointer-events-none aria-disabled:opacity-50 aria-disabled:cursor-not-allowed",
className
)}
onClick={(event) => {
if (disabled) {
event.preventDefault()
return
}
onClick?.(event)
}}
{...props}
/>
)
Expand Down