2
2
3
3
import { CommandIcon , CornerDownLeft , LoaderIcon , PauseIcon , PlayIcon } from "lucide-react" ;
4
4
import { KeyCode , KeyMod , editor } from "monaco-editor/esm/vs/editor/editor.api" ;
5
- import { useQueryState } from "nuqs" ;
5
+ import { parseAsInteger , useQueryState } from "nuqs" ;
6
6
import { useEffect , useRef , useState } from "react" ;
7
7
import { useForm } from "react-hook-form" ;
8
8
import { Table } from "@latticexyz/config" ;
@@ -12,9 +12,10 @@ import { Button } from "../../../../../../components/ui/Button";
12
12
import { Form , FormField } from "../../../../../../components/ui/Form" ;
13
13
import { cn } from "../../../../../../utils" ;
14
14
import { useTableDataQuery } from "../../../../queries/useTableDataQuery" ;
15
- import { monacoOptions } from "./consts" ;
15
+ import { PAGE_SIZE_OPTIONS , monacoOptions } from "./consts" ;
16
16
import { useMonacoSuggestions } from "./useMonacoSuggestions" ;
17
17
import { useQueryValidator } from "./useQueryValidator" ;
18
+ import { getLimitOffset } from "./utils/getLimitOffset" ;
18
19
19
20
type Props = {
20
21
table ?: Table ;
@@ -28,6 +29,7 @@ export function SQLEditor({ table, isLiveQuery, setIsLiveQuery }: Props) {
28
29
const [ isFocused , setIsFocused ] = useState ( false ) ;
29
30
const [ isUserTriggeredRefetch , setIsUserTriggeredRefetch ] = useState ( false ) ;
30
31
const [ query , setQuery ] = useQueryState ( "query" , { defaultValue : "" } ) ;
32
+ const [ , setPage ] = useQueryState ( "page" , parseAsInteger . withDefault ( 0 ) ) ;
31
33
32
34
const validateQuery = useQueryValidator ( table ) ;
33
35
const {
@@ -52,6 +54,17 @@ export function SQLEditor({ table, isLiveQuery, setIsLiveQuery }: Props) {
52
54
const handleSubmit = form . handleSubmit ( ( { query } ) => {
53
55
if ( validateQuery ( query ) ) {
54
56
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
+
55
68
setIsUserTriggeredRefetch ( true ) ;
56
69
refetch ( ) . finally ( ( ) => setIsUserTriggeredRefetch ( false ) ) ;
57
70
}
0 commit comments