Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix suggestions dropdown for query input #80990

Merged
merged 7 commits into from
Oct 26, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export default class QueryStringInputUI extends Component<Props, State> {

private persistedLog: PersistedLog | undefined;
private abortController?: AbortController;
private fetchIndexPatternsAbortController?: AbortController;
private services = this.props.kibana.services;
private componentIsUnmounting = false;
private queryBarInputDivRefInstance: RefObject<HTMLDivElement> = createRef();
Expand All @@ -119,24 +120,34 @@ export default class QueryStringInputUI extends Component<Props, State> {
return toUser(this.props.query.query);
};

private fetchIndexPatterns = async () => {
private fetchIndexPatterns = debounce(async () => {
alexwizp marked this conversation as resolved.
Show resolved Hide resolved
const stringPatterns = this.props.indexPatterns.filter(
(indexPattern) => typeof indexPattern === 'string'
) as string[];
const objectPatterns = this.props.indexPatterns.filter(
(indexPattern) => typeof indexPattern !== 'string'
) as IIndexPattern[];

// abort the previous fetch to avoid overriding with outdated data
// issue https://github.com/elastic/kibana/issues/80831
if (this.fetchIndexPatternsAbortController) this.fetchIndexPatternsAbortController.abort();
this.fetchIndexPatternsAbortController = new AbortController();
const currentAbortController = this.fetchIndexPatternsAbortController;

const objectPatternsFromStrings = (await fetchIndexPatterns(
this.services.savedObjects!.client,
stringPatterns,
this.services.uiSettings!
)) as IIndexPattern[];

this.setState({
indexPatterns: [...objectPatterns, ...objectPatternsFromStrings],
});
};
if (!currentAbortController.signal.aborted) {
this.setState({
indexPatterns: [...objectPatterns, ...objectPatternsFromStrings],
});

this.updateSuggestions();
}
}, 200);

private getSuggestions = async () => {
if (!this.inputRef) {
Expand Down Expand Up @@ -506,7 +517,7 @@ export default class QueryStringInputUI extends Component<Props, State> {
}

this.initPersistedLog();
this.fetchIndexPatterns().then(this.updateSuggestions);
this.fetchIndexPatterns();
this.handleListUpdate();

window.addEventListener('resize', this.handleAutoHeight);
Expand All @@ -525,7 +536,7 @@ export default class QueryStringInputUI extends Component<Props, State> {
this.initPersistedLog();

if (!isEqual(prevProps.indexPatterns, this.props.indexPatterns)) {
this.fetchIndexPatterns().then(this.updateSuggestions);
this.fetchIndexPatterns();
} else if (!isEqual(prevProps.query, this.props.query)) {
this.updateSuggestions();
}
Expand Down