-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryTypeSwitcher.tsx
100 lines (97 loc) · 4.06 KB
/
QueryTypeSwitcher.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
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
import React, { useState } from 'react';
import { SelectableValue, VariableWithMultiSupport } from '@grafana/data';
import { RadioButtonGroup, ConfirmModal } from '@grafana/ui';
import { getQueryOptionsFromSql, getSQLFromQueryOptions } from './queryBuilder/utils';
import { selectors } from './../selectors';
import { QuestDBQuery, QueryType, defaultBuilderQuery, SqlBuilderOptions, QuestDBSQLQuery } from 'types';
import { isString } from 'lodash';
import { Datasource } from '../data/QuestDbDatasource';
import { getTemplateSrv } from '@grafana/runtime';
interface QueryTypeSwitcherProps {
query: QuestDBQuery;
onChange: (query: QuestDBQuery) => void;
datasource?: Datasource;
}
export const QueryTypeSwitcher = ({ query, onChange, datasource }: QueryTypeSwitcherProps) => {
const { options: queryTypeLabels, switcher, cannotConvert } = selectors.components.QueryEditor.Types;
let queryType: QueryType =
query.queryType ||
((query as QuestDBSQLQuery).rawSql && !(query as QuestDBQuery).queryType ? QueryType.SQL : QueryType.Builder);
const [editor, setEditor] = useState<QueryType>(queryType);
const [confirmModalState, setConfirmModalState] = useState<boolean>(false);
const [cannotConvertModalState, setCannotConvertModalState] = useState<boolean>(false);
const options: Array<SelectableValue<QueryType>> = [
{ label: queryTypeLabels.SQLEditor, value: QueryType.SQL },
{ label: queryTypeLabels.QueryBuilder, value: QueryType.Builder },
];
const [errorMessage, setErrorMessage] = useState<string>('');
const templateVars = getTemplateSrv().getVariables() as VariableWithMultiSupport[];
async function onQueryTypeChange(queryType: QueryType, confirm = false) {
if (query.queryType === QueryType.SQL && queryType === QueryType.Builder && !confirm) {
const queryOptionsFromSql = await getQueryOptionsFromSql(query.rawSql);
if (isString(queryOptionsFromSql)) {
setCannotConvertModalState(true);
setErrorMessage(queryOptionsFromSql);
} else {
setConfirmModalState(true);
}
} else {
setEditor(queryType);
let builderOptions: SqlBuilderOptions;
switch (query.queryType) {
case QueryType.Builder:
builderOptions = query.builderOptions;
break;
case QueryType.SQL:
builderOptions =
((await getQueryOptionsFromSql(query.rawSql, datasource)) as SqlBuilderOptions) ||
defaultBuilderQuery.builderOptions;
break;
default:
builderOptions = defaultBuilderQuery.builderOptions;
break;
}
if (queryType === QueryType.SQL) {
onChange({
...query,
queryType,
rawSql: getSQLFromQueryOptions(builderOptions, templateVars),
meta: { builderOptions },
format: query.format,
selectedFormat: query.selectedFormat,
});
} else if (queryType === QueryType.Builder) {
onChange({ ...query, queryType, rawSql: getSQLFromQueryOptions(builderOptions, templateVars), builderOptions });
}
}
}
async function onConfirmQueryTypeChange() {
await onQueryTypeChange(QueryType.Builder, true);
setConfirmModalState(false);
setCannotConvertModalState(false);
}
return (
<>
<RadioButtonGroup size="sm" options={options} value={editor} onChange={(e) => onQueryTypeChange(e!)} />
<ConfirmModal
isOpen={confirmModalState}
title={switcher.title}
body={switcher.body}
confirmText={switcher.confirmText}
dismissText={switcher.dismissText}
icon="exclamation-triangle"
onConfirm={onConfirmQueryTypeChange}
onDismiss={() => setConfirmModalState(false)}
/>
<ConfirmModal
title={cannotConvert.title}
body={`${errorMessage} \nDo you want to delete your current query and use the query builder?`}
isOpen={cannotConvertModalState}
icon="exclamation-triangle"
onConfirm={onConfirmQueryTypeChange}
confirmText={switcher.confirmText}
onDismiss={() => setCannotConvertModalState(false)}
/>
</>
);
};