mirror of
https://github.com/grafana/grafana.git
synced 2025-02-20 11:48:34 -06:00
* Add and configure eslint-plugin-import * Fix the lint:ts npm command * Autofix + prettier all the files * Manually fix remaining files * Move jquery code in jest-setup to external file to safely reorder imports * Resolve issue caused by circular dependencies within Prometheus * Update .betterer.results * Fix missing // @ts-ignore * ignore iconBundle.ts * Fix missing // @ts-ignore
65 lines
1.8 KiB
TypeScript
65 lines
1.8 KiB
TypeScript
import { PayloadAction } from '@reduxjs/toolkit';
|
|
import { AnyAction } from 'redux';
|
|
|
|
import { toStateKey } from '../utils';
|
|
|
|
import { getTemplatingReducers, TemplatingState } from './reducers';
|
|
import { variablesInitTransaction } from './transactionReducer';
|
|
|
|
export interface KeyedVariablesState {
|
|
lastKey?: string;
|
|
keys: Record<string, TemplatingState>;
|
|
}
|
|
|
|
export const initialKeyedVariablesState: KeyedVariablesState = { keys: {} };
|
|
|
|
export interface KeyedAction {
|
|
key: string;
|
|
action: PayloadAction<any>;
|
|
}
|
|
|
|
const keyedAction = (payload: KeyedAction) => ({
|
|
type: `templating/keyed/${payload.action.type.replace(/^templating\//, '')}`,
|
|
payload,
|
|
});
|
|
|
|
export function toKeyedAction(key: string, action: PayloadAction<any>): PayloadAction<KeyedAction> {
|
|
const keyAsString = toStateKey(key);
|
|
return keyedAction({ key: keyAsString, action });
|
|
}
|
|
|
|
const isKeyedAction = (action: AnyAction): action is PayloadAction<KeyedAction> => {
|
|
return (
|
|
typeof action.type === 'string' &&
|
|
action.type.startsWith('templating/keyed') &&
|
|
'payload' in action &&
|
|
typeof action.payload.key === 'string'
|
|
);
|
|
};
|
|
|
|
export function keyedVariablesReducer(state = initialKeyedVariablesState, outerAction: AnyAction): KeyedVariablesState {
|
|
if (isKeyedAction(outerAction)) {
|
|
const { key, action } = outerAction.payload;
|
|
const stringKey = toStateKey(key);
|
|
const lastKey = variablesInitTransaction.match(action) ? stringKey : state.lastKey;
|
|
const templatingReducers = getTemplatingReducers();
|
|
const prevKeyState = state.keys[stringKey];
|
|
const nextKeyState = templatingReducers(prevKeyState, action);
|
|
|
|
return {
|
|
...state,
|
|
lastKey,
|
|
keys: {
|
|
...state.keys,
|
|
[stringKey]: nextKeyState,
|
|
},
|
|
};
|
|
}
|
|
|
|
return state;
|
|
}
|
|
|
|
export default {
|
|
templating: keyedVariablesReducer,
|
|
};
|