Files
Josh Hunt 081d6e9d3e Typed variables pt5: Remove generics from getInstanceState (#53018)
* wip

* make diff easier to read

* Update template_srv getVariables to return new TypedVariableModel

* update VariableType to use the type from TypedVariableModel

* tidy things up

* Chore: Use type-accurate mock variables in tests

* Chore: Type VariableState to use TypedVariableModel

* fix typo

* remove type assertion from template_srv.getVariables

* use typescript/no-redeclare for compatibility with ts overloads

* remove generics from getVariable() and overload it to only return undefined based on arguments

* update usages of getVariable()

* Remove generic from getInstanceState

* update usages of getInstanceState

* fix lint
2022-08-10 16:06:49 +01:00

88 lines
2.5 KiB
TypeScript

import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { DataSourceInstanceSettings } from '@grafana/data';
import { ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE } from '../constants';
import { getInstanceState } from '../state/selectors';
import { initialVariablesState, VariablePayload, VariablesState } from '../state/types';
import { DataSourceVariableModel, initialVariableModelState, VariableOption, VariableRefresh } from '../types';
export const initialDataSourceVariableModelState: DataSourceVariableModel = {
...initialVariableModelState,
type: 'datasource',
current: {} as VariableOption,
regex: '',
options: [],
query: '',
multi: false,
includeAll: false,
refresh: VariableRefresh.onDashboardLoad,
};
export const dataSourceVariableSlice = createSlice({
name: 'templating/datasource',
initialState: initialVariablesState,
reducers: {
createDataSourceOptions: (
state: VariablesState,
action: PayloadAction<VariablePayload<{ sources: DataSourceInstanceSettings[]; regex: RegExp | undefined }>>
) => {
const { sources, regex } = action.payload.data;
const options: VariableOption[] = [];
const instanceState = getInstanceState(state, action.payload.id);
if (instanceState.type !== 'datasource') {
return;
}
for (let i = 0; i < sources.length; i++) {
const source = sources[i];
// must match on type
if (source.meta.id !== instanceState.query) {
continue;
}
if (isValid(source, regex)) {
options.push({ text: source.name, value: source.name, selected: false });
}
if (isDefault(source, regex)) {
options.push({ text: 'default', value: 'default', selected: false });
}
}
if (options.length === 0) {
options.push({ text: 'No data sources found', value: '', selected: false });
}
if (instanceState.includeAll) {
options.unshift({ text: ALL_VARIABLE_TEXT, value: ALL_VARIABLE_VALUE, selected: false });
}
instanceState.options = options;
},
},
});
function isValid(source: DataSourceInstanceSettings, regex?: RegExp) {
if (!regex) {
return true;
}
return regex.exec(source.name);
}
function isDefault(source: DataSourceInstanceSettings, regex?: RegExp) {
if (!source.isDefault) {
return false;
}
if (!regex) {
return true;
}
return regex.exec('default');
}
export const dataSourceVariableReducer = dataSourceVariableSlice.reducer;
export const { createDataSourceOptions } = dataSourceVariableSlice.actions;