diff --git a/src/components/data-table-remote.tsx b/src/components/data-table-remote.tsx index 2e8309cf..56f89755 100644 --- a/src/components/data-table-remote.tsx +++ b/src/components/data-table-remote.tsx @@ -2,22 +2,18 @@ import { Loader } from '@/components/loader'; import { Button } from '@/components/ui/button'; +import { Pagination, PaginationContent, PaginationEllipsis, PaginationItem } from '@/components/ui/pagination'; import { RowCheckbox } from '@/components/ui/row-checkbox'; +import { Select, SelectContent, SelectItem, SelectTrigger } from '@/components/ui/select'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table'; -import {getSortedRowModel, SortingState, Table as ReactTable} from "@tanstack/react-table" import { TooltipProvider } from '@/components/ui/tooltip'; import { DataTableProvider } from '@/components/use-data-table'; import type { Page } from '@/lib/database'; import { fetcher } from '@/lib/fetch'; -import { - ColumnDef, - type ColumnFilter, - flexRender, - getCoreRowModel, - useReactTable -} from '@tanstack/react-table'; +import { ColumnDef, type ColumnFilter, flexRender, getCoreRowModel, getSortedRowModel, SortingState, Table as ReactTable, useReactTable } from '@tanstack/react-table'; import type { PaginationState } from '@tanstack/table-core'; +import { ChevronLeft, ChevronRight } from 'lucide-react'; import { type ReactNode, useEffect, useMemo, useState } from 'react'; import useSWR from 'swr'; @@ -54,7 +50,7 @@ export function DataTableRemote ({ const [rowSelection, setRowSelection] = useState({}); const [columnFilters, setColumnFilters] = useState([]); const [globalFilter, setGlobalFilter] = useState(''); - const [sorting, setSorting] = useState(defaultSorting) + const [sorting, setSorting] = useState(defaultSorting); const idSelection = useMemo(() => { return Object.keys(rowSelection); @@ -83,7 +79,8 @@ export function DataTableRemote ({ refreshInterval, revalidateOnReconnect: false, revalidateOnFocus: false, - focusThrottleInterval: 1000 + focusThrottleInterval: 1000, + keepPreviousData: true, }); useEffect(() => { @@ -168,10 +165,10 @@ export function DataTableRemote ({ ? null : flexRender( header.column.columnDef.header, - header.getContext() + header.getContext(), )} - ) + ); })} ))} @@ -201,7 +198,7 @@ export function DataTableRemote ({ -
+
{selectable && ( <> @@ -210,23 +207,7 @@ export function DataTableRemote ({ {batchOperations?.(idSelection, () => mutate())} )} - - +
{after} @@ -234,6 +215,97 @@ export function DataTableRemote ({ ); } -function getSortingSearchString(sorting: SortingState) { +function getSortingSearchString (sorting: SortingState) { return sorting.map(({ id, desc }) => `${id}:${desc ? 'desc' : 'asc'}`).join(','); -} \ No newline at end of file +} + +const sizes = [10, 20, 50, 100]; + +function TablePagination ({ className, limit = 4, loading, table }: { className?: string, limit?: number, loading: boolean, table: ReactTable }) { + const options = table.getPageOptions(); + const pagination = table.getState().pagination; + + const min = Math.max(pagination.pageIndex - limit / 2, 0); + const max = Math.min(min + limit + 1, options.length - 1); + + if (min >= max) { + return ; + } + + return ( + + + + + + + {min > 0 && ( + + + + )} + {min > 1 && ( + + + + )} + {steps(min, max).map((page) => ( + + + + ))} + {(max < options.length - 2) && ( + + + + )} + {(max < options.length - 1) && ( + + + + )} + + + + + + ); +} + +function steps (from: number, to: number) { + if (from >= to) { + return []; + } + let arr = new Array(to - from + 1); + for (let i = from; i <= to; i++) { + arr[i - from] = i; + } + + return arr; +}