11import { Textarea } from "@mantine/core"
2- import { atom , useAtom , useSetAtom } from "jotai"
2+ import { atom , useAtom } from "jotai"
3+ import { useState } from "react" ;
34import { historyListAtom , } from "./History"
45import { dfCtx } from "../App"
56
@@ -10,7 +11,8 @@ const CTRL_KEY = "Ctrl"
1011
1112export function InputArea ( ) {
1213 const [ sql , setSql ] = useAtom ( sqlAtom )
13- const setHistoryList = useSetAtom ( historyListAtom )
14+ const [ historyList , setHistoryList ] = useAtom ( historyListAtom ) ;
15+ const [ historyCursor , setHistoryCursor ] = useState ( - 1 ) ;
1416
1517 const handleChange = ( e : React . ChangeEvent < HTMLTextAreaElement > ) => {
1618 setSql ( e . currentTarget . value )
@@ -20,7 +22,27 @@ export function InputArea() {
2022 if ( e . key === 'Enter' && ( e . ctrlKey || e . metaKey ) ) {
2123 doQuery ( ) ;
2224
23- e . currentTarget . value = ''
25+ setSql ( "" ) ;
26+ setHistoryCursor ( - 1 ) ;
27+ }
28+ if ( e . key === "ArrowUp" ) {
29+ const newHistoryCursor = Math . min (
30+ historyCursor + 1 ,
31+ historyList . length - 2
32+ ) ;
33+ setHistoryCursor ( newHistoryCursor ) ;
34+
35+ setSql ( historyList [ newHistoryCursor ] . query ) ;
36+ }
37+ if ( e . key === "ArrowDown" ) {
38+ const newHistoryCursor = Math . max ( historyCursor - 1 , - 1 ) ;
39+ setHistoryCursor ( newHistoryCursor ) ;
40+
41+ if ( newHistoryCursor === - 1 ) {
42+ setSql ( "" ) ;
43+ } else {
44+ setSql ( historyList [ newHistoryCursor ] . query ) ;
45+ }
2446 }
2547 }
2648
@@ -47,6 +69,7 @@ export function InputArea() {
4769 description = { `${ isMac ? COMMAND_KEY : CTRL_KEY } + Enter to execute` }
4870 placeholder = "SQL here"
4971 onChange = { handleChange }
72+ value = { sql }
5073 onKeyDown = { handleCtrlEnter }
5174 /> )
5275}
0 commit comments