From 1ad43b962b5b74ba911f7d7344583fc9f3a4d316 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 25 Mar 2024 20:46:17 -0500 Subject: [PATCH] dedup and limit history --- app/src/components/queryEditor.tsx | 10 +++++++--- shared-dom/src/language/queryCompletion.ts | 6 +++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/src/components/queryEditor.tsx b/app/src/components/queryEditor.tsx index 8b1a563a..78eb0b79 100644 --- a/app/src/components/queryEditor.tsx +++ b/app/src/components/queryEditor.tsx @@ -142,13 +142,17 @@ function createEditorState( } function getHistory() { - return JSON.parse(localStorage.getItem('queryHistory') ?? '[]') as string[] + const array = JSON.parse( + localStorage.getItem('queryHistory') ?? '[]', + ) as string[] + return new Set(array) } function appendHistory(value: string) { const history = getHistory() - history.push(value) - localStorage.setItem('queryHistory', JSON.stringify(history)) + history.delete(value) // used to reorder and put `value` at the bottom if it's already in the set + history.add(value) + localStorage.setItem('queryHistory', JSON.stringify([...history].slice(-100))) } const queryLanguage = LRLanguage.define({ diff --git a/shared-dom/src/language/queryCompletion.ts b/shared-dom/src/language/queryCompletion.ts index 3cd056d6..431cf74d 100644 --- a/shared-dom/src/language/queryCompletion.ts +++ b/shared-dom/src/language/queryCompletion.ts @@ -11,8 +11,8 @@ import { Tag, } from './queryParser.terms' -function buildHistory(getHistory: () => string[]) { - return getHistory().map( +function buildHistory(getHistory: () => Set) { + return Array.from(getHistory()).map( (label) => ({ label, @@ -23,7 +23,7 @@ function buildHistory(getHistory: () => string[]) { export const queryCompletion: (_: { getTags: () => Promise - getHistory: () => string[] + getHistory: () => Set }) => CompletionSource = ({ getTags, getHistory }) => async (context) => {