|
12 | 12 | // limitations under the License. |
13 | 13 |
|
14 | 14 | import { Stack, TextField } from '@mui/material'; |
15 | | -import { OptionsEditorProps, DatasourceSelect } from '@perses-dev/plugin-system'; |
16 | | -import { DatasourceSelector } from '@perses-dev/spec'; |
| 15 | +import { |
| 16 | + DatasourceSelect, |
| 17 | + DatasourceSelectProps, |
| 18 | + isVariableDatasource, |
| 19 | + OptionsEditorProps, |
| 20 | +} from '@perses-dev/plugin-system'; |
17 | 21 | import { SQLTimeSeriesQuerySpec } from './sql-time-series-query-types'; |
18 | 22 |
|
19 | | -export function SQLTimeSeriesQueryEditor({ value, onChange }: OptionsEditorProps<SQLTimeSeriesQuerySpec>) { |
20 | | - const handleDatasourceChange = (newDatasource: DatasourceSelector | string | undefined) => { |
21 | | - // Convert string to DatasourceSelector if needed |
22 | | - const datasourceValue: DatasourceSelector | undefined = |
23 | | - typeof newDatasource === 'string' |
24 | | - ? { kind: 'SQLDatasource', name: newDatasource } |
25 | | - : (newDatasource as DatasourceSelector | undefined); |
| 23 | +const DATASOURCE_KIND = 'SQLDatasource'; |
26 | 24 |
|
27 | | - onChange({ ...value, datasource: datasourceValue }); |
| 25 | +export function SQLTimeSeriesQueryEditor({ value, onChange }: OptionsEditorProps<SQLTimeSeriesQuerySpec>) { |
| 26 | + const handleDatasourceChange: DatasourceSelectProps['onChange'] = (newDatasource) => { |
| 27 | + if (isVariableDatasource(newDatasource)) { |
| 28 | + onChange({ ...value, datasource: newDatasource }); |
| 29 | + return; |
| 30 | + } |
| 31 | + if (newDatasource.kind === DATASOURCE_KIND) { |
| 32 | + onChange({ ...value, datasource: newDatasource }); |
| 33 | + return; |
| 34 | + } |
| 35 | + throw new Error('Got unexpected non SQLDatasource selection'); |
28 | 36 | }; |
29 | 37 |
|
30 | 38 | return ( |
31 | 39 | <Stack spacing={2}> |
32 | 40 | <DatasourceSelect |
33 | | - datasourcePluginKind="SQLDatasource" |
34 | | - value={value?.datasource || { kind: 'SQLDatasource' }} |
| 41 | + datasourcePluginKind={DATASOURCE_KIND} |
| 42 | + value={value?.datasource ?? { kind: DATASOURCE_KIND }} |
35 | 43 | onChange={handleDatasourceChange} |
36 | 44 | /> |
37 | 45 |
|
|
0 commit comments