Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

refactor: replace Pagination component with TablePagination and update pagination logic in Pages component

Bill Yang31950c1d6e24
diff --git a/client/src/app/[site]/pages/components/Pagination.tsx b/client/src/app/[site]/pages/components/Pagination.tsx
deleted file mode 100644
index 14a182aea..000000000
--- a/client/src/app/[site]/pages/components/Pagination.tsx
+++ /dev/null
@@ -1,152 +0,0 @@
-"use client";
-
-import { Button } from "@/components/ui/button";
-import {
- ChevronLeft,
- ChevronRight,
- ChevronsLeft,
- ChevronsRight,
-} from "lucide-react";
-
-type PaginationProps = {
- currentPage: number;
- totalPages: number;
- onPageChange: (page: number) => void;
-};
-
-export function Pagination({
- currentPage,
- totalPages,
- onPageChange,
-}: PaginationProps) {
- const isPrevDisabled = currentPage === 1;
- const isNextDisabled = currentPage === totalPages || totalPages === 0;
-
- // Calculate which page numbers to show
- const getPageNumbers = () => {
- const pages = [];
- const maxVisiblePages = 5;
-
- if (totalPages <= maxVisiblePages) {
- // Show all pages if there are fewer than maxVisiblePages
- for (let i = 1; i <= totalPages; i++) {
- pages.push(i);
- }
- } else {
- // Always show the first page
- pages.push(1);
-
- // Logic for showing pages around the current page
- let startPage = Math.max(2, currentPage - 1);
- let endPage = Math.min(totalPages - 1, currentPage + 1);
-
- // Adjust if at the beginning
- if (currentPage <= 3) {
- endPage = Math.min(totalPages - 1, 4);
- }
-
- // Adjust if at the end
- if (currentPage >= totalPages - 2) {
- startPage = Math.max(2, totalPages - 3);
- }
-
- // Show ellipsis if needed before startPage
- if (startPage > 2) {
- pages.push(-1); // -1 indicates ellipsis
- }
-
- // Add middle pages
- for (let i = startPage; i <= endPage; i++) {
- pages.push(i);
- }
-
- // Show ellipsis if needed after endPage
- if (endPage < totalPages - 1) {
- pages.push(-2); // -2 indicates ellipsis
- }
-
- // Always show the last page
- pages.push(totalPages);
- }
-
- return pages;
- };
-
- const pageNumbers = getPageNumbers();
-
- return (
- <div className="flex justify-center items-center gap-1 my-6">
- {/* First page button */}
- <Button
- variant="outline"
- size="icon"
- onClick={() => onPageChange(1)}
- disabled={isPrevDisabled}
- className="h-8 w-8"
- >
- <ChevronsLeft className="h-4 w-4" />
- </Button>
-
- {/* Previous page button */}
- <Button
- variant="outline"
- size="icon"
- onClick={() => onPageChange(currentPage - 1)}
- disabled={isPrevDisabled}
- className="h-8 w-8"
- >
- <ChevronLeft className="h-4 w-4" />
- </Button>
-
- {/* Page numbers */}
- {pageNumbers.map((pageNum, index) => {
- if (pageNum < 0) {
- // Render ellipsis
- return (
- <span
- key={`ellipsis-${index}`}
- className="px-2 text-muted-foreground"
- >
- &hellip;
- </span>
- );
- }
-
- return (
- <Button
- key={pageNum}
- variant={currentPage === pageNum ? "default" : "outline"}
- size="sm"
- onClick={() => onPageChange(pageNum)}
- className="h-8 w-8"
- disabled={totalPages === 0}
- >
- {pageNum}
- </Button>
- );
- })}
-
- {/* Next page button */}
- <Button
- variant="outline"
- size="icon"
- onClick={() => onPageChange(currentPage + 1)}
- disabled={isNextDisabled}
- className="h-8 w-8"
- >
- <ChevronRight className="h-4 w-4" />
- </Button>
-
- {/* Last page button */}
- <Button
- variant="outline"
- size="icon"
- onClick={() => onPageChange(totalPages)}
- disabled={isNextDisabled}
- className="h-8 w-8"
- >
- <ChevronsRight className="h-4 w-4" />
- </Button>
- </div>
- );
-}
diff --git a/client/src/app/[site]/pages/page.tsx b/client/src/app/[site]/pages/page.tsx
index 76edd3763..6e4f30093 100644
--- a/client/src/app/[site]/pages/page.tsx
+++ b/client/src/app/[site]/pages/page.tsx
@@ -10,7 +10,8 @@ import { useEffect, useState } from "react";
import { SubHeader } from "../components/SubHeader/SubHeader";
import { PageListItem } from "./components/PageListItem";
import { PageListSkeleton } from "./components/PageListSkeleton";
-import { Pagination } from "./components/Pagination";
+import { TablePagination } from "@/components/pagination";
+import { createColumnHelper } from "@tanstack/react-table";
// Number of items per page
const PAGE_SIZE = 10;
@@ -18,7 +19,10 @@ const PAGE_SIZE = 10;
export default function Pages() {
const { site } = useStore();
- const [currentPage, setCurrentPage] = useState(1);
+ const [pagination, setPagination] = useState({
+ pageIndex: 0,
+ pageSize: PAGE_SIZE,
+ });
const [totalPages, setTotalPages] = useState(0);
useSetPageTitle("Rybbit · Pages");
@@ -28,7 +32,7 @@ export default function Pages() {
}
// Calculate offset based on current page
- const offset = (currentPage - 1) * PAGE_SIZE;
+ const offset = pagination.pageIndex * pagination.pageSize;
const {
data: apiResponse,
@@ -38,7 +42,7 @@ export default function Pages() {
isPlaceholderData,
isFetching,
} = useGetPageTitlesPaginated({
- limit: PAGE_SIZE,
+ limit: pagination.pageSize,
offset: offset,
});
@@ -47,10 +51,32 @@ export default function Pages() {
const isLoading = isLoadingPages || isFetching;
+ // Create a minimal table object with the required pagination methods
+ const table = {
+ getState: () => ({
+ pagination: {
+ pageIndex: pagination.pageIndex,
+ pageSize: pagination.pageSize,
+ },
+ }),
+ getCanPreviousPage: () => pagination.pageIndex > 0,
+ getCanNextPage: () => {
+ if (totalCount === undefined) return true;
+ return (pagination.pageIndex + 1) * pagination.pageSize < totalCount;
+ },
+ getPageCount: () => totalPages,
+ setPageIndex: (index: number) =>
+ setPagination({ ...pagination, pageIndex: index }),
+ previousPage: () =>
+ setPagination({ ...pagination, pageIndex: pagination.pageIndex - 1 }),
+ nextPage: () =>
+ setPagination({ ...pagination, pageIndex: pagination.pageIndex + 1 }),
+ };
+
useEffect(() => {
if (!isLoadingPages && !isPlaceholderData && !isFetching) {
if (totalCount !== undefined) {
- setTotalPages(Math.ceil(totalCount / PAGE_SIZE));
+ setTotalPages(Math.ceil(totalCount / pagination.pageSize));
} else if (
pagesDataArray &&
pagesDataArray.length === 0 &&
@@ -58,14 +84,18 @@ export default function Pages() {
) {
// Fallback if totalCount is somehow undefined but we have an empty array on page 1
setTotalPages(0);
- } else if (pagesDataArray && pagesDataArray.length < PAGE_SIZE) {
+ } else if (
+ pagesDataArray &&
+ pagesDataArray.length < pagination.pageSize
+ ) {
// Fallback: if less than a full page is returned, assume it's the last
- setTotalPages(currentPage);
+ setTotalPages(pagination.pageIndex + 1);
}
- // If totalCount is undefined and we have a full page, we can't be sure of totalPages
- // In this scenario, the UI might show a next button that leads to an empty page if it was the last one.
- // This is a limitation if totalCount is not reliably provided.
- } else if (currentPage === 1 && isLoadingPages && !pagesDataArray) {
+ } else if (
+ pagination.pageIndex === 0 &&
+ isLoadingPages &&
+ !pagesDataArray
+ ) {
// Initial load, no data yet
setTotalPages(0);
}
@@ -76,28 +106,17 @@ export default function Pages() {
isLoadingPages,
isPlaceholderData,
isFetching,
- currentPage,
+ pagination.pageIndex,
+ pagination.pageSize,
offset,
]);
- const handlePageChange = (page: number) => {
- if (page === currentPage || page < 1) return;
- // With totalCount, we can be more direct.
- // Allow navigating to any page up to totalPages if totalPages is known and > 0.
- // If totalPages is 0 (e.g. initial load or no data), only allow navigating to page 1.
- if (totalPages > 0 && page > totalPages) return;
- if (totalPages === 0 && page > 1) return;
-
- setCurrentPage(page);
- window.scrollTo({ top: 0, behavior: "smooth" });
- };
-
return (
<div className="p-2 md:p-4 max-w-[1100px] mx-auto space-y-3">
<SubHeader />
{isLoading ? (
- <PageListSkeleton count={PAGE_SIZE} />
+ <PageListSkeleton count={pagination.pageSize} />
) : isErrorPages ? (
<div className="text-center p-8 text-destructive">
<p>Error loading pages data</p>
@@ -107,7 +126,7 @@ export default function Pages() {
<>
{pagesDataArray.map((pageItem: PageTitleItem, index: number) => (
<PageListItem
- key={`${pageItem.value}-${index}-${currentPage}`}
+ key={`${pageItem.value}-${index}-${pagination.pageIndex}`}
pageData={{
value: pageItem.pathname,
title: pageItem.value,
@@ -117,10 +136,13 @@ export default function Pages() {
/>
))}
{totalPages > 0 && (
- <Pagination
- currentPage={currentPage}
- totalPages={totalPages}
- onPageChange={handlePageChange}
+ <TablePagination
+ table={table}
+ data={{ items: pagesDataArray || [], total: totalCount || 0 }}
+ pagination={pagination}
+ setPagination={setPagination}
+ isLoading={isLoading}
+ itemName="pages"
/>
)}
</>
diff --git a/client/src/components/admin/UserFilters.tsx b/client/src/app/admin/components/UserFilters.tsx
similarity index 100%
rename from client/src/components/admin/UserFilters.tsx
rename to client/src/app/admin/components/UserFilters.tsx
diff --git a/client/src/app/admin/components/UserTablePagination.tsx b/client/src/app/admin/components/UserTablePagination.tsx
new file mode 100644
index 000000000..30e08b4dc
--- /dev/null
+++ b/client/src/app/admin/components/UserTablePagination.tsx
@@ -0,0 +1,32 @@
+"use client";
+
+import { Table } from "@tanstack/react-table";
+import { AdminUser } from "@/types/admin";
+import { TablePagination } from "@/components/pagination";
+
+interface UserPaginationProps {
+ table: Table<AdminUser>;
+ data: { users: AdminUser[]; total: number } | undefined;
+ pagination: { pageIndex: number; pageSize: number };
+ setPagination: (value: { pageIndex: number; pageSize: number }) => void;
+ isLoading: boolean;
+}
+
+export function UserTablePagination({
+ table,
+ data,
+ pagination,
+ setPagination,
+ isLoading,
+}: UserPaginationProps) {
+ return (
+ <TablePagination
+ table={table}
+ data={data ? { items: data.users, total: data.total } : undefined}
+ pagination={pagination}
+ setPagination={setPagination}
+ isLoading={isLoading}
+ itemName="users"
+ />
+ );
+}
diff --git a/client/src/components/admin/UserTableSkeleton.tsx b/client/src/app/admin/components/UserTableSkeleton.tsx
similarity index 100%
rename from client/src/components/admin/UserTableSkeleton.tsx
rename to client/src/app/admin/components/UserTableSkeleton.tsx
diff --git a/client/src/components/admin/UsersTable.tsx b/client/src/app/admin/components/UsersTable.tsx
similarity index 100%
rename from client/src/components/admin/UsersTable.tsx
rename to client/src/app/admin/components/UsersTable.tsx
diff --git a/client/src/app/admin/page.tsx b/client/src/app/admin/page.tsx
index f541f34e7..daeab1782 100644
--- a/client/src/app/admin/page.tsx
+++ b/client/src/app/admin/page.tsx
@@ -7,9 +7,9 @@ import { AlertCircle } from "lucide-react";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { StandardPage } from "@/components/StandardPage";
import { useAdminUsers } from "@/hooks/useAdminUsers";
-import { UsersTable } from "@/components/admin/UsersTable";
-import { UserFilters } from "@/components/admin/UserFilters";
-import { UserTablePagination } from "@/components/admin/UserTablePagination";
+import { UsersTable } from "@/app/admin/components/UsersTable";
+import { UserFilters } from "@/app/admin/components/UserFilters";
+import { UserTablePagination } from "@/app/admin/components/UserTablePagination";
import { authClient } from "@/lib/auth";
export default function AdminPage() {
diff --git a/client/src/components/admin/UserTablePagination.tsx b/client/src/components/pagination.tsx
similarity index 81%
rename from client/src/components/admin/UserTablePagination.tsx
rename to client/src/components/pagination.tsx
index 45b073ca4..dea12a3ba 100644
--- a/client/src/components/admin/UserTablePagination.tsx
+++ b/client/src/components/pagination.tsx
@@ -1,7 +1,6 @@
"use client";
import { Table } from "@tanstack/react-table";
-import { AdminUser } from "@/types/admin";
import { Button } from "@/components/ui/button";
import {
Select,
@@ -17,44 +16,58 @@ import {
ChevronsRight,
} from "lucide-react";
-interface PaginationProps {
- table: Table<AdminUser>;
- data: { users: AdminUser[]; total: number } | undefined;
+// Simplified pagination controller interface
+interface PaginationController {
+ getState: () => { pagination: { pageIndex: number; pageSize: number } };
+ getCanPreviousPage: () => boolean;
+ getCanNextPage: () => boolean;
+ getPageCount: () => number;
+ setPageIndex: (index: number) => void;
+ previousPage: () => void;
+ nextPage: () => void;
+}
+
+interface PaginationProps<TData> {
+ table: Table<TData> | PaginationController;
+ data: { items: TData[]; total: number } | undefined;
pagination: { pageIndex: number; pageSize: number };
setPagination: (value: { pageIndex: number; pageSize: number }) => void;
isLoading: boolean;
+ itemName?: string;
}
-export function UserTablePagination({
+export function TablePagination<TData>({
table,
data,
pagination,
setPagination,
isLoading,
-}: PaginationProps) {
+ itemName = "items",
+}: PaginationProps<TData>) {
return (
<div className="flex items-center justify-between">
<div className="text-sm text-neutral-400">
{isLoading ? (
- <span>Loading users...</span>
+ <span>Loading {itemName}...</span>
) : (
<>
Showing{" "}
<span className="font-semibold">
- {data?.users?.length
+ {data?.items?.length
? pagination.pageIndex * pagination.pageSize + 1
: 0}
</span>{" "}
to{" "}
<span className="font-semibold">
- {data?.users?.length
+ {data?.items?.length
? Math.min(
(pagination.pageIndex + 1) * pagination.pageSize,
data?.total || 0
)
: 0}
</span>{" "}
- of <span className="font-semibold">{data?.total || 0}</span> users
+ of <span className="font-semibold">{data?.total || 0}</span>{" "}
+ {itemName}
</>
)}
</div>
@@ -83,7 +96,7 @@ export function UserTablePagination({
</div>
<Button
variant="outline"
- size="sm"
+ size="icon"
onClick={() => table.setPageIndex(0)}
disabled={!table.getCanPreviousPage() || isLoading}
>
@@ -91,7 +104,7 @@ export function UserTablePagination({
</Button>
<Button
variant="outline"
- size="sm"
+ size="icon"
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage() || isLoading}
>
@@ -115,7 +128,7 @@ export function UserTablePagination({
</div>
<Button
variant="outline"
- size="sm"
+ size="icon"
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage() || isLoading}
>
@@ -123,7 +136,7 @@ export function UserTablePagination({
</Button>
<Button
variant="outline"
- size="sm"
+ size="icon"
onClick={() => table.setPageIndex(table.getPageCount() - 1)}
disabled={!table.getCanNextPage() || isLoading}
>