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
69
70
71
72
73
74
75
76
77
78
79
80
81
|
import { Button, Modal, Stack, Textarea } from '@mantine/core';
import React, { useState } from 'react';
import { Icon } from 'app/components';
import {
ACTION,
dispatch,
jsonToInputs,
} from 'app/pages/querybuilder/context/query-builder-provider';
function PasteForm({ close }) {
const [query, setQuery] = useState('');
const [error, setError] = useState('');
function onSubmit() {
try {
const json = JSON.parse(query);
try {
jsonToInputs(json);
dispatch(ACTION.SET_QUERY, query);
close();
} catch (error) {
setError(`Invalid Vespa query: ${error.message}`);
}
} catch (error) {
setError(`Invalid JSON: ${error.message}`);
}
}
return (
<Stack>
<Textarea
styles={(theme) => ({
input: {
fontFamily: theme.fontFamilyMonospace,
fontSize: theme.fontSizes.xs,
},
})}
placeholder="Your Vespa query JSON"
error={error}
minRows={21}
value={query}
onChange={({ target }) => setQuery(target.value)}
autosize
/>
<Button leftIcon={<Icon name="paste" />} onClick={onSubmit}>
Save
</Button>
</Stack>
);
}
export function PasteModal() {
const [opened, setOpened] = useState(false);
const close = () => setOpened(false);
return (
<>
<Modal
opened={opened}
onClose={close}
title="Vespa Query"
overlayColor="white"
shadow="1px 3px 10px 2px rgb(0 0 0 / 20%)"
padding="xl"
size="xl"
centered
>
<PasteForm close={close} />
</Modal>
<Button
onClick={() => setOpened(true)}
leftIcon={<Icon name="paste" />}
variant="outline"
size="xs"
compact
>
Paste JSON
</Button>
</>
);
}
|