grafana/public/app/features/variables/editor/selectors.ts
Josh Hunt 1c6eca09bb
Variables: Explicitly type variable editor extended state (#44749)
* Narrow Variable editor state using selector functions

 - Explicitly type "extended" editor state in editor/reducter.ts using a union
 - Create selectors to narrow the types, using unique properties from each extended state to discriminate the union
 - Update DataSourceVariableEditor to use new style of redux connector
 - Update variable editor components to use new selectors

* fix tests

* Make adhoc variable infoText optional, because it is!

* Add AdHocVariableEditor tests

* DataSourceVariableEditor tests

* comment

* reset

* Wrote tests for selectors \(that actually caught a bug, whodathunkit)

* fix stray types and lint issues

* Rename selector functions
2022-02-08 09:31:42 +11:00

42 lines
1.2 KiB
TypeScript

import {
AdHocVariableEditorState,
DataSourceVariableEditorState,
QueryVariableEditorState,
VariableEditorState,
} from './reducer';
/**
* Narrows generic variable editor state down to specific Adhoc variable extended editor state
*/
export function getAdhocVariableEditorState(editorState: VariableEditorState): AdHocVariableEditorState | null {
if (editorState.extended && 'dataSources' in editorState.extended) {
return editorState.extended;
}
return null;
}
/**
* Narrows generic variable editor state down to specific Datasource variable extended editor state
*/
export function getDatasourceVariableEditorState(
editorState: VariableEditorState
): DataSourceVariableEditorState | null {
if (editorState.extended && 'dataSourceTypes' in editorState.extended) {
return editorState.extended;
}
return null;
}
/**
* Narrows generic variable editor state down to specific Query variable extended editor state
*/
export function getQueryVariableEditorState(editorState: VariableEditorState): QueryVariableEditorState | null {
if (editorState.extended && 'dataSource' in editorState.extended) {
return editorState.extended;
}
return null;
}