diff --git a/src/lib/components/input/search.svelte b/src/lib/components/input/search.svelte index 61c0567f..ccb16437 100644 --- a/src/lib/components/input/search.svelte +++ b/src/lib/components/input/search.svelte @@ -7,7 +7,7 @@ import { preventDefault } from '$lib/utils'; import { goto } from '$app/navigation'; import { fade, scale } from 'svelte/transition'; - import { ArrowLeftRight, Box, Key, SearchIcon, UserSearch } from 'lucide-svelte'; + import { ArrowLeftRight, Box, Key, SearchIcon, UserSearch, X } from 'lucide-svelte'; import { SearchHistory } from '$lib/state/search.svelte'; import Button from '$lib/components/button/button.svelte'; import { Stack } from '$lib/components/layout'; @@ -31,7 +31,7 @@ let selectedIndex: number | undefined = $state(); const searchHistory = new SearchHistory(); - const history = searchHistory.get(); + const history = $derived(searchHistory.get()); const searchType = $derived.by(() => { /* eslint-disable @typescript-eslint/no-unused-vars */ @@ -126,7 +126,7 @@ } if (selectedIndex !== undefined && event.key === 'Enter') { - goTosearchHistory(history[selectedIndex].result); + goToSearchHistory(history[selectedIndex].result); } } } @@ -144,7 +144,7 @@ searchValue = ''; } - function goTosearchHistory(url: string) { + function goToSearchHistory(url: string) { goto(url); closeSearch(); } @@ -214,49 +214,59 @@ {#if history.length}
-
+ {/each}
diff --git a/src/lib/state/search.svelte.ts b/src/lib/state/search.svelte.ts index 8081a751..1a038254 100644 --- a/src/lib/state/search.svelte.ts +++ b/src/lib/state/search.svelte.ts @@ -6,10 +6,9 @@ type SearchResult = { searchValue: string; }; -const maxHistoryLength = 10; - export class SearchHistory { - history = $state([]) as SearchResult[]; + private history = $state([]) as SearchResult[]; + maxHistoryLength = 10; storageKey = 'searchHistory'; constructor() { @@ -35,13 +34,16 @@ export class SearchHistory { add(s: SearchResult) { this.history.unshift(s); - if (history.length > maxHistoryLength) { - this.history.splice(maxHistoryLength); + if (history.length > this.maxHistoryLength) { + this.history.splice(this.maxHistoryLength); } this.saveHistory(); } - // remove() {} + remove(i: number) { + this.history.splice(i, 1); + this.saveHistory(); + } get(): SearchResult[] { return this.history;