Improve autocomplete model logic

This commit is contained in:
Eric Bailey
2023-10-25 12:07:12 -05:00
parent b78ec89338
commit 8314f90a5e
+19 -12
View File
@@ -6,8 +6,6 @@ import {isObj, hasProp, isStrArray} from 'lib/type-guards'
/** /**
* Used only to persist recent tags across app restarts. * Used only to persist recent tags across app restarts.
*
* TODO may want an LRU?
*/ */
export class RecentTagsModel { export class RecentTagsModel {
_tags: string[] = [] _tags: string[] = []
@@ -21,7 +19,7 @@ export class RecentTagsModel {
} }
add(tag: string) { add(tag: string) {
this._tags = Array.from(new Set([tag, ...this._tags])) this._tags = Array.from(new Set([tag, ...this._tags])).slice(0, 100) // save up to 100 recent tags
} }
remove(tag: string) { remove(tag: string) {
@@ -74,23 +72,32 @@ export class TagsAutocompleteModel {
return [] return []
} }
// no query, return default suggestions
if (!this.query) {
return Array.from(
// de-duplicates via Set
new Set([
// sample 6 recent tags
...this.rootStore.recentTags.tags.slice(0, 6),
// sample 3 of your profile tags
...this.profileTags.slice(0, 3),
]),
)
}
// we're going to search this list
const items = Array.from( const items = Array.from(
// de-duplicates via Set // de-duplicates via Set
new Set([ new Set([
// sample up to 3 recent tags // all recent tags
...this.rootStore.recentTags.tags.slice(0, 3), ...this.rootStore.recentTags.tags,
// sample up to 3 of your profile tags // all profile tags
...this.profileTags.slice(0, 3), ...this.profileTags,
// and all searched tags // and all searched tags
...this.searchedTags, ...this.searchedTags,
]), ]),
) )
// no query, return default suggestions
if (!this.query) {
return items.slice(0, 9)
}
// Fuse allows weighting values too, if we ever need it // Fuse allows weighting values too, if we ever need it
const fuse = new Fuse(items) const fuse = new Fuse(items)
// search amongst mixed set of tags // search amongst mixed set of tags