partitionTags function, rename to display-tags pipe

This commit is contained in:
shamoon committed 2026-11-25 16:20:11 -08:00
1 parent 551d2a9882
commit dd74137cdb
5 files changed
+104 -30

No files matched your search

@@ -27,6 +27,7 @@ import { SettingsService } from 'src/app/services/settings.service'
import {
collapseImpliedAncestors,
getTagAncestors,
partitionTags,
} from 'src/app/utils/collapse-tags'
import { matchesSearchText } from 'src/app/utils/text-search'
import { ConfirmDialogComponent } from '../../confirm-dialog/confirm-dialog.component'
@@ -161,14 +162,15 @@ export class TagsComponent implements OnInit, ControlValueAccessor {
}
isHidden(id: number): boolean {
return this.tagsLoaded && !this.tagsById.has(id) && !this.isImplied(id)
return this.hiddenTagIds.includes(id)
}
/**
* Tags that are hidden from the current user.
*/
get hiddenTagIds(): number[] {
return this.value?.filter((id) => this.isHidden(id)) ?? []
if (!this.tagsLoaded) return []
return partitionTags(this.value, this.tagsById, this.showsHierarchy).hidden
}
/**
@@ -6,7 +6,7 @@ import { Tag } from '../data/tag'
import { PermissionsService } from '../services/permissions.service'
import { TagService } from '../services/rest/tag.service'
import { SettingsService } from '../services/settings.service'
import { CollapseTagsPipe } from './collapse-tags.pipe'
import { DisplayTagsPipe } from './display-tags.pipe'
const tags: Tag[] = [
{ id: 1, name: 'Parent' },
@@ -14,8 +14,8 @@ const tags: Tag[] = [
{ id: 3, name: 'Other' },
]
describe('CollapseTagsPipe', () => {
let pipe: CollapseTagsPipe
describe('DisplayTagsPipe', () => {
let pipe: DisplayTagsPipe
let tagService: TagService
let settingsService: SettingsService
let permissionsService: PermissionsService
@@ -23,12 +23,12 @@ describe('CollapseTagsPipe', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
CollapseTagsPipe,
DisplayTagsPipe,
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting(),
],
})
pipe = TestBed.inject(CollapseTagsPipe)
pipe = TestBed.inject(DisplayTagsPipe)
tagService = TestBed.inject(TagService)
settingsService = TestBed.inject(SettingsService)
permissionsService = TestBed.inject(PermissionsService)
@@ -39,29 +39,43 @@ describe('CollapseTagsPipe', () => {
.mockReturnValue(of({ count: 3, all: [1, 2, 3], results: tags }))
})
it('should drop parents implied by a selected child', async () => {
expect(await firstValueFrom(pipe.transform([1, 2, 3]))).toEqual([2, 3])
it('should split visible and hidden tags, collapsing implied parents', async () => {
expect(await firstValueFrom(pipe.transform([1, 2, 98, 3]))).toEqual({
visible: [2, 3],
hidden: [98],
})
})
it('should pass ids through when hierarchy display is off or not permitted', async () => {
it('should not collapse parents when hierarchy display is off', async () => {
jest.spyOn(settingsService, 'get').mockReturnValue(false)
expect(await firstValueFrom(pipe.transform([1, 2]))).toEqual([1, 2])
expect(await firstValueFrom(pipe.transform([1, 2]))).toEqual({
visible: [1, 2],
hidden: [],
})
})
jest.spyOn(settingsService, 'get').mockReturnValue(true)
it('should hide all tags without tag view permission', async () => {
jest.spyOn(permissionsService, 'currentUserCan').mockReturnValue(false)
expect(await firstValueFrom(pipe.transform([1, 2]))).toEqual([1, 2])
expect(await firstValueFrom(pipe.transform([1, 2]))).toEqual({
visible: [],
hidden: [1, 2],
})
expect(tagService.listAll).not.toHaveBeenCalled()
})
it('should handle empty input', async () => {
expect(await firstValueFrom(pipe.transform(null))).toEqual([])
expect(await firstValueFrom(pipe.transform([]))).toEqual([])
const empty = { visible: [], hidden: [] }
expect(await firstValueFrom(pipe.transform(null))).toEqual(empty)
expect(await firstValueFrom(pipe.transform([]))).toEqual(empty)
})
it('should fall back to the original ids if tags fail to load', async () => {
it('should show the original ids if tags fail to load', async () => {
jest
.spyOn(tagService, 'listAll')
.mockReturnValue(throwError(() => new Error('failed')))
expect(await firstValueFrom(pipe.transform([1, 2]))).toEqual([1, 2])
expect(await firstValueFrom(pipe.transform([1, 2]))).toEqual({
visible: [1, 2],
hidden: [],
})
})
})
@@ -9,39 +9,39 @@ import {
} from '../services/permissions.service'
import { TagService } from '../services/rest/tag.service'
import { SettingsService } from '../services/settings.service'
import { collapseImpliedAncestors } from '../utils/collapse-tags'
import { partitionTags, TagPartition } 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.
* Splits a document's tag IDs into regular ones vs those the
* current user cannot view, see partitionTags.
*/
@Pipe({
name: 'collapseTags',
name: 'displayTags',
})
export class CollapseTagsPipe implements PipeTransform {
export class DisplayTagsPipe implements PipeTransform {
private readonly permissionsService = inject(PermissionsService)
private readonly tagService = inject(TagService)
private readonly settingsService = inject(SettingsService)
transform(ids: number[]): Observable<number[]> {
transform(ids: number[]): Observable<TagPartition> {
if (!ids?.length) return of({ visible: [], hidden: [] })
if (
!ids?.length ||
!this.settingsService.get(SETTINGS_KEYS.TAGS_SHOW_HIERARCHY) ||
!this.permissionsService.currentUserCan(
PermissionAction.View,
PermissionType.Tag
)
) {
return of(ids ?? [])
return of({ visible: [], hidden: ids })
}
return this.tagService.listAll().pipe(
map((tags) =>
collapseImpliedAncestors(
partitionTags(
ids,
new Map<number, Tag>(tags.results.map((t) => [t.id, t]))
new Map<number, Tag>(tags.results.map((t) => [t.id, t])),
!!this.settingsService.get(SETTINGS_KEYS.TAGS_SHOW_HIERARCHY)
)
),
catchError(() => of(ids))
catchError(() => of({ visible: ids, hidden: [] }))
)
}
}
+35 -1
View File
@@ -1,5 +1,9 @@
import { Tag } from '../data/tag'
import { collapseImpliedAncestors, getTagAncestors } from './collapse-tags'
import {
collapseImpliedAncestors,
getTagAncestors,
partitionTags,
} from './collapse-tags'
const tags: Tag[] = [
{ id: 1, name: 'Finance' },
@@ -45,3 +49,33 @@ describe('getTagAncestors', () => {
expect(getTagAncestors(7, cyclic).map((t) => t.id)).toEqual([7, 8])
})
})
describe('partitionTags', () => {
it('splits ids into visible and hidden, keeping order', () => {
expect(partitionTags([5, 98, 4, 99], tagsById, true)).toEqual({
visible: [5, 4],
hidden: [98, 99],
})
expect(partitionTags(null, tagsById, true)).toEqual({
visible: [],
hidden: [],
})
})
it('drops implied ancestors only when showing hierarchy', () => {
expect(partitionTags([1, 2, 3], tagsById, true).visible).toEqual([3])
expect(partitionTags([1, 2, 3], tagsById, false).visible).toEqual([1, 2, 3])
})
it('drops a hidden ancestor of a visible tag only when showing hierarchy', () => {
const withHiddenParent = new Map(tagsById)
withHiddenParent.set(6, { id: 6, name: 'Child', parent: 98 })
expect(partitionTags([6, 98, 99], withHiddenParent, true)).toEqual({
visible: [6],
hidden: [99],
})
expect(partitionTags([6, 98, 99], withHiddenParent, false).hidden).toEqual([
98, 99,
])
})
})
+24
View File
@@ -41,3 +41,27 @@ export function getTagAncestors(id: number, tagsById: Map<number, Tag>): Tag[] {
}
return chain
}
export interface TagPartition {
visible: number[]
hidden: number[]
}
/**
* Splits a document's tag IDs for display. Tags missing from tagsById are hidden
* from the current user and when showing hierarchy, ancestors of a child are dropped
* since the descendant badge will show them.
*/
export function partitionTags(
ids: number[],
tagsById: Map<number, Tag>,
showHierarchy: boolean
): TagPartition {
const shown = showHierarchy
? collapseImpliedAncestors(ids, tagsById)
: (ids ?? [])
return {
visible: shown.filter((id) => tagsById.has(id)),
hidden: shown.filter((id) => !tagsById.has(id)),
}
}