Skip to content

Commit c84b9e2

Browse files
committed
set page based on query
1 parent ecaead9 commit c84b9e2

File tree

1 file changed

+15
-2
lines changed
  • packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/explore

1 file changed

+15
-2
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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 { useQueryState } from "nuqs";
5+
import { parseAsInteger, useQueryState } from "nuqs";
66
import { useEffect, useRef, useState } from "react";
77
import { useForm } from "react-hook-form";
88
import { Table } from "@latticexyz/config";
@@ -12,9 +12,10 @@ import { Button } from "../../../../../../components/ui/Button";
1212
import { Form, FormField } from "../../../../../../components/ui/Form";
1313
import { cn } from "../../../../../../utils";
1414
import { useTableDataQuery } from "../../../../queries/useTableDataQuery";
15-
import { monacoOptions } from "./consts";
15+
import { PAGE_SIZE_OPTIONS, monacoOptions } from "./consts";
1616
import { useMonacoSuggestions } from "./useMonacoSuggestions";
1717
import { useQueryValidator } from "./useQueryValidator";
18+
import { getLimitOffset } from "./utils/getLimitOffset";
1819

1920
type Props = {
2021
table?: Table;
@@ -28,6 +29,7 @@ export function SQLEditor({ table, isLiveQuery, setIsLiveQuery }: Props) {
2829
const [isFocused, setIsFocused] = useState(false);
2930
const [isUserTriggeredRefetch, setIsUserTriggeredRefetch] = useState(false);
3031
const [query, setQuery] = useQueryState("query", { defaultValue: "" });
32+
const [, setPage] = useQueryState("page", parseAsInteger.withDefault(0));
3133

3234
const validateQuery = useQueryValidator(table);
3335
const {
@@ -52,6 +54,17 @@ export function SQLEditor({ table, isLiveQuery, setIsLiveQuery }: Props) {
5254
const handleSubmit = form.handleSubmit(({ query }) => {
5355
if (validateQuery(query)) {
5456
setQuery(query);
57+
58+
// Set the page based on the query
59+
const { limit, offset } = getLimitOffset(query);
60+
if (limit == null || offset == null) {
61+
setPage(0);
62+
} else if (PAGE_SIZE_OPTIONS.includes(limit) && (offset === 0 || offset % limit === 0)) {
63+
setPage(offset / limit);
64+
} else {
65+
setPage(0);
66+
}
67+
5568
setIsUserTriggeredRefetch(true);
5669
refetch().finally(() => setIsUserTriggeredRefetch(false));
5770
}

0 commit comments

Comments
 (0)