From 8314f90a5e9eeba82bb47a548e5f50ecd3ea21ed Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Wed, 25 Oct 2023 12:07:12 -0500 Subject: [PATCH] Improve autocomplete model logic --- src/state/models/ui/tags-autocomplete.ts | 31 +++++++++++++++--------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/state/models/ui/tags-autocomplete.ts b/src/state/models/ui/tags-autocomplete.ts index e6f2a27ead..e8ddf9e6d9 100644 --- a/src/state/models/ui/tags-autocomplete.ts +++ b/src/state/models/ui/tags-autocomplete.ts @@ -6,8 +6,6 @@ import {isObj, hasProp, isStrArray} from 'lib/type-guards' /** * Used only to persist recent tags across app restarts. - * - * TODO may want an LRU? */ export class RecentTagsModel { _tags: string[] = [] @@ -21,7 +19,7 @@ export class RecentTagsModel { } 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) { @@ -74,23 +72,32 @@ export class TagsAutocompleteModel { 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( // de-duplicates via Set new Set([ - // sample up to 3 recent tags - ...this.rootStore.recentTags.tags.slice(0, 3), - // sample up to 3 of your profile tags - ...this.profileTags.slice(0, 3), + // all recent tags + ...this.rootStore.recentTags.tags, + // all profile tags + ...this.profileTags, // and all searched tags ...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 const fuse = new Fuse(items) // search amongst mixed set of tags