22
33import { CommandIcon , CornerDownLeft , LoaderIcon , PauseIcon , PlayIcon } from "lucide-react" ;
44import { KeyCode , KeyMod , editor } from "monaco-editor/esm/vs/editor/editor.api" ;
5- import { useQueryState } from "nuqs" ;
65import { useEffect , useRef , useState } from "react" ;
76import { useForm } from "react-hook-form" ;
87import { Table } from "@latticexyz/config" ;
@@ -12,9 +11,12 @@ import { Button } from "../../../../../../components/ui/Button";
1211import { Form , FormField } from "../../../../../../components/ui/Form" ;
1312import { cn } from "../../../../../../utils" ;
1413import { useTableDataQuery } from "../../../../queries/useTableDataQuery" ;
15- import { monacoOptions } from "./consts" ;
14+ import { PAGE_SIZE_OPTIONS , monacoOptions } from "./consts" ;
15+ import { usePaginationState } from "./hooks/usePaginationState" ;
16+ import { useSQLQueryState } from "./hooks/useSQLQueryState" ;
1617import { useMonacoSuggestions } from "./useMonacoSuggestions" ;
1718import { useQueryValidator } from "./useQueryValidator" ;
19+ import { getLimitOffset } from "./utils/getLimitOffset" ;
1820
1921type Props = {
2022 table ?: Table ;
@@ -27,7 +29,9 @@ export function SQLEditor({ table, isLiveQuery, setIsLiveQuery }: Props) {
2729 const containerRef = useRef < HTMLDivElement > ( null ) ;
2830 const [ isFocused , setIsFocused ] = useState ( false ) ;
2931 const [ isUserTriggeredRefetch , setIsUserTriggeredRefetch ] = useState ( false ) ;
30- const [ query , setQuery ] = useQueryState ( "query" , { defaultValue : "" } ) ;
32+ const [ pagination , setPagination ] = usePaginationState ( ) ;
33+ const [ query , setQuery ] = useSQLQueryState ( ) ;
34+
3135 const validateQuery = useQueryValidator ( table ) ;
3236 const {
3337 data : tableData ,
@@ -48,9 +52,28 @@ export function SQLEditor({ table, isLiveQuery, setIsLiveQuery }: Props) {
4852 } ) ;
4953 const currentQuery = form . watch ( "query" ) ;
5054
51- const handleSubmit = form . handleSubmit ( ( data ) => {
52- if ( validateQuery ( data . query ) ) {
53- setQuery ( data . query ) ;
55+ const handleSubmit = form . handleSubmit ( ( { query } ) => {
56+ if ( validateQuery ( query ) ) {
57+ setQuery ( query ) ;
58+
59+ // Set the page based on the query
60+ const { limit, offset } = getLimitOffset ( query ) ;
61+ if ( limit == null || offset == null ) {
62+ setPagination ( {
63+ ...pagination ,
64+ pageIndex : 0 ,
65+ } ) ;
66+ } else if ( PAGE_SIZE_OPTIONS . includes ( limit ) && ( offset === 0 || offset % limit === 0 ) ) {
67+ setPagination ( {
68+ pageSize : limit ,
69+ pageIndex : offset / limit ,
70+ } ) ;
71+ } else {
72+ setPagination ( {
73+ ...pagination ,
74+ pageIndex : 0 ,
75+ } ) ;
76+ }
5477
5578 setIsUserTriggeredRefetch ( true ) ;
5679 refetch ( ) . finally ( ( ) => setIsUserTriggeredRefetch ( false ) ) ;
@@ -100,10 +123,7 @@ export function SQLEditor({ table, isLiveQuery, setIsLiveQuery }: Props) {
100123 id : "executeSQL" ,
101124 label : "Execute SQL command" ,
102125 keybindings : [ KeyMod . CtrlCmd | KeyCode . Enter ] ,
103- run : ( ) => {
104- console . log ( "EXECUTING SQL COMMAND" ) ;
105- handleSubmit ( ) ;
106- } ,
126+ run : ( ) => handleSubmit ( ) ,
107127 } ) ;
108128
109129 updateHeight ( ) ;
0 commit comments