import { useState } from 'react';
import { selectors } from '@grafana/e2e-selectors';
import { t, Trans } from '@grafana/i18n';
import { QueryVariable } from '@grafana/scenes';
import { Field, Stack, Switch } from '@grafana/ui';
import { VariableLegend } from 'app/features/dashboard-scene/settings/variables/components/VariableLegend';
import { VariableOptionsInput } from 'app/features/dashboard-scene/settings/variables/components/VariableOptionsInput';
import { VariableSelectField } from 'app/features/dashboard-scene/settings/variables/components/VariableSelectField';
export type StaticOptionsType = QueryVariable['state']['staticOptions'];
export type StaticOptionsOrderType = QueryVariable['state']['staticOptionsOrder'];
interface QueryVariableStaticOptionsProps {
staticOptions: StaticOptionsType;
staticOptionsOrder: StaticOptionsOrderType;
onStaticOptionsChange: (staticOptions: StaticOptionsType) => void;
onStaticOptionsOrderChange: (staticOptionsOrder: StaticOptionsOrderType) => void;
}
const SORT_OPTIONS = [
{ label: 'Before query values', value: 'before' },
{ label: 'After query values', value: 'after' },
{ label: 'Sorted with query values', value: 'sorted' },
];
export function QueryVariableStaticOptions(props: QueryVariableStaticOptionsProps) {
const { staticOptions, onStaticOptionsChange, staticOptionsOrder, onStaticOptionsOrderChange } = props;
const value = SORT_OPTIONS.find((o) => o.value === staticOptionsOrder) ?? SORT_OPTIONS[0];
const [areStaticOptionsEnabled, setAreStaticOptionsEnabled] = useState(!!staticOptions?.length);
return (
<>
Static options
<>
{
if (e.currentTarget.checked) {
setAreStaticOptionsEnabled(true);
} else {
setAreStaticOptionsEnabled(false);
if (!!staticOptions?.length) {
onStaticOptionsChange(undefined);
}
}
}}
/>
{areStaticOptionsEnabled && (
)}
>
{areStaticOptionsEnabled && (
onStaticOptionsOrderChange(opt.value)}
testId={
selectors.pages.Dashboard.Settings.Variables.Edit.QueryVariable.queryOptionsStaticOptionsOrderDropdown
}
width={25}
/>
)}
>
);
}