Files
grafana/public/app/features/dashboard-scene/settings/variables/components/AdHocVariableForm.tsx
Ashley Harrison b1b65faf02 Variables: Support static keys in AdHocFiltersVariable (#83157)
* initial start

* don't use getTagKeysProvider

* some cleanup

* undo kinds adjustment

* simplify

* remove async declaration

* add description and a couple of unit tests

* add transformSaveModelToScene test

* add tests for AdHocVariableForm

* add tests for AdHocFiltersVariableEditor

* update to defaultKeys

* fix snapshots

* update to 3.13.3
2024-03-18 16:12:00 +00:00

86 lines
2.7 KiB
TypeScript

import React, { useCallback } from 'react';
import { DataSourceInstanceSettings, MetricFindValue, readCSV } from '@grafana/data';
import { selectors } from '@grafana/e2e-selectors';
import { DataSourceRef } from '@grafana/schema';
import { Alert, CodeEditor, Field, Switch } from '@grafana/ui';
import { DataSourcePicker } from 'app/features/datasources/components/picker/DataSourcePicker';
import { VariableLegend } from './VariableLegend';
export interface AdHocVariableFormProps {
datasource?: DataSourceRef;
onDataSourceChange: (dsSettings: DataSourceInstanceSettings) => void;
infoText?: string;
defaultKeys?: MetricFindValue[];
onDefaultKeysChange?: (keys?: MetricFindValue[]) => void;
}
export function AdHocVariableForm({
datasource,
infoText,
onDataSourceChange,
onDefaultKeysChange,
defaultKeys,
}: AdHocVariableFormProps) {
const updateStaticKeys = useCallback(
(csvContent: string) => {
const df = readCSV('key,value\n' + csvContent)[0];
const options = [];
for (let i = 0; i < df.length; i++) {
options.push({ text: df.fields[0].values[i], value: df.fields[1].values[i] });
}
onDefaultKeysChange?.(options);
},
[onDefaultKeysChange]
);
return (
<>
<VariableLegend>Ad-hoc options</VariableLegend>
<Field label="Data source" htmlFor="data-source-picker">
<DataSourcePicker current={datasource} onChange={onDataSourceChange} width={30} variables={true} noDefault />
</Field>
{infoText ? (
<Alert
title={infoText}
severity="info"
data-testid={selectors.pages.Dashboard.Settings.Variables.Edit.AdHocFiltersVariable.infoText}
/>
) : null}
{onDefaultKeysChange && (
<>
<Field label="Use static key dimensions" description="Provide dimensions as CSV: dimensionName, dimensionId">
<Switch
data-testid={selectors.pages.Dashboard.Settings.Variables.Edit.AdHocFiltersVariable.modeToggle}
value={defaultKeys !== undefined}
onChange={(e) => {
if (defaultKeys === undefined) {
onDefaultKeysChange([]);
} else {
onDefaultKeysChange(undefined);
}
}}
/>
</Field>
{defaultKeys !== undefined && (
<CodeEditor
height={300}
language="csv"
value={defaultKeys.map((o) => `${o.text},${o.value}`).join('\n')}
onBlur={updateStaticKeys}
onSave={updateStaticKeys}
showMiniMap={false}
showLineNumbers={true}
/>
)}
</>
)}
</>
);
}