Fix search helper nonsense

This commit is contained in:
Jon Staab 2024-05-31 09:42:22 -07:00
parent 7ad2625f81
commit b49f50a08d
7 changed files with 28 additions and 55 deletions

BIN
package-lock.json generated

Binary file not shown.

View File

@ -59,7 +59,6 @@
"@welshman/lib": "^0.0.8",
"@welshman/net": "^0.0.11",
"@welshman/util": "^0.0.11",
"autobind-decorator": "^2.4.0",
"classnames": "^2.5.1",
"compressorjs": "^1.2.1",
"date-picker-svelte": "^2.12.0",

View File

@ -58,6 +58,8 @@
{label: "Image", kind: FILE_METADATA},
{label: "Relay selections", kind: RELAYS},
])
console.log(helper)
</script>
<span class="staatliches text-lg">What kind of content do you want to see?</span>

View File

@ -45,14 +45,8 @@ export const displayHandler = (handler?: Handler) => handler?.name || "[no name]
export class HandlerSearch extends SearchHelper<Handler, string> {
config = {keys: ["name", "about"]}
getValue(option: Handler) {
return getAddress(option.event)
}
displayValue(address: string) {
return displayHandler(this.getOption(address))
}
getValue = (option: Handler) => getAddress(option.event)
displayValue = (address: string) => displayHandler(this.getOption(address))
}
export const getHandlerAddress = (event: TrustedEvent) => {

View File

@ -8,11 +8,9 @@ export type KindOption = {
export class KindSearch extends SearchHelper<KindOption, number> {
config = {keys: ["kind", "label"]}
getValue(option: KindOption) {
return option.kind
}
getValue = (option: KindOption) => option.kind
displayValue(kind: number) {
displayValue = (kind: number) => {
const option = this.getOption(kind)
return option ? `${option.label} (kind ${kind})` : `Kind ${kind}`

View File

@ -353,17 +353,7 @@ export class ProfileSearch extends SearchHelper<PublishedProfile, string> {
score: getWotScore($pubkey, profile.event.pubkey),
}))
const fuse = new Fuse(options, {
keys: [
"profile.name",
"profile.display_name",
{name: "profile.nip05", weight: 0.5},
{name: "profile.about", weight: 0.1},
],
threshold: 0.3,
shouldSort: false,
includeScore: true,
})
const fuse = new Fuse(options, this.config)
return (term: string) => {
if (!term) {

View File

@ -344,47 +344,37 @@ export const getStringWidth = (text: string) => {
}
export class SearchHelper<T, V> {
config: any
config: Record<string, any> = {}
_optionsByValue = new Map<V, T>()
_search?: (term: string) => T[]
constructor(readonly options: T[]) {
for (const option of options) {
this._optionsByValue.set(this.getValue(option), option)
}
}
constructor(readonly options: T[]) {}
getSearch() {
return fuzzy(this.options, this.config)
}
getOption(value: V): T {
return this._optionsByValue.get(value)
}
getValue(option: T): V {
return option as unknown as V
}
displayValue(value: V) {
return String(value)
}
displayOption(option: T) {
return this.displayValue(this.getValue(option))
}
searchOptions(term: string) {
_setup() {
if (!this._search) {
for (const option of this.options) {
this._optionsByValue.set(this.getValue(option), option)
}
this._search = this.getSearch()
}
return this._search(term)
return this
}
searchValues(term: string) {
return this.searchOptions(term).map(this.getValue)
}
getSearch = () => fuzzy(this.options, this.config)
getOption = (value: V) => this._optionsByValue.get(value)
getValue = (option: T) => option as unknown as V
displayValue = (value: V) => String(value)
displayOption = (option: T) => this.displayValue(this.getValue(option))
searchOptions = (term: string) => this._setup()._search(term)
searchValues = (term: string) => this.searchOptions(term).map(this.getValue)
}
export const fromCsv = s => (s || "").split(",").filter(identity)