2020-03-10 04:16:56 -05:00
|
|
|
import React, { FC } from 'react';
|
2019-09-03 02:54:01 -05:00
|
|
|
import { debounce } from 'lodash';
|
2020-03-10 04:16:56 -05:00
|
|
|
import { useAsyncFn } from 'react-use';
|
2020-03-03 08:09:52 -06:00
|
|
|
import { SelectableValue } from '@grafana/data';
|
2020-04-02 03:57:35 -05:00
|
|
|
import { AsyncSelect } from '@grafana/ui';
|
2020-01-21 03:08:07 -06:00
|
|
|
import { backendSrv } from 'app/core/services/backend_srv';
|
2020-04-24 10:07:57 -05:00
|
|
|
import { DashboardSearchHit } from 'app/features/search/types';
|
|
|
|
import { DashboardDTO } from 'app/types';
|
2019-09-03 02:54:01 -05:00
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
onSelected: (dashboard: DashboardDTO) => void;
|
2020-03-10 04:16:56 -05:00
|
|
|
currentDashboard?: SelectableValue<number>;
|
2020-04-21 03:42:57 -05:00
|
|
|
width?: number;
|
2020-03-10 04:16:56 -05:00
|
|
|
isClearable?: boolean;
|
2020-03-17 05:07:40 -05:00
|
|
|
invalid?: boolean;
|
|
|
|
disabled?: boolean;
|
2019-09-03 02:54:01 -05:00
|
|
|
}
|
|
|
|
|
2020-03-10 04:16:56 -05:00
|
|
|
const getDashboards = (query = '') => {
|
|
|
|
return backendSrv.search({ type: 'dash-db', query }).then((result: DashboardSearchHit[]) => {
|
|
|
|
return result.map((item: DashboardSearchHit) => ({
|
|
|
|
id: item.id,
|
|
|
|
value: item.id,
|
|
|
|
label: `${item.folderTitle ? item.folderTitle : 'General'}/${item.title}`,
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-03-13 07:07:15 -05:00
|
|
|
export const DashboardPicker: FC<Props> = ({
|
|
|
|
onSelected,
|
|
|
|
currentDashboard,
|
2020-04-21 03:42:57 -05:00
|
|
|
width,
|
2020-03-13 07:07:15 -05:00
|
|
|
isClearable = false,
|
2020-03-17 05:07:40 -05:00
|
|
|
invalid,
|
|
|
|
disabled,
|
2020-03-13 07:07:15 -05:00
|
|
|
}) => {
|
2020-03-10 04:16:56 -05:00
|
|
|
const debouncedSearch = debounce(getDashboards, 300, {
|
|
|
|
leading: true,
|
|
|
|
trailing: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const [state, searchDashboards] = useAsyncFn(debouncedSearch, []);
|
|
|
|
|
|
|
|
return (
|
2020-04-02 03:57:35 -05:00
|
|
|
<AsyncSelect
|
2020-04-21 03:42:57 -05:00
|
|
|
width={width}
|
2020-03-10 04:16:56 -05:00
|
|
|
isLoading={state.loading}
|
|
|
|
isClearable={isClearable}
|
|
|
|
defaultOptions={true}
|
|
|
|
loadOptions={searchDashboards}
|
|
|
|
onChange={onSelected}
|
|
|
|
placeholder="Select dashboard"
|
|
|
|
noOptionsMessage="No dashboards found"
|
|
|
|
value={currentDashboard}
|
2020-03-17 05:07:40 -05:00
|
|
|
invalid={invalid}
|
|
|
|
disabled={disabled}
|
2020-03-10 04:16:56 -05:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|