Skip to content

Commit

Permalink
dedup and limit history
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexErrant committed Mar 26, 2024
1 parent 1d12d0f commit 1ad43b9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
10 changes: 7 additions & 3 deletions app/src/components/queryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
6 changes: 3 additions & 3 deletions shared-dom/src/language/queryCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
Tag,
} from './queryParser.terms'

function buildHistory(getHistory: () => string[]) {
return getHistory().map(
function buildHistory(getHistory: () => Set<string>) {
return Array.from(getHistory()).map(
(label) =>
({
label,
Expand All @@ -23,7 +23,7 @@ function buildHistory(getHistory: () => string[]) {

export const queryCompletion: (_: {
getTags: () => Promise<string[]>
getHistory: () => string[]
getHistory: () => Set<string>
}) => CompletionSource =
({ getTags, getHistory }) =>
async (context) => {
Expand Down

0 comments on commit 1ad43b9

Please sign in to comment.