[sceneVariablesSetToVariables]: Manually check variable type, instead of instanceof (#78407)

Check type instead of instanceof, manually type variable
This commit is contained in:
Javier Ruiz 2023-11-21 10:40:30 +01:00 committed by GitHub
parent eae0d8b12d
commit ee42f41e0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,95 +13,106 @@ import { getIntervalsQueryFromNewIntervalModel } from '../utils/utils';
export function sceneVariablesSetToVariables(set: SceneVariables) { export function sceneVariablesSetToVariables(set: SceneVariables) {
const variables: VariableModel[] = []; const variables: VariableModel[] = [];
for (const variable of set.state.variables) { for (const variable of set.state.variables) {
const type = variable.state.type;
const commonProperties = { const commonProperties = {
name: variable.state.name, name: variable.state.name,
label: variable.state.label, label: variable.state.label,
description: variable.state.description, description: variable.state.description,
skipUrlSync: Boolean(variable.state.skipUrlSync), skipUrlSync: Boolean(variable.state.skipUrlSync),
hide: variable.state.hide || VariableHide.dontHide, hide: variable.state.hide || VariableHide.dontHide,
type: variable.state.type, type,
}; };
if (variable instanceof QueryVariable) { if (type === 'query') {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const queryVariable = variable as QueryVariable;
variables.push({ variables.push({
...commonProperties, ...commonProperties,
current: { current: {
// @ts-expect-error // @ts-expect-error
value: variable.state.value, value: queryVariable.state.value,
// @ts-expect-error // @ts-expect-error
text: variable.state.text, text: queryVariable.state.text,
}, },
options: [], options: [],
query: variable.state.query, query: queryVariable.state.query,
datasource: variable.state.datasource, datasource: queryVariable.state.datasource,
sort: variable.state.sort, sort: queryVariable.state.sort,
refresh: variable.state.refresh, refresh: queryVariable.state.refresh,
regex: variable.state.regex, regex: queryVariable.state.regex,
allValue: variable.state.allValue, allValue: queryVariable.state.allValue,
includeAll: variable.state.includeAll, includeAll: queryVariable.state.includeAll,
multi: variable.state.isMulti, multi: queryVariable.state.isMulti,
skipUrlSync: variable.state.skipUrlSync, skipUrlSync: queryVariable.state.skipUrlSync,
hide: variable.state.hide || VariableHide.dontHide, hide: queryVariable.state.hide || VariableHide.dontHide,
}); });
} else if (variable instanceof CustomVariable) { } else if (type === 'custom') {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const customVariable = variable as CustomVariable;
variables.push({ variables.push({
...commonProperties, ...commonProperties,
current: { current: {
// @ts-expect-error // @ts-expect-error
text: variable.state.value, text: customVariable.state.value,
// @ts-expect-error // @ts-expect-error
value: variable.state.value, value: customVariable.state.value,
}, },
options: [], options: [],
query: variable.state.query, query: customVariable.state.query,
multi: variable.state.isMulti, multi: customVariable.state.isMulti,
allValue: variable.state.allValue, allValue: customVariable.state.allValue,
includeAll: variable.state.includeAll, includeAll: customVariable.state.includeAll,
}); });
} else if (variable instanceof DataSourceVariable) { } else if (type === 'datasource') {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const datasourceVariable = variable as DataSourceVariable;
variables.push({ variables.push({
...commonProperties, ...commonProperties,
current: { current: {
// @ts-expect-error // @ts-expect-error
value: variable.state.value, value: datasourceVariable.state.value,
// @ts-expect-error // @ts-expect-error
text: variable.state.text, text: datasourceVariable.state.text,
}, },
options: [], options: [],
regex: variable.state.regex, regex: datasourceVariable.state.regex,
refresh: VariableRefresh.onDashboardLoad, refresh: VariableRefresh.onDashboardLoad,
query: variable.state.pluginId, query: datasourceVariable.state.pluginId,
multi: variable.state.isMulti, multi: datasourceVariable.state.isMulti,
allValue: variable.state.allValue, allValue: datasourceVariable.state.allValue,
includeAll: variable.state.includeAll, includeAll: datasourceVariable.state.includeAll,
}); });
} else if (variable instanceof ConstantVariable) { } else if (type === 'constant') {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const constantVariable = variable as ConstantVariable;
variables.push({ variables.push({
...commonProperties, ...commonProperties,
current: { current: {
// @ts-expect-error // @ts-expect-error
value: variable.state.value, value: constantVariable.state.value,
// @ts-expect-error // @ts-expect-error
text: variable.state.value, text: constantVariable.state.value,
}, },
// @ts-expect-error // @ts-expect-error
query: variable.state.value, query: constantVariable.state.value,
hide: VariableHide.hideVariable, hide: VariableHide.hideVariable,
}); });
} else if (variable instanceof IntervalVariable) { } else if (type === 'interval') {
const intervals = getIntervalsQueryFromNewIntervalModel(variable.state.intervals); // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const intervalVariable = variable as IntervalVariable;
const intervals = getIntervalsQueryFromNewIntervalModel(intervalVariable.state.intervals);
variables.push({ variables.push({
...commonProperties, ...commonProperties,
current: { current: {
text: variable.state.value, text: intervalVariable.state.value,
value: variable.state.value, value: intervalVariable.state.value,
}, },
query: intervals, query: intervals,
hide: VariableHide.hideVariable, hide: VariableHide.hideVariable,
refresh: variable.state.refresh, refresh: intervalVariable.state.refresh,
// @ts-expect-error ?? how to fix this without adding the ts-expect-error // @ts-expect-error ?? how to fix this without adding the ts-expect-error
auto: variable.state.autoEnabled, auto: intervalVariable.state.autoEnabled,
auto_min: variable.state.autoMinInterval, auto_min: intervalVariable.state.autoMinInterval,
auto_count: variable.state.autoStepCount, auto_count: intervalVariable.state.autoStepCount,
}); });
} else { } else {
throw new Error('Unsupported variable type'); throw new Error('Unsupported variable type');