mirror of
https://github.com/grafana/grafana.git
synced 2025-02-13 00:55:47 -06:00
* 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
53 lines
1.7 KiB
TypeScript
53 lines
1.7 KiB
TypeScript
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
|
import { map } from 'lodash';
|
|
|
|
import { getInstanceState } from '../state/selectors';
|
|
import { initialVariablesState, VariablePayload, VariablesState } from '../state/types';
|
|
import { initialVariableModelState, IntervalVariableModel, VariableOption, VariableRefresh } from '../types';
|
|
|
|
export const initialIntervalVariableModelState: IntervalVariableModel = {
|
|
...initialVariableModelState,
|
|
type: 'interval',
|
|
auto_count: 30,
|
|
auto_min: '10s',
|
|
options: [],
|
|
auto: false,
|
|
query: '1m,10m,30m,1h,6h,12h,1d,7d,14d,30d',
|
|
refresh: VariableRefresh.onTimeRangeChanged,
|
|
current: {} as VariableOption,
|
|
};
|
|
|
|
export const intervalVariableSlice = createSlice({
|
|
name: 'templating/interval',
|
|
initialState: initialVariablesState,
|
|
reducers: {
|
|
createIntervalOptions: (state: VariablesState, action: PayloadAction<VariablePayload>) => {
|
|
const instanceState = getInstanceState(state, action.payload.id);
|
|
if (instanceState.type !== 'interval') {
|
|
return;
|
|
}
|
|
const options: VariableOption[] = map(instanceState.query.match(/(["'])(.*?)\1|\w+/g), (text) => {
|
|
text = text.replace(/["']+/g, '');
|
|
return { text: text.trim(), value: text.trim(), selected: false };
|
|
});
|
|
|
|
if (instanceState.auto) {
|
|
// add auto option if missing
|
|
if (options.length && options[0].text !== 'auto') {
|
|
options.unshift({
|
|
text: 'auto',
|
|
value: '$__auto_interval_' + instanceState.name,
|
|
selected: false,
|
|
});
|
|
}
|
|
}
|
|
|
|
instanceState.options = options;
|
|
},
|
|
},
|
|
});
|
|
|
|
export const intervalVariableReducer = intervalVariableSlice.reducer;
|
|
|
|
export const { createIntervalOptions } = intervalVariableSlice.actions;
|