import { Trans } from "@lingui/react/macro" import { useStore } from "@nanostores/react" import type { PaginationState, Table, Updater } from "@tanstack/react-table" import { ChevronLeftIcon, ChevronRightIcon, ChevronsLeftIcon, ChevronsRightIcon } from "lucide-react" import { useCallback, useEffect, useId, useMemo, useState } from "react" import { Button } from "@/components/ui/button" import { Label } from "@/components/ui/label" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { atom } from "nanostores" const defaultPageSizes = [10, 15, 20, 30, 40, 50, 100, 200] const pageSizeKey = "besz-pageSize" // The page size is stored per device in localStorage rather than in user settings by design. // The right number of rows depends on screen size (e.g. 20 on desktop, 10 on a phone), so // syncing it across devices would mean changing it every time you switch devices. const $pageSize = atom(getLocalPageSize()) /** * Pagination state for a table. The page size is shared by all tables on this device. * Pass `pagination` to table state and `onPaginationChange` to the table options along with * `autoResetPageIndex: false`, so realtime data updates don't jump back to the first page. * Call `resetPageIndex` when the filter or sort changes. */ export function usePagination() { const savedPageSize = useStore($pageSize) // saved sizes below the smallest option (e.g. the removed 5) fall back to the smallest option const pageSize = Math.max(savedPageSize, defaultPageSizes[0]) const [pageIndex, setPageIndex] = useState(0) const pagination = useMemo(() => ({ pageIndex, pageSize }), [pageIndex, pageSize]) const onPaginationChange = useCallback( (updater: Updater) => { const next = typeof updater === "function" ? updater(pagination) : updater setPageIndex(next.pageIndex) if (next.pageSize !== pageSize) { localStorage.setItem(pageSizeKey, JSON.stringify(next.pageSize)) $pageSize.set(next.pageSize) } }, [pagination, pageSize] ) const resetPageIndex = useCallback(() => setPageIndex(0), []) return { pagination, onPaginationChange, resetPageIndex } } /** Page size saved on this device */ function getLocalPageSize(): number { try { return JSON.parse(localStorage.getItem(pageSizeKey) || "null") ?? 20 } catch { return 20 } } export function DataTablePagination({ table, pageSizes = defaultPageSizes, showSelected = true, }: { table: Table pageSizes?: number[] showSelected?: boolean }) { const { pageIndex } = table.getState().pagination const pageCount = table.getPageCount() // unique per instance, since several tables can be on the same page const selectId = useId() // move back when the current page no longer exists (e.g. after rows are removed) useEffect(() => { if (pageIndex > 0 && pageIndex >= pageCount) { table.setPageIndex(Math.max(0, pageCount - 1)) } }, [table, pageIndex, pageCount]) // every page size option shows all rows, so there's nothing to paginate or change if (table.getFilteredRowModel().rows.length <= Math.min(...pageSizes)) { return null } return ( // sections show based on the width of this container rather than the viewport, // since tables are often rendered in cards narrower than the screen
{showSelected ? ( {table.getFilteredSelectedRowModel().rows.length} of {table.getFilteredRowModel().rows.length} row(s) selected. ) : ( {table.getFilteredRowModel().rows.length} row(s) )}
Page {pageIndex + 1} of {Math.max(1, pageCount)}
) }