Variables: allow {} type in VariableWithOptions.current (#64358)

This commit is contained in:
Simon Podlipsky
2023-06-16 14:31:12 +02:00
committed by GitHub
parent 68637059c4
commit 6f880b713a
7 changed files with 24 additions and 4 deletions
@@ -109,7 +109,7 @@ export interface VariableWithMultiSupport extends VariableWithOptions {
}
export interface VariableWithOptions extends BaseVariableModel {
current: VariableOption;
current: VariableOption | Record<string, never>;
options: VariableOption[];
query: string;
}
@@ -6,3 +6,7 @@
return acc;
}, {});
};
export const isEmptyObject = (value: unknown): value is Record<string, never> => {
return typeof value === 'object' && value !== null && Object.keys(value).length === 0;
};
@@ -12,6 +12,7 @@ import {
getActiveThreshold,
getDataSourceRef,
isDataSourceRef,
isEmptyObject,
MappingType,
PanelPlugin,
SpecialValueMatch,
@@ -584,6 +585,9 @@ export class DashboardMigrator {
continue;
}
const { multi, current } = variable;
if (isEmptyObject(current)) {
continue;
}
variable.current = alignCurrentWithMulti(current, multi);
}
}
@@ -1,5 +1,6 @@
import { debounce, trim } from 'lodash';
import { isEmptyObject } from '@grafana/data';
import { StoreState, ThunkDispatch, ThunkResult } from 'app/types';
import { variableAdapters } from '../../adapters';
@@ -87,6 +88,10 @@ export const filterOrSearchOptions = (
};
const setVariable = async (updated: VariableWithOptions) => {
if (isEmptyObject(updated.current)) {
return;
}
const adapter = variableAdapters.get(updated.type);
await adapter.setValue(updated, updated.current, true);
return;
@@ -4,6 +4,7 @@ import {
DataQuery,
getDataSourceRef,
isDataSourceRef,
isEmptyObject,
LoadingState,
TimeRange,
UrlQueryMap,
@@ -257,7 +258,7 @@ export const changeVariableMultiValue = (identifier: KeyedVariableIdentifier, mu
return (dispatch, getState) => {
const { rootStateKey: key } = identifier;
const variable = getVariable(identifier, getState());
if (!isMulti(variable)) {
if (!isMulti(variable) || isEmptyObject(variable.current)) {
return;
}
@@ -1,5 +1,6 @@
import React, { ChangeEvent, FocusEvent, KeyboardEvent, ReactElement, useCallback, useEffect, useState } from 'react';
import { isEmptyObject } from '@grafana/data';
import { Input } from '@grafana/ui';
import { t } from 'app/core/internationalization';
import { useDispatch } from 'app/types';
@@ -43,7 +44,7 @@ export function TextBoxVariablePicker({ variable, onVariableChange, readOnly }:
if (onVariableChange) {
onVariableChange({
...variable,
current: { ...variable.current, value: updatedValue },
current: isEmptyObject(variable.current) ? {} : { ...variable.current, value: updatedValue },
});
return;
}
@@ -48,7 +48,12 @@ export const isTemplateVariable = (templateSrv: TemplateSrv, string: string) =>
};
const isVariableOption = (
current: VariableOption | { value: UserProps } | { value: OrgProps } | { value: DashboardProps }
current:
| VariableOption
| Record<string, never>
| { value: UserProps }
| { value: OrgProps }
| { value: DashboardProps }
): current is VariableOption => {
return current.hasOwnProperty('value') && current.hasOwnProperty('text');
};