1import { StoreState } from '../../../types'; 2import { VariableModel } from '../types'; 3import { getState } from '../../../store/store'; 4import memoizeOne from 'memoize-one'; 5 6export const getVariable = <T extends VariableModel = VariableModel>( 7 id: string, 8 state: StoreState = getState(), 9 throwWhenMissing = true 10): T => { 11 if (!state.templating.variables[id]) { 12 if (throwWhenMissing) { 13 throw new Error(`Couldn't find variable with id:${id}`); 14 } 15 return (undefined as unknown) as T; 16 } 17 18 return state.templating.variables[id] as T; 19}; 20 21export const getFilteredVariables = (filter: (model: VariableModel) => boolean, state: StoreState = getState()) => { 22 return Object.values(state.templating.variables) 23 .filter(filter) 24 .sort((s1, s2) => s1.index - s2.index); 25}; 26 27export const getVariableWithName = (name: string, state: StoreState = getState()) => { 28 return getVariable(name, state, false); 29}; 30 31export const getVariables = (state: StoreState = getState()): VariableModel[] => { 32 const filter = (variable: VariableModel) => { 33 return variable.type !== 'system'; 34 }; 35 36 return getFilteredVariables(filter, state); 37}; 38 39export const getSubMenuVariables = memoizeOne((variables: Record<string, VariableModel>): VariableModel[] => { 40 return getVariables(getState()); 41}); 42 43export const getEditorVariables = (state: StoreState): VariableModel[] => { 44 return getVariables(state); 45}; 46 47export type GetVariables = typeof getVariables; 48 49export const getNewVariabelIndex = (state: StoreState = getState()): number => { 50 return Object.values(state.templating.variables).length; 51}; 52 53export function getVariablesIsDirty(state: StoreState = getState()): boolean { 54 return state.templating.transaction.isDirty; 55} 56