Files
Oscar Kilhed a7e74f6d6d DataSourcePicker: Refactor and collapse the DataSourceDropdown components (#66820)
* clean up the components and convert to functional components

* Create hooks for getting DS

* remove focus style override from input

---------

Co-authored-by: Ivan Ortega <ivanortegaalba@gmail.com>
2023-04-20 12:11:13 +02:00

61 lines
1.5 KiB
TypeScript

import { DataSourceInstanceSettings, DataSourceJsonData, DataSourceRef } from '@grafana/data';
import { GetDataSourceListFilters, getDataSourceSrv } from '@grafana/runtime';
export function isDataSourceMatch(
ds: DataSourceInstanceSettings | undefined,
current: string | DataSourceInstanceSettings | DataSourceRef | null | undefined
): boolean | undefined {
if (!ds) {
return false;
}
if (!current) {
return false;
}
if (typeof current === 'string') {
return ds.uid === current;
}
return ds.uid === current.uid;
}
export function dataSourceLabel(
dataSource: DataSourceInstanceSettings<DataSourceJsonData> | string | DataSourceRef | null | undefined
) {
if (!dataSource) {
return 'Unknown';
}
if (typeof dataSource === 'string') {
return `${dataSource} - not found`;
}
if ('name' in dataSource) {
return dataSource.name;
}
if (dataSource.uid) {
return `${dataSource.uid} - not found`;
}
return 'Unknown';
}
export function useGetDatasources(filters: GetDataSourceListFilters) {
const dataSourceSrv = getDataSourceSrv();
return dataSourceSrv.getList(filters);
}
export function useGetDatasource(dataSource: string | DataSourceRef | DataSourceInstanceSettings | null | undefined) {
const dataSourceSrv = getDataSourceSrv();
if (!dataSource) {
return undefined;
}
if (typeof dataSource === 'string') {
return dataSourceSrv.getInstanceSettings(dataSource);
}
return dataSourceSrv.getInstanceSettings(dataSource);
}