@@ -33,6 +33,8 @@ import { TData, TDataRow, useTableDataQuery } from "../../../../queries/useTable
33
33
import { indexerForChainId } from "../../../../utils/indexerForChainId" ;
34
34
import { EditableTableCell } from "./EditableTableCell" ;
35
35
import { ExportButton } from "./ExportButton" ;
36
+ import { PAGE_SIZE_OPTIONS } from "./consts" ;
37
+ import { getLimitOffset } from "./utils/getLimitOffset" ;
36
38
import { typeSortingFn } from "./utils/typeSortingFn" ;
37
39
38
40
const initialSortingState : SortingState = [ ] ;
@@ -153,6 +155,16 @@ export function TablesViewer({ table, isLiveQuery }: Props) {
153
155
} ,
154
156
} ) ;
155
157
158
+ // Pagination is only enabled if the query has a LIMIT and OFFSET that are divisible by the page size
159
+ const isPaginationEnabled = useMemo ( ( ) => {
160
+ if ( ! query ) return false ;
161
+
162
+ const { limit, offset } = getLimitOffset ( query ) ;
163
+ if ( limit == null || offset == null ) return false ;
164
+
165
+ return PAGE_SIZE_OPTIONS . includes ( limit ) && offset % pagination . pageSize === 0 ;
166
+ } , [ pagination . pageSize , query ] ) ;
167
+
156
168
return (
157
169
< div
158
170
className = { cn ( "space-y-4" , {
@@ -237,12 +249,13 @@ export function TablesViewer({ table, isLiveQuery }: Props) {
237
249
< Select
238
250
value = { pagination . pageSize . toString ( ) }
239
251
onValueChange = { ( value ) => reactTable . setPageSize ( Number ( value ) ) }
252
+ disabled = { ! isPaginationEnabled }
240
253
>
241
254
< SelectTrigger className = "h-8 w-[70px]" >
242
255
< SelectValue > { pagination . pageSize } </ SelectValue >
243
256
</ SelectTrigger >
244
257
< SelectContent >
245
- { [ 5 , 10 , 20 , 30 , 40 , 50 , 100 ] . map ( ( pageSize ) => (
258
+ { PAGE_SIZE_OPTIONS . map ( ( pageSize ) => (
246
259
< SelectItem key = { pageSize } value = { pageSize . toString ( ) } >
247
260
{ pageSize }
248
261
</ SelectItem >
@@ -257,23 +270,28 @@ export function TablesViewer({ table, isLiveQuery }: Props) {
257
270
variant = "outline"
258
271
size = "sm"
259
272
onClick = { ( ) => reactTable . setPageIndex ( 0 ) }
260
- disabled = { ! reactTable . getCanPreviousPage ( ) }
273
+ disabled = { ! isPaginationEnabled || ! reactTable . getCanPreviousPage ( ) }
261
274
>
262
275
< ChevronsLeftIcon className = "mr-1 h-4 w-4" />
263
276
</ Button >
264
277
< Button
265
278
variant = "outline"
266
279
size = "sm"
267
280
onClick = { ( ) => reactTable . previousPage ( ) }
268
- disabled = { ! reactTable . getCanPreviousPage ( ) }
281
+ disabled = { ! isPaginationEnabled || ! reactTable . getCanPreviousPage ( ) }
269
282
>
270
283
< ChevronLeftIcon className = "mr-1 h-4 w-4" /> Prev
271
284
</ Button >
272
285
< Button
273
286
variant = "outline"
274
287
size = "sm"
275
288
onClick = { ( ) => reactTable . nextPage ( ) }
276
- disabled = { ! reactTable . getCanNextPage ( ) || ! tableData || tableData . rows . length < pagination . pageSize }
289
+ disabled = {
290
+ ! isPaginationEnabled ||
291
+ ! reactTable . getCanNextPage ( ) ||
292
+ ! tableData ||
293
+ tableData . rows . length < pagination . pageSize
294
+ }
277
295
>
278
296
Next < ChevronRightIcon className = "ml-1 h-4 w-4" />
279
297
</ Button >
0 commit comments