From c06929e4efb225be0f799e2a0d41a59468abe8c2 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Sun, 22 Nov 2026 09:00:41 -0800 Subject: [PATCH] Cache this --- .../common/input/tags/tags.component.spec.ts | 9 +++ .../common/input/tags/tags.component.ts | 58 +++++++++++++++---- 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/src-ui/src/app/components/common/input/tags/tags.component.spec.ts b/src-ui/src/app/components/common/input/tags/tags.component.spec.ts index 8bc1876e6..93b722c11 100644 --- a/src-ui/src/app/components/common/input/tags/tags.component.spec.ts +++ b/src-ui/src/app/components/common/input/tags/tags.component.spec.ts @@ -355,6 +355,15 @@ describe('TagsComponent', () => { expect(visible[1].textContent).toContain('2 hidden') }) + it('should only recompute hidden tags when the value or tags change', () => { + const first = component.hiddenTagIds + expect(component.hiddenTagIds).toBe(first) + component.value = [1, 99] + expect(component.hiddenTagIds).toEqual([99]) + component.tags = [...tags, { id: 99, name: 'now visible' }] + expect(component.hiddenTagIds).toEqual([]) + }) + it('should not treat tags as hidden before tags load', () => { ;(component as any).tagsLoaded = false expect(component.hiddenTagIds).toEqual([]) diff --git a/src-ui/src/app/components/common/input/tags/tags.component.ts b/src-ui/src/app/components/common/input/tags/tags.component.ts index 20ec3d38a..aab70a0bc 100644 --- a/src-ui/src/app/components/common/input/tags/tags.component.ts +++ b/src-ui/src/app/components/common/input/tags/tags.component.ts @@ -24,11 +24,7 @@ import { Tag } from 'src/app/data/tag' import { SETTINGS_KEYS } from 'src/app/data/ui-settings' import { TagService } from 'src/app/services/rest/tag.service' import { SettingsService } from 'src/app/services/settings.service' -import { - collapseImpliedAncestors, - getTagAncestors, - partitionTags, -} from 'src/app/utils/tag-display' +import { getTagAncestors, partitionTags } from 'src/app/utils/tag-display' import { matchesSearchText } from 'src/app/utils/text-search' import { ConfirmDialogComponent } from '../../confirm-dialog/confirm-dialog.component' import { HiddenTagsBadgeComponent } from '../../hidden-tags-badge/hidden-tags-badge.component' @@ -150,8 +146,7 @@ export class TagsComponent implements OnInit, ControlValueAccessor { * as segments of their descendant's badge. */ isImplied(id: number): boolean { - if (!this.showsHierarchy || !this.tags || !this.value) return false - return !collapseImpliedAncestors(this.value, this.tagsById).includes(id) + return !!this.value?.includes(id) && !this.display.shown.has(id) } private get showsHierarchy(): boolean { @@ -162,15 +157,58 @@ export class TagsComponent implements OnInit, ControlValueAccessor { } isHidden(id: number): boolean { - return this.hiddenTagIds.includes(id) + return this.display.hiddenSet.has(id) } /** * Tags that are hidden from the current user. */ get hiddenTagIds(): number[] { - if (!this.tagsLoaded) return [] - return partitionTags(this.value, this.tagsById, this.showsHierarchy).hidden + return this.display.hidden + } + + private displayCache: { + value: number[] + tagsById: Map + showsHierarchy: boolean + tagsLoaded: boolean + shown: Set + hidden: number[] + hiddenSet: Set + } + + /** + * Partition of the current value, recomputed only when the value, tags or + * hierarchy setting change. + */ + private get display() { + const tagsById = this.tagsById + const showsHierarchy = this.showsHierarchy + const cached = this.displayCache + if ( + cached?.value === this.value && + cached.tagsById === tagsById && + cached.showsHierarchy === showsHierarchy && + cached.tagsLoaded === this.tagsLoaded + ) { + return cached + } + const { visible, hidden } = partitionTags( + this.value, + tagsById, + showsHierarchy + ) + const hiddenIds = this.tagsLoaded ? hidden : [] + this.displayCache = { + value: this.value, + tagsById, + showsHierarchy, + tagsLoaded: this.tagsLoaded, + shown: new Set([...visible, ...hidden]), + hidden: hiddenIds, + hiddenSet: new Set(hiddenIds), + } + return this.displayCache } /**