Help integrating microfuzz search library with solid
I want to integrate this library into my component
https://github.com/Nozbe/microfuzz
I have this code for it:
https://github.com/learn-anything/learn-anything/blob/main/app/packages/electron-web/src/components/SearchModal.tsx
GitHub
GitHub - Nozbe/microfuzz: A tiny, simple, fast JS fuzzy search library
A tiny, simple, fast JS fuzzy search library. Contribute to Nozbe/microfuzz development by creating an account on GitHub.
GitHub
learn-anything/app/packages/electron-web/src/components/SearchModal...
Organize world's knowledge, explore connections and curate learning paths - learn-anything/learn-anything
72 Replies
component is being rendered here: https://github.com/learn-anything/learn-anything/blob/main/app/packages/electron-web/src/App.tsx#L96
GitHub
learn-anything/app/packages/electron-web/src/App.tsx at main · lear...
Organize world's knowledge, explore connections and curate learning paths - learn-anything/learn-anything
https://github.com/learn-anything/learn-anything/blob/main/app/packages/electron-web/src/GlobalContext/user.ts#L24 it uses this store
GitHub
learn-anything/app/packages/electron-web/src/GlobalContext/user.ts ...
Organize world's knowledge, explore connections and curate learning paths - learn-anything/learn-anything
im basically trying to search over an array of strings that i pass
so
props.items
is array like thisthe problem is that i follow readme of this lib
https://github.com/Nozbe/microfuzz#using-microfuzz-plain-js
GitHub
GitHub - Nozbe/microfuzz: A tiny, simple, fast JS fuzzy search library
A tiny, simple, fast JS fuzzy search library. Contribute to Nozbe/microfuzz development by creating an account on GitHub.
and i thought i do it like this in solid
i get items as prop so i create local searchResults signal that will show filtered results
then fuzzySearch is created from
props.items
i tried with unwrap() toothen onInput of the input box it should show matches
but its 0 matches, not sure what im doing wrong on solid side
thought above would work
i also tried making fuzzySearch into a signal
library does work, just my solid integration is broken
you don't seem to be calling
searchResults
nor setSearchResults
anywhereyes but im not filtering anything
im trying to do matches
can't filter if i can't do matches
filteredItems is more appropriate name actually
results always shows []
is
props.items
an empty array at the time you call createFuzzySearch
?oh yea
strange though
ok i think its because store gets filled in async way
ok yea
i was just opening this modal too fast as i was testing
and store did not get filled with values
how can i make it work though im curious
if i render SearchModal instantly
before my store value of topics gets filled
i.e. it works with above
but if I do
this will take like 100ms to complete but by the time the comopnent already rendered
i need to put it in effect and make fuzzysearch signal?
this runs twice for example
first empty then with values
i thought maybe this
i would probably put
fuzzySearch
in a memo, and the search term in a signal
and the results in a memo as well
this?
yeah
search term in a signal results in a memo as wellthis i dont fully get
create a signal for the search term and update it on input
then create a memo for the search results that uses fuzzySearch
ok done
const searchResults = createMemo(() => fuzzySearch()(searchQuery()));
doesnt like that
oh wow
yours it likes
oh probably missing return
fuzzySearch
is a function that returns the search function
so you need to call it before calling it
i thought doing this effect
oh wow
ok that does work
thank you lots @otonashi9 ❤️
should this become a memo too?
im thinking about this loop
that's basically what
searchResults
is i guesslike it should loop over filteredResults
i think
which on default are props.items
its empty though on mount
so if the query is empty it should return the whole array?
yes
then just change the memo to return
props.items
if the search query is emptywell you wouldn't be searching
and this
you'd just check
searchQuery()
so like if (!searchQuery()) { return props.items } else { ... }
ok i have this as solution
it does work but i imagine that .map could be avoided potentially
its just this fuzzysearch thing returns like an object with matches etc.
and i need to turn it into array of strings
so i thought i would map over it to create that array
otherwise i had issues with <For
as it would both be going over array of strings or array of objects
and didn't know how to make it work for both there nicely
memos are neat
should really use more of it 🤔
why not access
item.item
in For
?ok so
with this
yeah
in that div
it can either be
item
or item.item
just
item.item
also getting this now for some reason
oh right because it can be either an array of strings or an array of results
yes
this is result
from that fuzzy lib
thus i do the map
but that seems expensive
i guess either keep using
.map
or check if item
is a string or not when accessing it in For
{typeof item === "string" ? item : item.item}
sick
that works
i guess thats less expensive than map
or actually you could also just
wait nah
FYI: Now a Solid port of the @nozbe/microfuzz/demo is available on StackBlitz
https://stackblitz.com/edit/solidjs-templates-vitmqw
https://github.com/peerreynders/solid-microfuzz-demo
peerreynders
StackBlitz
Solid version of microfuzz demo - StackBlitz
A port of the @nozbe/microfuzz demo ported to SolidJS.
https://github.com/peerreynders/solid-microfuzz-demo
Original: nozbe.github.io/microfuzz/
GitHub
GitHub - peerreynders/solid-microfuzz-demo: A port of the @nozbe/mi...
A port of the @nozbe/microfuzz demo ported to SolidJS. - GitHub - peerreynders/solid-microfuzz-demo: A port of the @nozbe/microfuzz demo ported to SolidJS.