Help integrating microfuzz search library with solid

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
nikivi
nikiviOP2y ago
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
nikivi
nikiviOP2y ago
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
nikivi
nikiviOP2y ago
im basically trying to search over an array of strings that i pass
nikivi
nikiviOP2y ago
so props.items is array like this
nikivi
nikiviOP2y ago
the 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.
nikivi
nikiviOP2y ago
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
nikivi
nikiviOP2y ago
then fuzzySearch is created from props.items i tried with unwrap() too
nikivi
nikiviOP2y ago
then onInput of the input box it should show matches
nikivi
nikiviOP2y ago
but its 0 matches, not sure what im doing wrong on solid side
nikivi
nikiviOP2y ago
thought above would work i also tried making fuzzySearch into a signal
nikivi
nikiviOP2y ago
library does work, just my solid integration is broken
Otonashi
Otonashi2y ago
you don't seem to be calling searchResults nor setSearchResults anywhere
nikivi
nikiviOP2y ago
yes 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 []
Otonashi
Otonashi2y ago
is props.items an empty array at the time you call createFuzzySearch?
nikivi
nikiviOP2y ago
oh yea
nikivi
nikiviOP2y ago
strange though ok i think its because store gets filled in async way
nikivi
nikiviOP2y ago
ok yea
nikivi
nikiviOP2y ago
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
nikivi
nikiviOP2y ago
nikivi
nikiviOP2y ago
i.e. it works with above
nikivi
nikiviOP2y ago
but if I do
nikivi
nikiviOP2y ago
nikivi
nikiviOP2y ago
this will take like 100ms to complete but by the time the comopnent already rendered
nikivi
nikiviOP2y ago
i need to put it in effect and make fuzzysearch signal?
nikivi
nikiviOP2y ago
nikivi
nikiviOP2y ago
nikivi
nikiviOP2y ago
this runs twice for example first empty then with values
nikivi
nikiviOP2y ago
nikivi
nikiviOP2y ago
i thought maybe this
Otonashi
Otonashi2y ago
i would probably put fuzzySearch in a memo, and the search term in a signal and the results in a memo as well
nikivi
nikiviOP2y ago
const fuzzySearch = createMemo(() => {
return createFuzzySearch(props.items)
})
const fuzzySearch = createMemo(() => {
return createFuzzySearch(props.items)
})
this?
Otonashi
Otonashi2y ago
yeah
nikivi
nikiviOP2y ago
search term in a signal results in a memo as well
this i dont fully get
nikivi
nikiviOP2y ago
Otonashi
Otonashi2y ago
create a signal for the search term and update it on input
nikivi
nikiviOP2y ago
onInput={(e) => {
setSearchQuery(e.target.value)
onInput={(e) => {
setSearchQuery(e.target.value)
Otonashi
Otonashi2y ago
then create a memo for the search results that uses fuzzySearch
nikivi
nikiviOP2y ago
ok done
Otonashi
Otonashi2y ago
const searchResults = createMemo(() => fuzzySearch()(searchQuery()));
nikivi
nikiviOP2y ago
nikivi
nikiviOP2y ago
doesnt like that oh wow yours it likes oh probably missing return
Otonashi
Otonashi2y ago
fuzzySearch is a function that returns the search function so you need to call it before calling it
nikivi
nikiviOP2y ago
export default function SearchModal(props: Props) {
const [filteredItems, setFilteredItems] = createSignal(props.items)
const [searchQuery, setSearchQuery] = createSignal("")

const fuzzySearch = createMemo(() => {
return createFuzzySearch(props.items)
})

const searchResults = createMemo(() => fuzzySearch()(searchQuery()))

createEffect(() => {
console.log(searchResults, "search results")
})
export default function SearchModal(props: Props) {
const [filteredItems, setFilteredItems] = createSignal(props.items)
const [searchQuery, setSearchQuery] = createSignal("")

const fuzzySearch = createMemo(() => {
return createFuzzySearch(props.items)
})

const searchResults = createMemo(() => fuzzySearch()(searchQuery()))

createEffect(() => {
console.log(searchResults, "search results")
})
i thought doing this effect
createEffect(() => {
console.log(searchResults(), "search results")
})
createEffect(() => {
console.log(searchResults(), "search results")
})
oh wow ok that does work thank you lots @otonashi9 ❤️
const [filteredItems, setFilteredItems] = createSignal(props.items)
const [filteredItems, setFilteredItems] = createSignal(props.items)
should this become a memo too?
<For each={props.items}>
{(item) => {
return (
<div
<For each={props.items}>
{(item) => {
return (
<div
im thinking about this loop
Otonashi
Otonashi2y ago
that's basically what searchResults is i guess
nikivi
nikiviOP2y ago
like it should loop over filteredResults i think which on default are props.items
nikivi
nikiviOP2y ago
nikivi
nikiviOP2y ago
its empty though on mount
Otonashi
Otonashi2y ago
so if the query is empty it should return the whole array?
nikivi
nikiviOP2y ago
yes
Otonashi
Otonashi2y ago
then just change the memo to return props.items if the search query is empty
nikivi
nikiviOP2y ago
const searchResults = createMemo(() => {
if (fuzzySearch()(searchQuery).length === 0) {
return props.items
} else {
return fuzzySearch()(searchQuery())
}
})
const searchResults = createMemo(() => {
if (fuzzySearch()(searchQuery).length === 0) {
return props.items
} else {
return fuzzySearch()(searchQuery())
}
})
<For each={searchResults()}>
<For each={searchResults()}>
Otonashi
Otonashi2y ago
well you wouldn't be searching
nikivi
nikiviOP2y ago
and this
Otonashi
Otonashi2y ago
you'd just check searchQuery() so like if (!searchQuery()) { return props.items } else { ... }
nikivi
nikiviOP2y ago
ok i have this as solution
nikivi
nikiviOP2y ago
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 🤔
Otonashi
Otonashi2y ago
why not access item.item in For?
nikivi
nikiviOP2y ago
ok so
const searchResults = createMemo(() => {
if (!searchQuery()) {
return props.items
} else {
return fuzzySearch()(searchQuery())
const searchResults = createMemo(() => {
if (!searchQuery()) {
return props.items
} else {
return fuzzySearch()(searchQuery())
with this
Otonashi
Otonashi2y ago
yeah
nikivi
nikiviOP2y ago
<For each={searchResults()}>
{(item) => {
return (
<div
onClick={() => {}}
class="text-black dark:text-white px-6 rounded-lg py-3"
>
<div>{item}</div>
<For each={searchResults()}>
{(item) => {
return (
<div
onClick={() => {}}
class="text-black dark:text-white px-6 rounded-lg py-3"
>
<div>{item}</div>
in that div it can either be item or item.item
Otonashi
Otonashi2y ago
just item.item
nikivi
nikiviOP2y ago
nikivi
nikiviOP2y ago
also getting this now for some reason
nikivi
nikiviOP2y ago
Otonashi
Otonashi2y ago
oh right because it can be either an array of strings or an array of results
nikivi
nikiviOP2y ago
yes
nikivi
nikiviOP2y ago
nikivi
nikiviOP2y ago
this is result from that fuzzy lib thus i do the map but that seems expensive
Otonashi
Otonashi2y ago
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}
nikivi
nikiviOP2y ago
sick that works i guess thats less expensive than map
Otonashi
Otonashi2y ago
or actually you could also just wait nah
peerreynders
peerreynders2y ago
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.
Want results from more Discord servers?
Add your server