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