DashboardPickerByID: Add option to exclude dashboards (#49211)

This commit is contained in:
Alex Khomenko 2022-05-19 16:47:55 +03:00 committed by GitHub
parent f7f2253072
commit dacfdc0cde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,6 +23,8 @@ interface Props {
disabled?: boolean; disabled?: boolean;
id?: string; id?: string;
optionLabel?: string; optionLabel?: string;
/** List of dashboard UIDs to be excluded from the select options */
excludedDashboards?: string[];
} }
/** /**
@ -37,8 +39,9 @@ export const DashboardPickerByID: FC<Props> = ({
disabled, disabled,
id, id,
optionLabel = 'label', optionLabel = 'label',
excludedDashboards,
}) => { }) => {
const debouncedSearch = debounce((query: string) => getDashboards(query || '', optionLabel), 300); const debouncedSearch = debounce((query: string) => getDashboards(query || '', optionLabel, excludedDashboards), 300);
const option = value ? { value, [optionLabel]: value[optionLabel] } : undefined; const option = value ? { value, [optionLabel]: value[optionLabel] } : undefined;
const onChange = (item: SelectableValue<DashboardPickerItem>) => { const onChange = (item: SelectableValue<DashboardPickerItem>) => {
propsOnChange(item?.value); propsOnChange(item?.value);
@ -62,9 +65,13 @@ export const DashboardPickerByID: FC<Props> = ({
); );
}; };
async function getDashboards(query: string, label: string): Promise<Array<SelectableValue<DashboardPickerItem>>> { async function getDashboards(
query: string,
label: string,
excludedDashboards?: string[]
): Promise<Array<SelectableValue<DashboardPickerItem>>> {
const result = await backendSrv.search({ type: 'dash-db', query, limit: 100 }); const result = await backendSrv.search({ type: 'dash-db', query, limit: 100 });
return result.map(({ id, uid = '', title, folderTitle }) => { const dashboards = result.map(({ id, uid = '', title, folderTitle }) => {
const value: DashboardPickerItem = { const value: DashboardPickerItem = {
id, id,
uid, uid,
@ -73,4 +80,10 @@ async function getDashboards(query: string, label: string): Promise<Array<Select
return { value, [label]: value[label] }; return { value, [label]: value[label] };
}); });
if (excludedDashboards) {
return dashboards.filter(({ value }) => !excludedDashboards.includes(value.uid as string));
}
return dashboards;
} }