-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHaystackVariableQueryEditor.tsx
More file actions
106 lines (96 loc) · 3.57 KB
/
HaystackVariableQueryEditor.tsx
File metadata and controls
106 lines (96 loc) · 3.57 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import React from 'react';
import { HaystackDataSourceOptions, HaystackQuery, HaystackVariableQuery } from '../types';
import { HaystackQueryTypeSelector } from './HaystackQueryTypeSelector';
import { HaystackQueryInput } from './HaystackQueryInput';
import { QueryEditorProps } from '@grafana/data';
import { InlineField, Input, Stack } from '@grafana/ui';
import { DataSource } from 'datasource';
export type Props = QueryEditorProps<DataSource, HaystackQuery, HaystackDataSourceOptions, HaystackVariableQuery>;
const refId = 'HaystackVariableQueryEditor-VariableQuery';
export const HaystackVariableQueryEditor = ({ onChange, query }: Props) => {
let variableInputWidth = 30;
// Computes the query string and calls the onChange function. Should be used instead of onChange for all mutating functions.
const onChangeAndSave = (query: HaystackVariableQuery) => {
let type = query.type;
let queryCmd = "";
if (query.type === "eval") {
queryCmd = query.eval ?? "";
} else if (query.type === "hisRead") {
queryCmd = query.hisRead ?? "";
} else if (query.type === "hisReadFilter") {
queryCmd = query.hisReadFilter ?? "";
} else if (query.type === "read") {
queryCmd = query.read ?? "";
}
let column = "none";
if (query.column !== undefined && query.column !== '') {
column = `'${query.column}'`;
}
let displayColumn = "none";
if (query.displayColumn !== undefined && query.displayColumn !== '') {
displayColumn = `'${query.displayColumn}'`;
}
let displayString = `${type}: '${queryCmd}', Column: ${column}, Display: ${displayColumn}`
onChange({ ...query, query: displayString });
};
const onTypeChange = (newType: string) => {
onChangeAndSave({ ...query, type: newType});
};
const onQueryChange = (newQuery: string) => {
if (query.type === "eval") {
onChangeAndSave({ ...query, refId: refId, eval: newQuery });
} else if (query.type === "hisRead") {
onChangeAndSave({ ...query, refId: refId, hisRead: newQuery });
} else if (query.type === "hisReadFilter") {
onChangeAndSave({ ...query, refId: refId, hisReadFilter: newQuery });
} else if (query.type === "read") {
onChangeAndSave({ ...query, refId: refId, read: newQuery });
}
};
const onColumnChange = (event: React.FormEvent<HTMLInputElement>) => {
onChangeAndSave({...query, column: event.currentTarget.value });
};
const onDisplayColumnChange = (event: React.FormEvent<HTMLInputElement>) => {
onChangeAndSave({...query, displayColumn: event.currentTarget.value });
};
const handleBlur = () => {
if (query.query !== undefined && query.query !== "") {
onChange({ ...query, refId: refId });
}
};
return (
<Stack
direction="column"
alignItems="flex-start"
>
<HaystackQueryTypeSelector
datasource={null}
type={query.type}
refId={query.refId}
onChange={onTypeChange}
/>
<HaystackQueryInput
query={query}
onChange={onQueryChange}
/>
<InlineField label="Column">
<Input
width={variableInputWidth}
onBlur={handleBlur}
onChange={onColumnChange}
value={query.column}
placeholder="Defaults to 'id' or first column"
/>
</InlineField>
<InlineField label="Display Column">
<Input
width={variableInputWidth}
onBlur={handleBlur}
onChange={onDisplayColumnChange}
value={query.displayColumn}
placeholder="Defaults to 'Column'"
/>
</InlineField>
</Stack>
);
};