Skip to content

Commit

Permalink
[ES|QL] Small refactoring to ensure that the localstorage limit will …
Browse files Browse the repository at this point in the history
…always be respected (elastic#181415)

## Summary

Make the guard of 20 max queries in the local storage more robust.

This is just a refactoring of the implementation. In case anything goes
wrong, it makes sure that the queries will always be the maximum
allowed.
  • Loading branch information
stratoula authored Apr 25, 2024
1 parent 8e758d9 commit b4e0575
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions packages/kbn-text-based-editor/src/history_local_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,17 @@ export const updateCachedQueries = (
);
let allQueries = [...queriesToStore, ...newQueries];

if (allQueries.length === maxQueriesAllowed + 1) {
if (allQueries.length >= maxQueriesAllowed + 1) {
const sortedByDate = allQueries.sort((a, b) =>
sortDates(b?.startDateMilliseconds, a?.startDateMilliseconds)
);

// delete the last element
const toBeDeletedQuery = sortedByDate[maxQueriesAllowed];
cachedQueries.delete(toBeDeletedQuery.queryString);
allQueries = allQueries.filter((q) => {
return q.queryString !== toBeDeletedQuery.queryString;
// queries to store in the localstorage
allQueries = sortedByDate.slice(0, maxQueriesAllowed);
// clear and reset the queries in the cache
cachedQueries.clear();
allQueries.forEach((queryItem) => {
cachedQueries.set(queryItem.queryString, queryItem);
});
}
localStorage.setItem(QUERY_HISTORY_ITEM_KEY, JSON.stringify(allQueries));
Expand Down

0 comments on commit b4e0575

Please sign in to comment.