-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQueryEditor.tsx
68 lines (57 loc) · 2.7 KB
/
QueryEditor.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2024 Massachusetts Institute of Technology
// SPDX-License-Identifier: MIT
import CodeMirror from '@uiw/react-codemirror';
import { StreamLanguage } from '@codemirror/language';
import { sparql } from '@codemirror/legacy-modes/mode/sparql';
import { useDisclosure } from '@mantine/hooks';
import { ActionIcon, Button, Divider, Modal, Title } from '@mantine/core';
import { IconCaretRight, IconHistory } from '@tabler/icons-react';
import { setResults } from 'redux/resultsSlice';
import { setQueryValue } from 'redux/queryValueSlice';
import { useAppDispatch, useAppSelector } from 'redux/store';
import { useRunQuery } from 'hooks/useRunQuery';
import styles from "./QueryEditor.module.scss"
export function QueryEditor() {
const dispatch = useAppDispatch()
const queryHistory = useAppSelector(state => state.queryHistory.queryHistory)
const queryValue = useAppSelector(state => state.queryValue.queryValue)
const { runQuery } = useRunQuery()
const [historyOpened, { open:openHistory, close:closeHistory }] = useDisclosure(false);
return (
<div>
{/* probably want to style this appropriately later */}
<Title style={{color:"white", marginLeft: 13, marginBottom: 3, marginTop: 2, padding: 1}} order={4}>Query Editor</Title>
<div id={styles["query-editor-container"]}>
<div style={{display: "flex", flexDirection: "column"}}>
<ActionIcon size="lg" radius={0} variant="filled" aria-label="Run Query" onClick={() => runQuery(queryValue)}>
<IconCaretRight/>
</ActionIcon>
<ActionIcon size="lg" radius={0} variant="filled" color="gray" aria-label="History" onClick={openHistory}>
<IconHistory/>
</ActionIcon>
</div>
<div>
<CodeMirror
extensions={[StreamLanguage.define(sparql)]}
onChange={val => dispatch(setQueryValue(val))}
value={queryValue}
/>
</div>
<Modal opened={historyOpened} onClose={closeHistory} size="lg" withCloseButton={false}>
<Title order={2}>Query History</Title>
<Divider/>
{queryHistory.map((record,i) => {
return (
<Button key={i} className={styles["query-history-button"]} fullWidth variant='default' onClick={() => {
dispatch(setQueryValue(record.query))
dispatch(setResults(record.results))
}}>
{i+1}: {record.name || "There was an error generating a name for this query"}
</Button>
)
})}
</Modal>
</div>
</div>
);
}