-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryHeader.tsx
62 lines (57 loc) · 2.25 KB
/
QueryHeader.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
import React from 'react';
import { BuilderMode, QuestDBQuery, Format, QueryType, QuestDBBuilderQuery } from '../types';
import { QueryTypeSwitcher } from 'components/QueryTypeSwitcher';
import { FormatSelect } from '../components/FormatSelect';
import { Button } from '@grafana/ui';
import { getFormat } from 'components/editor';
import { EditorHeader, FlexItem } from '@grafana/plugin-ui';
import { Datasource } from '../data/QuestDbDatasource';
interface QueryHeaderProps {
query: QuestDBQuery;
onChange: (query: QuestDBQuery) => void;
onRunQuery: () => void;
datasource: Datasource;
}
export const QueryHeader = ({ query, onChange, onRunQuery, datasource }: QueryHeaderProps) => {
React.useEffect(() => {
if (typeof query.selectedFormat === 'undefined' && query.queryType === QueryType.SQL) {
const selectedFormat = Format.AUTO;
const format = getFormat(query.rawSql, selectedFormat);
onChange({ ...query, selectedFormat, format });
}
}, [query, onChange]);
const runQuery = () => {
if (query.queryType === QueryType.SQL) {
const format = getFormat(query.rawSql, query.selectedFormat);
if (format !== query.format) {
onChange({ ...query, format });
}
}
onRunQuery();
};
const onFormatChange = (selectedFormat: Format) => {
switch (query.queryType) {
case QueryType.SQL:
onChange({ ...query, format: getFormat(query.rawSql, selectedFormat), selectedFormat });
case QueryType.Builder:
default:
if (selectedFormat === Format.AUTO) {
let builderOptions = (query as QuestDBBuilderQuery).builderOptions;
const format = builderOptions && builderOptions.mode === BuilderMode.Trend ? Format.TIMESERIES : Format.TABLE;
onChange({ ...query, format, selectedFormat });
} else {
onChange({ ...query, format: selectedFormat, selectedFormat });
}
}
};
return (
<EditorHeader>
<QueryTypeSwitcher query={query} onChange={onChange} datasource={datasource} />
<FlexItem grow={1} />
<Button variant="primary" icon="play" size="sm" onClick={runQuery}>
Run query
</Button>
<FormatSelect format={query.selectedFormat ?? Format.AUTO} onChange={onFormatChange} />
</EditorHeader>
);
};