mirror of
https://github.com/coracle-social/coracle.git
synced 2024-09-29 00:10:52 +00:00
Fix search helper nonsense
This commit is contained in:
parent
7ad2625f81
commit
b49f50a08d
BIN
package-lock.json
generated
BIN
package-lock.json
generated
Binary file not shown.
@ -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",
|
||||
|
@ -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>
|
||||
|
@ -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) => {
|
||||
|
@ -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}`
|
||||
|
@ -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) {
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user