S
SolidJS11mo ago
nikivi

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
nikivi11mo 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
nikivi11mo 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
nikivi11mo ago
im basically trying to search over an array of strings that i pass
nikivi
nikivi11mo ago
so props.items is array like this
nikivi
nikivi11mo 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
nikivi11mo 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
nikivi11mo ago
then fuzzySearch is created from props.items i tried with unwrap() too
nikivi
nikivi11mo ago
then onInput of the input box it should show matches
nikivi
nikivi11mo ago
but its 0 matches, not sure what im doing wrong on solid side
nikivi
nikivi11mo ago
thought above would work i also tried making fuzzySearch into a signal
nikivi
nikivi11mo ago
library does work, just my solid integration is broken
Otonashi
Otonashi11mo ago
you don't seem to be calling searchResults nor setSearchResults anywhere
nikivi
nikivi11mo 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
Otonashi11mo ago
is props.items an empty array at the time you call createFuzzySearch?
nikivi
nikivi11mo ago
oh yea
nikivi
nikivi11mo ago
strange though ok i think its because store gets filled in async way
nikivi
nikivi11mo ago
ok yea
nikivi
nikivi11mo 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
nikivi11mo ago
nikivi
nikivi11mo ago
i.e. it works with above
nikivi
nikivi11mo ago
but if I do
nikivi
nikivi11mo ago
nikivi
nikivi11mo ago
this will take like 100ms to complete but by the time the comopnent already rendered
nikivi
nikivi11mo ago
i need to put it in effect and make fuzzysearch signal?
nikivi
nikivi11mo ago
nikivi
nikivi11mo ago
nikivi
nikivi11mo ago
this runs twice for example first empty then with values
nikivi
nikivi11mo ago
nikivi
nikivi11mo ago
i thought maybe this
Otonashi
Otonashi11mo 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
nikivi11mo ago
const fuzzySearch = createMemo(() => {
return createFuzzySearch(props.items)
})
const fuzzySearch = createMemo(() => {
return createFuzzySearch(props.items)
})
this?
Otonashi
Otonashi11mo ago
yeah
nikivi
nikivi11mo ago
search term in a signal results in a memo as well
this i dont fully get
nikivi
nikivi11mo ago
Otonashi
Otonashi11mo ago
create a signal for the search term and update it on input
nikivi
nikivi11mo ago
onInput={(e) => {
setSearchQuery(e.target.value)
onInput={(e) => {
setSearchQuery(e.target.value)
Otonashi
Otonashi11mo ago
then create a memo for the search results that uses fuzzySearch
nikivi
nikivi11mo ago
ok done
Otonashi
Otonashi11mo ago
const searchResults = createMemo(() => fuzzySearch()(searchQuery()));
nikivi
nikivi11mo ago
nikivi
nikivi11mo ago
doesnt like that oh wow yours it likes oh probably missing return
Otonashi
Otonashi11mo ago
fuzzySearch is a function that returns the search function so you need to call it before calling it
nikivi
nikivi11mo 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
Otonashi11mo ago
that's basically what searchResults is i guess
nikivi
nikivi11mo ago
like it should loop over filteredResults i think which on default are props.items
nikivi
nikivi11mo ago
nikivi
nikivi11mo ago
its empty though on mount
Otonashi
Otonashi11mo ago
so if the query is empty it should return the whole array?
nikivi
nikivi11mo ago
yes
Otonashi
Otonashi11mo ago
then just change the memo to return props.items if the search query is empty
nikivi
nikivi11mo 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
Otonashi11mo ago
well you wouldn't be searching
nikivi
nikivi11mo ago
and this
Otonashi
Otonashi11mo ago
you'd just check searchQuery() so like if (!searchQuery()) { return props.items } else { ... }
nikivi
nikivi11mo ago
ok i have this as solution
nikivi
nikivi11mo 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
Otonashi11mo ago
why not access item.item in For?
nikivi
nikivi11mo 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
Otonashi11mo ago
yeah
nikivi
nikivi11mo 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
Otonashi11mo ago
just item.item
nikivi
nikivi11mo ago
nikivi
nikivi11mo ago
also getting this now for some reason
nikivi
nikivi11mo ago
Otonashi
Otonashi11mo ago
oh right because it can be either an array of strings or an array of results
nikivi
nikivi11mo ago
yes
nikivi
nikivi11mo ago
nikivi
nikivi11mo ago
this is result from that fuzzy lib thus i do the map but that seems expensive
Otonashi
Otonashi11mo 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
nikivi11mo ago
sick that works i guess thats less expensive than map
Otonashi
Otonashi11mo ago
or actually you could also just wait nah
peerreynders
peerreynders11mo 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
More Posts