React UI: Simplify query history setting code (#6225)

The metric names only get loaded once initially, so there is no reason
to mix them up with the handling of ongoing query history.

Signed-off-by: Julius Volz <julius.volz@gmail.com>
This commit is contained in:
Julius Volz 2019-10-26 21:17:24 +02:00 committed by GitHub
parent dab87ca281
commit 9698262b63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -50,7 +50,7 @@ class PanelList extends Component<any, PanelListState> {
}
})
.then(json => {
this.setMetrics(json.data);
this.setState({metricNames: json.data});
})
.catch(error => this.setState({ fetchMetricsError: error.message }));
@ -87,18 +87,17 @@ class PanelList extends Component<any, PanelListState> {
toggleQueryHistory = (e: ChangeEvent<HTMLInputElement>) => {
localStorage.setItem('enable-query-history', `${e.target.checked}`);
this.setMetrics();
this.updatePastQueries();
}
setMetrics = (metricNames: string[] = this.state.metricNames) => {
updatePastQueries = () => {
this.setState({
pastQueries: this.isHistoryEnabled() ? this.getHistoryItems() : [],
metricNames
pastQueries: this.isHistoryEnabled() ? this.getHistoryItems() : []
});
}
handleQueryHistory = (query: string) => {
const isSimpleMetric = this.state.metricNames.indexOf(query) !== -1;
const isSimpleMetric = this.state.metricNames.indexOf(query) !== -1;
if (isSimpleMetric || !query.length) {
return;
}
@ -107,7 +106,7 @@ class PanelList extends Component<any, PanelListState> {
return metric === query ? acc : [...acc, metric]; // Prevent adding query twice.
}, [query]);
localStorage.setItem('history', JSON.stringify(extendedItems.slice(0, 50)));
this.setMetrics();
this.updatePastQueries();
}
getKey(): string {