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