From c1e1741b71ed03cf800cd2dad6a235696452559b Mon Sep 17 00:00:00 2001 From: sethburkart123 Date: Thu, 31 Oct 2024 16:54:02 +1100 Subject: [PATCH] feat: add fuse.js to store search --- package.json | 1 + .../components/store/Backgrounds.svelte | 33 +++++++++++------ .../themes/BackgroundSelector.svelte | 35 +++++++++++++------ .../hooks/BackgroundUpdates.ts | 29 +++++++++++++++ 4 files changed, 77 insertions(+), 21 deletions(-) create mode 100644 src/svelte-interface/hooks/BackgroundUpdates.ts diff --git a/package.json b/package.json index 71e06735..e6b45d1d 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,7 @@ "codemirror": "^6.0.1", "color": "^4.2.3", "dompurify": "^3.1.6", + "fuse.js": "^7.0.0", "idb": "^8.0.0", "kolorist": "^1.8.0", "localforage": "^1.10.0", diff --git a/src/svelte-interface/components/store/Backgrounds.svelte b/src/svelte-interface/components/store/Backgrounds.svelte index dffcfba6..6ffabd2f 100644 --- a/src/svelte-interface/components/store/Backgrounds.svelte +++ b/src/svelte-interface/components/store/Backgrounds.svelte @@ -3,6 +3,8 @@ import { setTheme } from '@/seqta/ui/themes/setTheme'; import Spinner from '../Spinner.svelte'; import { settingsState } from '@/seqta/utils/listeners/SettingsState' + import Fuse from 'fuse.js'; + import { backgroundUpdates } from '@/svelte-interface/hooks/BackgroundUpdates' type Background = { id: string; category: string; type: string; lowResUrl: string; highResUrl: string; name: string; description: string; featured?: boolean }; let { searchTerm } = $props<{ searchTerm: string }>(); @@ -21,6 +23,14 @@ let activeTab = $state<'all' | 'installed' | 'photos' | 'videos'>('all'); let sortBy = $state<'newest' | 'popular' | 'name'>('newest'); + // Add Fuse.js options + const fuseOptions = { + keys: ['name', 'description'], + threshold: 0.4, + ignoreLocation: true + }; + let fuse: Fuse; + // Existing functions const loadStore = async () => { try { @@ -31,7 +41,7 @@ } const data = await response.json(); backgrounds = data.backgrounds; - console.log(data.backgrounds); + fuse = new Fuse(backgrounds, fuseOptions); debugInfo = `Loaded ${backgrounds.length} backgrounds`; await loadSavedBackgrounds(); } catch (e) { @@ -60,15 +70,20 @@ // Derived states let filteredBackgrounds = $derived((() => { - let filtered = backgrounds.filter((bg: Background) => { - const matchesCategory = selectedCategory === 'All' + let filtered = backgrounds; + + // Use Fuse.js search if there's a search term + if (searchTerm.trim()) { + filtered = fuse?.search(searchTerm).map((result: any) => result.item) ?? []; + } + + // Apply category filtering + filtered = filtered.filter((bg: Background) => { + return selectedCategory === 'All' ? true : selectedCategory === 'Featured' ? bg.featured : bg.category === selectedCategory; - const matchesSearch = bg.name.toLowerCase().includes(searchTerm.toLowerCase()) || - bg.description.toLowerCase().includes(searchTerm.toLowerCase()); - return matchesCategory && matchesSearch; }); // Apply sorting @@ -133,6 +148,7 @@ installingBackgrounds = new Set(installingBackgrounds).add(background.id); try { await saveBackgroundFromUrl(background.highResUrl, background.id, background.type); + backgroundUpdates.triggerUpdate(); } finally { installingBackgrounds = new Set(installingBackgrounds); installingBackgrounds.delete(background.id); @@ -191,7 +207,7 @@
-

Explore Backgrounds

+

Explore Backgrounds {searchTerm ? `- "${searchTerm}"` : ''}