Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

Improvement: Pagination

Ashish Viradiyaebeae52fc564
diff --git a/components/documents/documents-pagination.tsx b/components/documents/documents-pagination.tsx
deleted file mode 100644
index 3f47d787..00000000
--- a/components/documents/documents-pagination.tsx
+++ /dev/null
@@ -1,103 +0,0 @@
-import {
- ChevronLeftIcon,
- ChevronRightIcon,
- ChevronsLeftIcon,
- ChevronsRightIcon,
-} from "lucide-react";
-import { Button } from "@/components/ui/button";
-import {
- Select,
- SelectContent,
- SelectItem,
- SelectTrigger,
- SelectValue,
-} from "@/components/ui/select";
-
-interface DocumentsPaginationProps {
- currentPage: number;
- pageSize: number;
- totalDocuments: number;
- totalPages: number;
- onPageChange: (page: number) => void;
- onPageSizeChange: (size: number) => void;
- totalShownDocuments: number;
-}
-
-export function DocumentsPagination({
- currentPage,
- pageSize,
- totalDocuments,
- totalPages,
- onPageChange,
- onPageSizeChange,
- totalShownDocuments,
-}: DocumentsPaginationProps) {
- return (
- <div className="mt-4 flex items-center justify-between p-4 rounded-lg bottom-0 left-0 right-0 bg-white w-full border border-gray-200 dark:border-gray-800">
- <div className="flex-1 text-sm text-muted-foreground">
- Showing {totalShownDocuments} of {totalDocuments} documents
- </div>
- <div className="flex items-center space-x-6 lg:space-x-8">
- <div className="hidden items-center space-x-2 sm:flex">
- <p className="text-sm font-medium">Items per page</p>
- <Select
- value={`${pageSize}`}
- onValueChange={(value) => onPageSizeChange(Number(value))}
- >
- <SelectTrigger className="h-8 w-[70px]">
- <SelectValue placeholder={pageSize} />
- </SelectTrigger>
- <SelectContent side="top">
- {[10, 20, 30, 40, 50].map((size) => (
- <SelectItem key={size} value={`${size}`}>
- {size}
- </SelectItem>
- ))}
- </SelectContent>
- </Select>
- </div>
- <div className="flex w-[100px] items-center justify-center text-sm font-medium">
- Page {currentPage} of {totalPages || 1}
- </div>
- <div className="flex items-center space-x-2">
- <Button
- variant="outline"
- className="hidden h-8 w-8 p-0 lg:flex"
- onClick={() => onPageChange(1)}
- disabled={currentPage === 1}
- >
- <span className="sr-only">Go to first page</span>
- <ChevronsLeftIcon className="h-4 w-4" />
- </Button>
- <Button
- variant="outline"
- className="h-8 w-8 p-0"
- onClick={() => onPageChange(currentPage - 1)}
- disabled={currentPage === 1}
- >
- <span className="sr-only">Go to previous page</span>
- <ChevronLeftIcon className="h-4 w-4" />
- </Button>
- <Button
- variant="outline"
- className="h-8 w-8 p-0"
- onClick={() => onPageChange(currentPage + 1)}
- disabled={currentPage === totalPages || totalPages === 0}
- >
- <span className="sr-only">Go to next page</span>
- <ChevronRightIcon className="h-4 w-4" />
- </Button>
- <Button
- variant="outline"
- className="hidden h-8 w-8 p-0 lg:flex"
- onClick={() => onPageChange(totalPages)}
- disabled={currentPage === totalPages || totalPages === 0}
- >
- <span className="sr-only">Go to last page</span>
- <ChevronsRightIcon className="h-4 w-4" />
- </Button>
- </div>
- </div>
- </div>
- );
-}
\ No newline at end of file
diff --git a/components/documents/pagination.tsx b/components/documents/pagination.tsx
new file mode 100644
index 00000000..3e1debe2
--- /dev/null
+++ b/components/documents/pagination.tsx
@@ -0,0 +1,110 @@
+import {
+ ChevronLeftIcon,
+ ChevronRightIcon,
+ ChevronsLeftIcon,
+ ChevronsRightIcon,
+} from "lucide-react";
+
+import { Button } from "@/components/ui/button";
+import { Card } from "@/components/ui/card";
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "@/components/ui/select";
+
+interface PaginationProps {
+ currentPage: number;
+ pageSize: number;
+ totalItems: number;
+ totalPages: number;
+ onPageChange: (page: number) => void;
+ onPageSizeChange: (size: number) => void;
+ totalShownItems: number;
+ itemName: string;
+}
+
+export function Pagination({
+ currentPage,
+ pageSize,
+ totalItems,
+ totalPages,
+ onPageChange,
+ onPageSizeChange,
+ totalShownItems,
+ itemName,
+}: PaginationProps) {
+ return (
+ <Card className="mt-4 p-4">
+ <div className="flex items-center justify-between">
+ <div className="flex-1 text-sm text-muted-foreground">
+ Showing <span className="font-bold">{totalShownItems}</span> of{" "}
+ {totalItems} {itemName}
+ </div>
+ <div className="flex items-center space-x-6 lg:space-x-8">
+ <div className="hidden items-center space-x-2 sm:flex">
+ <p className="text-sm font-medium">Items per page</p>
+ <Select
+ value={`${pageSize}`}
+ onValueChange={(value) => onPageSizeChange(Number(value))}
+ >
+ <SelectTrigger className="h-8 w-[70px]">
+ <SelectValue placeholder={pageSize} />
+ </SelectTrigger>
+ <SelectContent side="top">
+ {[10, 20, 30, 40, 50].map((size) => (
+ <SelectItem key={size} value={`${size}`}>
+ {size}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ </div>
+ <div className="flex w-[100px] items-center justify-center text-sm font-medium">
+ Page {currentPage} of {totalPages || 1}
+ </div>
+ <div className="flex items-center space-x-2">
+ <Button
+ variant="outline"
+ className="hidden h-8 w-8 p-0 lg:flex"
+ onClick={() => onPageChange(1)}
+ disabled={currentPage === 1}
+ >
+ <span className="sr-only">Go to first page</span>
+ <ChevronsLeftIcon className="h-4 w-4" />
+ </Button>
+ <Button
+ variant="outline"
+ className="h-8 w-8 p-0"
+ onClick={() => onPageChange(currentPage - 1)}
+ disabled={currentPage === 1}
+ >
+ <span className="sr-only">Go to previous page</span>
+ <ChevronLeftIcon className="h-4 w-4" />
+ </Button>
+ <Button
+ variant="outline"
+ className="h-8 w-8 p-0"
+ onClick={() => onPageChange(currentPage + 1)}
+ disabled={currentPage === totalPages || totalPages === 0}
+ >
+ <span className="sr-only">Go to next page</span>
+ <ChevronRightIcon className="h-4 w-4" />
+ </Button>
+ <Button
+ variant="outline"
+ className="hidden h-8 w-8 p-0 lg:flex"
+ onClick={() => onPageChange(totalPages)}
+ disabled={currentPage === totalPages || totalPages === 0}
+ >
+ <span className="sr-only">Go to last page</span>
+ <ChevronsRightIcon className="h-4 w-4" />
+ </Button>
+ </div>
+ </div>
+ </div>
+ </Card>
+ );
+}
\ No newline at end of file
diff --git a/components/visitors/visitors-table.tsx b/components/visitors/visitors-table.tsx
index 1d90abfd..0196d7dd 100644
--- a/components/visitors/visitors-table.tsx
+++ b/components/visitors/visitors-table.tsx
@@ -43,6 +43,7 @@ import { useDocumentVisits } from "@/lib/swr/use-document";
import { durationFormat, timeAgo } from "@/lib/utils";
import { UpgradePlanModal } from "../billing/upgrade-plan-modal";
+import { Pagination } from "../documents/pagination";
import { Button } from "../ui/button";
import {
DropdownMenu,
@@ -52,14 +53,6 @@ import {
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "../ui/dropdown-menu";
-import {
- Pagination,
- PaginationContent,
- PaginationItem,
- PaginationLink,
- PaginationNext,
- PaginationPrevious,
-} from "../ui/pagination";
import { VisitorAvatar } from "./visitor-avatar";
import VisitorChart from "./visitor-chart";
import VisitorClicks from "./visitor-clicks";
@@ -78,15 +71,23 @@ export default function VisitorsTable({
const teamInfo = useTeam();
const teamId = teamInfo?.currentTeam?.id;
const [currentPage, setCurrentPage] = useState<number>(1);
- const limit = 10; // Set the number of items per page
+ const [pageSize, setPageSize] = useState<number>(10);
- const { views, mutate: mutateViews } = useDocumentVisits(currentPage, limit);
+ const { views, mutate: mutateViews } = useDocumentVisits(
+ currentPage,
+ pageSize,
+ );
const { plan } = usePlan();
const isFreePlan = plan === "free";
const [isLoading, setIsLoading] = useState(false);
const [isRefreshing, setIsRefreshing] = useState(false);
+ const handlePageSizeChange = (newSize: number) => {
+ setPageSize(newSize);
+ setCurrentPage(1);
+ };
+
const handleArchiveView = async (
viewId: string,
targetId: string,
@@ -494,62 +495,25 @@ export default function VisitorsTable({
</TableBody>
</Table>
</div>
- {/* Pagination Controls */}
- <div className="mt-2 flex w-full items-center">
- <div className="w-full text-sm">
- Showing{" "}
- <span className="font-semibold">
- {views?.totalViews && views?.totalViews > 10
- ? 10
- : views?.totalViews}
- </span>{" "}
- of {views?.totalViews} visits
- </div>
- <Pagination className="justify-end">
- <PaginationContent>
- <PaginationItem>
- <PaginationPrevious
- onClick={() => setCurrentPage(currentPage - 1)}
- disabled={currentPage === 1}
- />
- </PaginationItem>
- {currentPage !== 1 ? (
- <PaginationItem>
- <PaginationLink onClick={() => setCurrentPage(1)}>
- {1}
- </PaginationLink>
- </PaginationItem>
- ) : null}
-
- <PaginationItem>
- <PaginationLink isActive>{currentPage}</PaginationLink>
- </PaginationItem>
-
- {views?.totalViews &&
- currentPage !== Math.ceil(views?.totalViews / 10) ? (
- <PaginationItem>
- <PaginationLink
- onClick={() =>
- setCurrentPage(Math.ceil(views?.totalViews / 10))
- }
- >
- {Math.ceil(views?.totalViews / 10)}
- </PaginationLink>
- </PaginationItem>
- ) : null}
- <PaginationItem>
- <PaginationNext
- onClick={() => setCurrentPage(currentPage + 1)}
- disabled={
- views?.totalViews
- ? currentPage === Math.ceil(views?.totalViews / 10)
- : true
- }
- />
- </PaginationItem>
- </PaginationContent>
- </Pagination>
- </div>
+ <Pagination
+ itemName="visits"
+ currentPage={currentPage}
+ pageSize={pageSize}
+ totalItems={views?.totalViews || 0}
+ totalPages={
+ views?.totalViews ? Math.ceil(views.totalViews / pageSize) : 0
+ }
+ onPageChange={setCurrentPage}
+ onPageSizeChange={handlePageSizeChange}
+ totalShownItems={
+ views?.totalViews
+ ? Math.min(
+ pageSize,
+ views.totalViews - (currentPage - 1) * pageSize,
+ )
+ : 0
+ }
+ />
</div>
);
}
diff --git a/pages/documents/index.tsx b/pages/documents/index.tsx
index 4daac459..ee4852f6 100644
--- a/pages/documents/index.tsx
+++ b/pages/documents/index.tsx
@@ -8,8 +8,8 @@ import { handleInvitationStatus } from "@/lib/utils";
import { AddDocumentModal } from "@/components/documents/add-document-modal";
import { DocumentsList } from "@/components/documents/documents-list";
-import { DocumentsPagination } from "@/components/documents/documents-pagination";
import SortButton from "@/components/documents/filters/sort-button";
+import { Pagination } from "@/components/documents/pagination";
import { AddFolderModal } from "@/components/folders/add-folder-modal";
import AppLayout from "@/components/layouts/app";
import { SearchBoxPersisted } from "@/components/search-box";
@@ -106,14 +106,15 @@ export default function Documents() {
/>
{isFiltered && pagination && (
- <DocumentsPagination
+ <Pagination
currentPage={currentPage}
pageSize={pageSize}
- totalDocuments={pagination.total}
- totalShownDocuments={documents.length}
+ totalItems={pagination.total}
+ totalShownItems={documents.length}
totalPages={pagination.pages}
onPageChange={updatePagination}
onPageSizeChange={(size) => updatePagination(undefined, size)}
+ itemName="documents"
/>
)}
</div>