From e7b6255e540cb34177a5014d990e64668e92d4a0 Mon Sep 17 00:00:00 2001 From: louis-19041904 Date: Mon, 17 Aug 2026 22:20:43 +0200 Subject: [PATCH] fix(sidebar): highlight nav item for detail pages, not just their list route --- .../ui/app-menu/app-menu.component.html | 6 +-- .../shared/ui/app-menu/app-menu.component.ts | 41 ++++++++++++++----- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/webapp/src/app/shared/ui/app-menu/app-menu.component.html b/webapp/src/app/shared/ui/app-menu/app-menu.component.html index 5bcd72ff..8771e738 100644 --- a/webapp/src/app/shared/ui/app-menu/app-menu.component.html +++ b/webapp/src/app/shared/ui/app-menu/app-menu.component.html @@ -67,8 +67,7 @@ class="nav-icon-button" [routerLink]="item.disabled ? null : item.route" [queryParams]="item.queryParams || null" - routerLinkActive="active" - [routerLinkActiveOptions]="navLinkActiveOptions" + [class.active]="isItemActive(item)" [class.disabled]="item.disabled" [matTooltip]="item.label" matTooltipPosition="right" @@ -80,8 +79,7 @@ mat-list-item [routerLink]="item.disabled ? null : item.route" [queryParams]="item.queryParams || null" - routerLinkActive="active" - [routerLinkActiveOptions]="navLinkActiveOptions" + [class.active]="isItemActive(item)" [class.disabled]="item.disabled"> {{ item.icon }} {{ item.label }} diff --git a/webapp/src/app/shared/ui/app-menu/app-menu.component.ts b/webapp/src/app/shared/ui/app-menu/app-menu.component.ts index 40d4bfb6..d41bcc09 100644 --- a/webapp/src/app/shared/ui/app-menu/app-menu.component.ts +++ b/webapp/src/app/shared/ui/app-menu/app-menu.component.ts @@ -1,6 +1,8 @@ import { Component, computed, inject, input, output } from '@angular/core'; import { CommonModule } from '@angular/common'; -import { IsActiveMatchOptions, Router, RouterModule } from '@angular/router'; +import { NavigationEnd, Router, RouterModule } from '@angular/router'; +import { toSignal } from '@angular/core/rxjs-interop'; +import { filter, map } from 'rxjs'; import { MatIconModule } from '@angular/material/icon'; import { MatListModule } from '@angular/material/list'; import { MatButtonModule } from '@angular/material/button'; @@ -42,6 +44,7 @@ type NavItem = { adminOnly?: boolean; superAdminOnly?: boolean; managerOrAdminOnly?: boolean; + detailRoutePrefixes?: string[]; }; type NavItemGroup = { @@ -75,12 +78,26 @@ export class AppMenuComponent { collapsed = input(false); canCollapse = input(false); toggleCollapsed = output(); - readonly navLinkActiveOptions: IsActiveMatchOptions = { - paths: 'subset', - queryParams: 'ignored', - matrixParams: 'ignored', - fragment: 'ignored', - }; + + private readonly router = inject(Router); + + private readonly currentPath = toSignal( + this.router.events.pipe( + filter((event): event is NavigationEnd => event instanceof NavigationEnd), + map((event) => event.urlAfterRedirects.split('?')[0]), + ), + { initialValue: this.router.url.split('?')[0] }, + ); + + isItemActive(item: NavItem): boolean { + const path = this.currentPath(); + if (path === item.route || path.startsWith(`${item.route}/`)) { + return true; + } + return (item.detailRoutePrefixes ?? []).some((prefix) => + path.startsWith(prefix), + ); + } readonly navItems = computed(() => { const items: NavItem[] = [ @@ -99,6 +116,7 @@ export class AppMenuComponent { this.credentialService.organizationName(), ), disabled: false, + detailRoutePrefixes: ['/person/'], }, { label: 'Bookmarks', @@ -124,6 +142,7 @@ export class AppMenuComponent { sort_by: 'works_count', sorting_order: SortDirection.Desc, }, + detailRoutePrefixes: ['/institution/'], }, { label: 'Publikationen', @@ -137,6 +156,7 @@ export class AppMenuComponent { [workQueryParamKeys.sortBy]: WorkSortBy.CitedByCount, [workQueryParamKeys.sortDirection]: SortDirection.Desc, }, + detailRoutePrefixes: ['/publication/'], }, { label: 'Themen', @@ -150,6 +170,7 @@ export class AppMenuComponent { [topicQueryParamKeys.sortBy]: TopicSortBy.CitedByCount, [topicQueryParamKeys.sortDirection]: SortDirection.Desc, }, + detailRoutePrefixes: ['/topic/'], }, { label: 'Projekte', @@ -170,6 +191,7 @@ export class AppMenuComponent { icon: PATENT_ICON, route: '/patents', disabled: false, + detailRoutePrefixes: ['/patent/'], }, { label: 'Keywords', @@ -230,10 +252,7 @@ export class AppMenuComponent { ]; }); - constructor( - public credentialService: CredentialService, - private readonly router: Router, - ) {} + constructor(public credentialService: CredentialService) {} isNavigating = computed(() => !!this.router.currentNavigation()); readonly themeService = inject(ThemeService);