import React, { useEffect, useState } from 'react'; import { DataQuery, SelectableValue } from '@grafana/data'; import { InlineField, InlineFieldRow, Select } from '@grafana/ui'; import { TempoDatasource } from './datasource'; export enum TempoVariableQueryType { LabelNames, LabelValues, } export interface TempoVariableQuery extends DataQuery { type: TempoVariableQueryType; label?: string; stream?: string; } const variableOptions = [ { label: 'Label names', value: TempoVariableQueryType.LabelNames }, { label: 'Label values', value: TempoVariableQueryType.LabelValues }, ]; const refId = 'TempoDatasourceVariableQueryEditor-VariableQuery'; export type TempoVariableQueryEditorProps = { onChange: (value: TempoVariableQuery) => void; query: TempoVariableQuery; datasource: TempoDatasource; }; export const TempoVariableQueryEditor = ({ onChange, query, datasource }: TempoVariableQueryEditorProps) => { const [label, setLabel] = useState(query.label || ''); const [type, setType] = useState(query.type); const [labelOptions, setLabelOptions] = useState>>([]); useEffect(() => { if (type === TempoVariableQueryType.LabelValues) { datasource.labelNamesQuery().then((labelNames: Array<{ text: string }>) => { setLabelOptions(labelNames.map(({ text }) => ({ label: text, value: text }))); }); } }, [datasource, query, type]); const onQueryTypeChange = (newType: SelectableValue) => { setType(newType.value); if (newType.value !== undefined) { onChange({ type: newType.value, label, refId, }); } }; const onLabelChange = (newLabel: SelectableValue) => { const newLabelValue = newLabel.value || ''; setLabel(newLabelValue); if (type !== undefined) { onChange({ type, label: newLabelValue, refId, }); } }; const handleBlur = () => { if (type !== undefined) { onChange({ type, label, refId }); } }; return ( <> )} ); };