Files
grafana/public/app/features/transformers/configFromQuery/ConfigFromQueryTransformerEditor.tsx
Hugo Häggmark 2b8c74de2e i18n: removes useTranslate hook (#106556)
* i18n: removes useTranslate hook

* chore: fix duplicate imports

* chore: fix import sorting and hook dependencies
2025-06-12 11:03:52 +02:00

117 lines
3.9 KiB
TypeScript

import { css } from '@emotion/css';
import {
FieldMatcherID,
GrafanaTheme2,
PluginState,
SelectableValue,
TransformerRegistryItem,
TransformerUIProps,
TransformerCategory,
} from '@grafana/data';
import { t } from '@grafana/i18n';
import { fieldMatchersUI, InlineField, InlineFieldRow, Select, useStyles2 } from '@grafana/ui';
import { getTransformationContent } from '../docs/getTransformationContent';
import { FieldToConfigMappingEditor } from '../fieldToConfigMapping/FieldToConfigMappingEditor';
import { configFromDataTransformer, ConfigFromQueryTransformOptions } from './configFromQuery';
export interface Props extends TransformerUIProps<ConfigFromQueryTransformOptions> {}
export function ConfigFromQueryTransformerEditor({ input, onChange, options }: Props) {
const styles = useStyles2(getStyles);
const refIds = input
.map((x) => x.refId)
.filter((x) => x != null)
.map((x) => ({ label: x, value: x }));
const currentRefId = options.configRefId || 'config';
const currentMatcher = options.applyTo ?? { id: FieldMatcherID.byType, options: 'number' };
const matcherUI = fieldMatchersUI.get(currentMatcher.id);
const configFrame = input.find((x) => x.refId === currentRefId);
const onRefIdChange = (value: SelectableValue<string>) => {
onChange({
...options,
configRefId: value.value || 'config',
});
};
const onMatcherChange = (value: SelectableValue<string>) => {
onChange({ ...options, applyTo: { id: value.value! } });
};
const onMatcherConfigChange = (matcherOption: unknown) => {
onChange({ ...options, applyTo: { id: currentMatcher.id, options: matcherOption } });
};
const matchers = fieldMatchersUI
.list()
.filter((o) => !o.excludeFromPicker)
.map<SelectableValue<string>>((i) => ({ label: i.name, value: i.id, description: i.description }));
return (
<>
<InlineFieldRow>
<InlineField
label={t('transformers.config-from-query-transformer-editor.label-config-query', 'Config query')}
labelWidth={20}
>
<Select onChange={onRefIdChange} options={refIds} value={currentRefId} width={30} />
</InlineField>
</InlineFieldRow>
<InlineFieldRow>
<InlineField
label={t('transformers.config-from-query-transformer-editor.label-apply-to', 'Apply to')}
labelWidth={20}
>
<Select onChange={onMatcherChange} options={matchers} value={currentMatcher.id} width={30} />
</InlineField>
</InlineFieldRow>
<InlineFieldRow>
<InlineField
label={t('transformers.config-from-query-transformer-editor.label-apply-to-options', 'Apply to options')}
labelWidth={20}
className={styles.matcherOptions}
>
<matcherUI.component
matcher={matcherUI.matcher}
data={input}
options={currentMatcher.options}
onChange={onMatcherConfigChange}
/>
</InlineField>
</InlineFieldRow>
<InlineFieldRow>
{configFrame && (
<FieldToConfigMappingEditor
frame={configFrame}
mappings={options.mappings}
onChange={(mappings) => onChange({ ...options, mappings })}
withReducers
/>
)}
</InlineFieldRow>
</>
);
}
export const configFromQueryTransformRegistryItem: TransformerRegistryItem<ConfigFromQueryTransformOptions> = {
id: configFromDataTransformer.id,
editor: ConfigFromQueryTransformerEditor,
transformation: configFromDataTransformer,
name: configFromDataTransformer.name,
description: configFromDataTransformer.description,
state: PluginState.beta,
categories: new Set([TransformerCategory.CalculateNewFields]),
help: getTransformationContent(configFromDataTransformer.id).helperDocs,
};
const getStyles = (theme: GrafanaTheme2) => ({
matcherOptions: css({
minWidth: '404px',
}),
});