Skip to content

Commit

Permalink
Fix suggestions dropdown for query input (elastic#80990)
Browse files Browse the repository at this point in the history
* Fix suggestions

* Increase timeout

* Use abort controller

* Update abort controller

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
sulemanof and kibanamachine committed Oct 26, 2020
1 parent 27163f7 commit c07faa9
Showing 1 changed file with 18 additions and 7 deletions.
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 () => {
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

0 comments on commit c07faa9

Please sign in to comment.