Skip to content

Commit 3ae1c40

Browse files
committed
use pagination query hook, parse as json
1 parent bde5cc2 commit 3ae1c40

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/explore/SQLEditor.tsx

+3-9
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
import { CommandIcon, CornerDownLeft, LoaderIcon, PauseIcon, PlayIcon } from "lucide-react";
44
import { KeyCode, KeyMod, editor } from "monaco-editor/esm/vs/editor/editor.api";
5-
import { parseAsJson, useQueryState } from "nuqs";
5+
import { useQueryState } from "nuqs";
66
import { useEffect, useRef, useState } from "react";
77
import { useForm } from "react-hook-form";
88
import { Table } from "@latticexyz/config";
99
import Editor from "@monaco-editor/react";
10-
import { PaginationState } from "@tanstack/react-table";
1110
import { Tooltip } from "../../../../../../components/Tooltip";
1211
import { Button } from "../../../../../../components/ui/Button";
1312
import { Form, FormField } from "../../../../../../components/ui/Form";
1413
import { cn } from "../../../../../../utils";
1514
import { useTableDataQuery } from "../../../../queries/useTableDataQuery";
1615
import { PAGE_SIZE_OPTIONS, monacoOptions } from "./consts";
16+
import { usePaginationQueryState } from "./hooks/usePaginationQueryState";
1717
import { useMonacoSuggestions } from "./useMonacoSuggestions";
1818
import { useQueryValidator } from "./useQueryValidator";
1919
import { getLimitOffset } from "./utils/getLimitOffset";
@@ -30,13 +30,7 @@ export function SQLEditor({ table, isLiveQuery, setIsLiveQuery }: Props) {
3030
const [isFocused, setIsFocused] = useState(false);
3131
const [isUserTriggeredRefetch, setIsUserTriggeredRefetch] = useState(false);
3232
const [query, setQuery] = useQueryState("query", { defaultValue: "" });
33-
const [pagination, setPagination] = useQueryState(
34-
"pagination",
35-
parseAsJson<PaginationState>().withDefault({
36-
pageIndex: 0,
37-
pageSize: 10,
38-
}),
39-
);
33+
const [pagination, setPagination] = usePaginationQueryState();
4034

4135
const validateQuery = useQueryValidator(table);
4236
const {

packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/explore/TablesViewer.tsx

+3-8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { indexerForChainId } from "../../../../utils/indexerForChainId";
3434
import { EditableTableCell } from "./EditableTableCell";
3535
import { ExportButton } from "./ExportButton";
3636
import { PAGE_SIZE_OPTIONS } from "./consts";
37+
import { usePaginationQueryState } from "./hooks/usePaginationQueryState";
3738
import { getLimitOffset } from "./utils/getLimitOffset";
3839
import { typeSortingFn } from "./utils/typeSortingFn";
3940

@@ -52,15 +53,9 @@ export function TablesViewer({ table, isLiveQuery }: Props) {
5253
const [query, setQuery] = useQueryState("query", parseAsString.withDefault(""));
5354
const [globalFilter, setGlobalFilter] = useQueryState("filter", parseAsString.withDefault(""));
5455
const [sorting, setSorting] = useQueryState("sort", parseAsJson<SortingState>().withDefault(initialSortingState));
56+
const [pagination, setPagination] = usePaginationQueryState();
5557
const { data: tableData, isPending, isFetching, isError, error } = useTableDataQuery({ table, query, isLiveQuery });
5658
const isLoading = isPending || (isFetching && !isLiveQuery);
57-
const [pagination, setPagination] = useQueryState(
58-
"pagination",
59-
parseAsJson<PaginationState>().withDefault({
60-
pageIndex: 0,
61-
pageSize: 10,
62-
}),
63-
);
6459

6560
const handlePaginationChange: OnChangeFn<PaginationState> = useCallback(
6661
(updater) => {
@@ -77,7 +72,7 @@ export function TablesViewer({ table, isLiveQuery }: Props) {
7772

7873
return newPaginationState;
7974
},
80-
[pagination, query, setQuery],
75+
[pagination, setPagination, query, setQuery],
8176
);
8277

8378
const tableColumns: ColumnDef<TDataRow>[] = useMemo(() => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { parseAsJson, useQueryState } from "nuqs";
2+
import { PaginationState } from "@tanstack/react-table";
3+
4+
export function usePaginationQueryState(defaultPageSize = 10) {
5+
return useQueryState(
6+
"pagination",
7+
parseAsJson<PaginationState>().withDefault({
8+
pageIndex: 0,
9+
pageSize: defaultPageSize,
10+
}),
11+
);
12+
}

0 commit comments

Comments
 (0)