import React, { PureComponent } from 'react'; import { connect, ConnectedProps } from 'react-redux'; import { DataSourceRef, SelectableValue } from '@grafana/data'; import { Alert, InlineFieldRow, VerticalGroup } from '@grafana/ui'; import { StoreState } from 'app/types'; import { VariableSectionHeader } from '../editor/VariableSectionHeader'; import { VariableSelectField } from '../editor/VariableSelectField'; import { initialVariableEditorState } from '../editor/reducer'; import { getAdhocVariableEditorState } from '../editor/selectors'; import { VariableEditorProps } from '../editor/types'; import { getVariablesState } from '../state/selectors'; import { AdHocVariableModel } from '../types'; import { toKeyedVariableIdentifier } from '../utils'; import { changeVariableDatasource, initAdHocVariableEditor } from './actions'; const mapStateToProps = (state: StoreState, ownProps: OwnProps) => { const { rootStateKey } = ownProps.variable; if (!rootStateKey) { console.error('AdHocVariableEditor: variable has no rootStateKey'); return { extended: getAdhocVariableEditorState(initialVariableEditorState), }; } const { editor } = getVariablesState(rootStateKey, state); return { extended: getAdhocVariableEditorState(editor), }; }; const mapDispatchToProps = { initAdHocVariableEditor, changeVariableDatasource, }; const connector = connect(mapStateToProps, mapDispatchToProps); export interface OwnProps extends VariableEditorProps {} type Props = OwnProps & ConnectedProps; export class AdHocVariableEditorUnConnected extends PureComponent { componentDidMount() { const { rootStateKey } = this.props.variable; if (!rootStateKey) { console.error('AdHocVariableEditor: variable has no rootStateKey'); return; } this.props.initAdHocVariableEditor(rootStateKey); } onDatasourceChanged = (option: SelectableValue) => { this.props.changeVariableDatasource(toKeyedVariableIdentifier(this.props.variable), option.value); }; render() { const { variable, extended } = this.props; const dataSources = extended?.dataSources ?? []; const infoText = extended?.infoText ?? null; const options = dataSources.map((ds) => ({ label: ds.text, value: ds.value })); const value = options.find((o) => o.value?.uid === variable.datasource?.uid) ?? options[0]; return ( {infoText ? : null} ); } } export const AdHocVariableEditor = connector(AdHocVariableEditorUnConnected);