Annotations: Improve tag search performance (#40567)

This commit is contained in:
Ashley Harrison
2021-10-18 15:00:46 +01:00
committed by GitHub
parent a9faab6b09
commit ec0fcbbf4b
@@ -2,6 +2,7 @@
import React, { FC } from 'react'; import React, { FC } from 'react';
import { css } from '@emotion/css'; import { css } from '@emotion/css';
import { components } from 'react-select'; import { components } from 'react-select';
import debounce from 'debounce-promise';
import { stylesFactory, useTheme, resetSelectStyles, Icon, AsyncMultiSelect } from '@grafana/ui'; import { stylesFactory, useTheme, resetSelectStyles, Icon, AsyncMultiSelect } from '@grafana/ui';
import { escapeStringForRegex, GrafanaTheme } from '@grafana/data'; import { escapeStringForRegex, GrafanaTheme } from '@grafana/data';
// Components // Components
@@ -47,16 +48,17 @@ export const TagFilter: FC<Props> = ({
const theme = useTheme(); const theme = useTheme();
const styles = getStyles(theme); const styles = getStyles(theme);
const onLoadOptions = (query: string) => { const onLoadOptions = async (query: string) => {
return tagOptions().then((options) => { const options = await tagOptions();
return options.map((option) => ({ return options.map((option) => ({
value: option.term, value: option.term,
label: option.term, label: option.term,
count: option.count, count: option.count,
})); }));
});
}; };
const debouncedLoadOptions = debounce(onLoadOptions, 300);
const onTagChange = (newTags: any[]) => { const onTagChange = (newTags: any[]) => {
// On remove with 1 item returns null, so we need to make sure it's an empty array in that case // On remove with 1 item returns null, so we need to make sure it's an empty array in that case
// https://github.com/JedWatson/react-select/issues/3632 // https://github.com/JedWatson/react-select/issues/3632
@@ -74,7 +76,7 @@ export const TagFilter: FC<Props> = ({
getOptionValue: (i: any) => i.value, getOptionValue: (i: any) => i.value,
inputId, inputId,
isMulti: true, isMulti: true,
loadOptions: onLoadOptions, loadOptions: debouncedLoadOptions,
loadingMessage: 'Loading...', loadingMessage: 'Loading...',
noOptionsMessage: 'No tags found', noOptionsMessage: 'No tags found',
onChange: onTagChange, onChange: onTagChange,