grafana/public/app/plugins/datasource/loki/components/VariableQueryEditor.tsx
Matias Chomicki 6e6069a2ba
Loki: Create Variable Query Editor for Loki. (#54102)
* feat(loki-query-editor): create base editor component

* feat(loki-query-editor): update editor to use loki query type

* feat(loki-query-editor): add custom variable support to datasource

* feat(loki-query-editor): prevent errors when no label is present

* Add unit test for LokiMetricFindQuery

* Update datasource test

* Add component test

* Add variable query migration support

* feat(loki-query-editor): add migration support for older-format variables

* Fix enum capitalization for consistency

* Move attribute to the top of the class

* Remove unnecessary from()

* Update capitalization of new enum

* Fix enum capitalization in component

* feat(loki-query-editor): replace unnecessary class with class method
2022-08-30 18:18:51 +02:00

90 lines
2.7 KiB
TypeScript

import React, { FC, FormEvent, useState, useEffect } from 'react';
import { QueryEditorProps, SelectableValue } from '@grafana/data';
import { InlineField, InlineFieldRow, Input, Select } from '@grafana/ui';
import { LokiDatasource } from '../datasource';
import { migrateVariableQuery } from '../migrations/variableQueryMigrations';
import { LokiOptions, LokiQuery, LokiVariableQuery, LokiVariableQueryType as QueryType } from '../types';
const variableOptions = [
{ label: 'Label names', value: QueryType.LabelNames },
{ label: 'Label values', value: QueryType.LabelValues },
];
export type Props = QueryEditorProps<LokiDatasource, LokiQuery, LokiOptions, LokiVariableQuery>;
export const LokiVariableQueryEditor: FC<Props> = ({ onChange, query }) => {
const [type, setType] = useState<number | undefined>(undefined);
const [label, setLabel] = useState('');
const [stream, setStream] = useState('');
useEffect(() => {
if (!query || typeof query !== 'string') {
return;
}
const variableQuery = migrateVariableQuery(query);
setType(variableQuery.type);
setLabel(variableQuery.label || '');
setStream(variableQuery.stream || '');
}, [query]);
const onQueryTypeChange = (newType: SelectableValue<QueryType>) => {
setType(newType.value);
if (newType.value !== undefined) {
onChange({
type: newType.value,
label,
stream,
refId: 'LokiVariableQueryEditor-VariableQuery',
});
}
};
const onLabelChange = (e: FormEvent<HTMLInputElement>) => {
setLabel(e.currentTarget.value);
};
const onStreamChange = (e: FormEvent<HTMLInputElement>) => {
setStream(e.currentTarget.value);
};
const handleBlur = () => {
if (type !== undefined) {
onChange({ type, label, stream, refId: 'LokiVariableQueryEditor-VariableQuery' });
}
};
return (
<InlineFieldRow>
<InlineField label="Query type" labelWidth={20}>
<Select
aria-label="Query type"
onChange={onQueryTypeChange}
onBlur={handleBlur}
value={type}
options={variableOptions}
width={16}
/>
</InlineField>
{type === QueryType.LabelValues && (
<>
<InlineField label="Label" labelWidth={20}>
<Input type="text" aria-label="Label" value={label} onChange={onLabelChange} onBlur={handleBlur} />
</InlineField>
<InlineField label="Stream selector" labelWidth={20}>
<Input
type="text"
aria-label="Stream selector"
value={stream}
onChange={onStreamChange}
onBlur={handleBlur}
/>
</InlineField>
</>
)}
</InlineFieldRow>
);
};