// Libraries import React, { PureComponent } from 'react'; // Components import { HorizontalGroup, Select } from '@grafana/ui'; import { DataSourceInstanceSettings, SelectableValue } from '@grafana/data'; import { selectors } from '@grafana/e2e-selectors'; import { isUnsignedPluginSignature, PluginSignatureBadge } from '../../../features/plugins/PluginSignatureBadge'; import { getDataSourceSrv } from '@grafana/runtime'; export interface Props { onChange: (ds: DataSourceInstanceSettings) => void; current: string | null; hideTextValue?: boolean; onBlur?: () => void; autoFocus?: boolean; openMenuOnFocus?: boolean; placeholder?: string; tracing?: boolean; mixed?: boolean; dashboard?: boolean; metrics?: boolean; annotations?: boolean; variables?: boolean; pluginId?: string; noDefault?: boolean; } export interface State { error?: string; } export class DataSourcePicker extends PureComponent { dataSourceSrv = getDataSourceSrv(); static defaultProps: Partial = { autoFocus: false, openMenuOnFocus: false, placeholder: 'Select datasource', }; state: State = {}; constructor(props: Props) { super(props); } componentDidMount() { const { current } = this.props; const dsSettings = this.dataSourceSrv.getInstanceSettings(current); if (!dsSettings) { this.setState({ error: 'Could not find data source ' + current }); } } onChange = (item: SelectableValue) => { const dsSettings = this.dataSourceSrv.getInstanceSettings(item.value); if (dsSettings) { this.props.onChange(dsSettings); this.setState({ error: undefined }); } }; private getCurrentValue() { const { current, hideTextValue, noDefault } = this.props; if (!current && noDefault) { return null; } const ds = this.dataSourceSrv.getInstanceSettings(current); if (ds) { return { label: ds.name.substr(0, 37), value: ds.name, imgUrl: ds.meta.info.logos.small, hideText: hideTextValue, meta: ds.meta, }; } return { label: (current ?? 'no name') + ' - not found', value: current, imgUrl: '', hideText: hideTextValue, }; } getDataSourceOptions() { const { tracing, metrics, mixed, dashboard, variables, annotations, pluginId } = this.props; const options = this.dataSourceSrv .getList({ tracing, metrics, dashboard, mixed, variables, annotations, pluginId, }) .map((ds) => ({ value: ds.name, label: `${ds.name}${ds.isDefault ? ' (default)' : ''}`, imgUrl: ds.meta.info.logos.small, meta: ds.meta, })); return options; } render() { const { autoFocus, onBlur, openMenuOnFocus, placeholder } = this.props; const { error } = this.state; const options = this.getDataSourceOptions(); const value = this.getCurrentValue(); return (