diff --git a/src-ui/src/app/pipes/collapse-tags.pipe.ts b/src-ui/src/app/pipes/collapse-tags.pipe.ts new file mode 100644 index 0000000000..5ba98766d3 --- /dev/null +++ b/src-ui/src/app/pipes/collapse-tags.pipe.ts @@ -0,0 +1,47 @@ +import { inject, Pipe, PipeTransform } from '@angular/core' +import { catchError, map, Observable, of } from 'rxjs' +import { Tag } from '../data/tag' +import { SETTINGS_KEYS } from '../data/ui-settings' +import { + PermissionAction, + PermissionsService, + PermissionType, +} from '../services/permissions.service' +import { TagService } from '../services/rest/tag.service' +import { SettingsService } from '../services/settings.service' +import { collapseImpliedAncestors } from '../utils/collapse-tags' + +/** + * Drops tag IDs whose descendant is also in the list for when the child's + * Tag badge already shows its parent. + */ +@Pipe({ + name: 'collapseTags', +}) +export class CollapseTagsPipe implements PipeTransform { + private readonly permissionsService = inject(PermissionsService) + private readonly tagService = inject(TagService) + private readonly settingsService = inject(SettingsService) + + transform(ids: number[]): Observable { + if ( + !ids?.length || + !this.settingsService.get(SETTINGS_KEYS.TAGS_SHOW_HIERARCHY) || + !this.permissionsService.currentUserCan( + PermissionAction.View, + PermissionType.Tag + ) + ) { + return of(ids ?? []) + } + return this.tagService.listAll().pipe( + map((tags) => + collapseImpliedAncestors( + ids, + new Map(tags.results.map((t) => [t.id, t])) + ) + ), + catchError(() => of(ids)) + ) + } +} diff --git a/src-ui/src/app/utils/collapse-tags.spec.ts b/src-ui/src/app/utils/collapse-tags.spec.ts new file mode 100644 index 0000000000..a685460cb1 --- /dev/null +++ b/src-ui/src/app/utils/collapse-tags.spec.ts @@ -0,0 +1,36 @@ +import { Tag } from '../data/tag' +import { collapseImpliedAncestors, getTagAncestors } from './collapse-tags' + +const tags: Tag[] = [ + { id: 1, name: 'Finance' }, + { id: 2, name: 'Account', parent: 1 }, + { id: 3, name: 'Mortgage', parent: 2 }, + { id: 4, name: 'Taxes', parent: 1 }, + { id: 5, name: 'Inbox' }, +] +const tagsById = new Map(tags.map((t) => [t.id, t])) + +describe('collapseImpliedAncestors', () => { + it('drops ancestors that are implied by a descendant in the list', () => { + expect(collapseImpliedAncestors([1, 2, 3, 4, 5], tagsById)).toEqual([ + 3, 4, 5, + ]) + }) + + it('keeps a parent with none of its children present', () => { + expect(collapseImpliedAncestors([1, 5], tagsById)).toEqual([1, 5]) + }) + + it('keeps unknown (private) ids and handles empty input', () => { + expect(collapseImpliedAncestors([99, 2], tagsById)).toEqual([99, 2]) + expect(collapseImpliedAncestors([], tagsById)).toEqual([]) + expect(collapseImpliedAncestors(null, tagsById)).toEqual([]) + }) +}) + +describe('getTagAncestors', () => { + it('returns ancestors root first', () => { + expect(getTagAncestors(3, tagsById).map((t) => t.id)).toEqual([1, 2]) + expect(getTagAncestors(5, tagsById)).toEqual([]) + }) +}) diff --git a/src-ui/src/app/utils/collapse-tags.ts b/src-ui/src/app/utils/collapse-tags.ts new file mode 100644 index 0000000000..72f31646fb --- /dev/null +++ b/src-ui/src/app/utils/collapse-tags.ts @@ -0,0 +1,43 @@ +import { Tag } from '../data/tag' + +/** + * Collapses implied ancestor tags from a list of tag IDs such that if a tag's + * ancestor is also present in the list, the ancestor is removed. For example + * [Finance, Account, Mortgage] -> [Mortgage] + */ +export function collapseImpliedAncestors( + ids: number[], + tagsById: Map +): number[] { + if (!ids?.length) return ids ?? [] + const present = new Set(ids) + const implied = new Set() + for (const id of ids) { + const seen = new Set() + let parentId = tagsById.get(id)?.parent + while (parentId && !seen.has(parentId)) { + seen.add(parentId) + if (present.has(parentId)) implied.add(parentId) + parentId = tagsById.get(parentId)?.parent + } + } + return ids.filter((id) => !implied.has(id)) +} + +/** + * Returns an array of ancestor tags for the given tag ID, starting from the + * root and ending with the immediate parent. + */ +export function getTagAncestors(id: number, tagsById: Map): Tag[] { + const chain: Tag[] = [] + const seen = new Set() + let parentId = tagsById.get(id)?.parent + while (parentId && !seen.has(parentId)) { + seen.add(parentId) + const parent = tagsById.get(parentId) + if (!parent) break + chain.unshift(parent) + parentId = parent.parent + } + return chain +}