-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInputArea.tsx
52 lines (43 loc) · 1.38 KB
/
InputArea.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { Textarea } from "@mantine/core"
import { atom, useAtom, useSetAtom } from "jotai"
import { historyListAtom, } from "./History"
import { dfCtx } from "../App"
export const sqlAtom = atom('')
const COMMAND_KEY = '⌘';
const CTRL_KEY = "Ctrl"
export function InputArea() {
const [sql, setSql] = useAtom(sqlAtom)
const setHistoryList = useSetAtom(historyListAtom)
const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
setSql(e.currentTarget.value)
}
const handleCtrlEnter = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) {
doQuery();
e.currentTarget.value = ''
}
}
const doQuery = () => {
const result = dfCtx.execute_sql(sql)
result.then((r: string) => {
setHistoryList((history) => [{ query: sql, result: r, isErr: false }, ...history])
}).catch((e: string) => {
setHistoryList((history) => [{ query: sql, result: e, isErr: true }, ...history])
})
console.log('doQuery' + sql)
}
const isMac = navigator.userAgent.indexOf('Mac OS X') != -1;
return (
< Textarea
className="m-4"
size="md"
radius="m"
minRows={4}
maxRows={7}
autosize={true}
description={`${isMac ? COMMAND_KEY : CTRL_KEY} + Enter to execute`}
placeholder="SQL here"
onChange={handleChange}
onKeyDown={handleCtrlEnter}
/>)
}