diff --git a/frontend/src/app/shared/layout/layout-sidebar/app.sidebar.component.ts b/frontend/src/app/shared/layout/layout-sidebar/app.sidebar.component.ts index c2657dd8d..cfce3f47a 100644 --- a/frontend/src/app/shared/layout/layout-sidebar/app.sidebar.component.ts +++ b/frontend/src/app/shared/layout/layout-sidebar/app.sidebar.component.ts @@ -1,4 +1,11 @@ import { Component, computed, inject, signal } from '@angular/core'; +import {HttpClient} from '@angular/common/http'; +import {injectQuery} from '@tanstack/angular-query-experimental'; +import {lastValueFrom} from 'rxjs'; +import {map} from 'rxjs/operators'; +import {API_CONFIG} from '../../../core/config/api-config'; +import {QUERY_DEFAULTS} from '../../../core/data/query-transport'; +import {bookQueryKeys} from '../../../features/book/data/book-query-keys'; import { Router, RouterLink } from '@angular/router'; import { AppSidebarSectionComponent } from './app.sidebar-section.component'; import { CdkTrapFocus } from '@angular/cdk/a11y'; @@ -58,6 +65,9 @@ import {AppMenuItemComponent} from '../../ui/menu/app-menu-item.component'; import {AppMenuTriggerDirective} from '../../ui/menu/app-menu-trigger.directive'; const DOCUMENTATION_URL = 'https://grimmory.org/docs/getting-started'; +// Keyed under book collections so every book change refreshes these counts. +const SERIES_COUNT_QUERY_KEY = [...bookQueryKeys.collections(), 'series-count'] as const; +const AUTHOR_COUNT_QUERY_KEY = [...bookQueryKeys.collections(), 'author-count'] as const; const ABOVE_ALIGN_LEFT: ConnectedPosition[] = [ { originX: 'start', originY: 'top', overlayX: 'start', overlayY: 'bottom', offsetY: -8 }, { originX: 'start', originY: 'bottom', overlayX: 'start', overlayY: 'top', offsetY: 8 }, @@ -169,6 +179,7 @@ export class AppSidebarComponent { private readonly userService = inject(UserService); private readonly versionService = inject(VersionService); private readonly magicShelfService = inject(MagicShelfService); + private readonly http = inject(HttpClient); private readonly t = inject(TranslocoService); private readonly metadataProgressService = inject(MetadataProgressService); private readonly bookdropFileService = inject(BookdropFileService); @@ -210,10 +221,35 @@ export class AppSidebarComponent { typeof navigator !== 'undefined' ? navigator.userAgent : '' ); + private readonly homeCountsEnabled = computed(() => + this.authService.isAuthenticated() && this.layoutService.areSidebarCountsVisible('home')); + // Temporary: Using the /app/ endpoints until the new ones are ready. They work for now, easy enough to swap when ready. + private readonly seriesCountQuery = injectQuery(() => ({ + queryKey: SERIES_COUNT_QUERY_KEY, + queryFn: () => this.entityCount('series'), + enabled: this.homeCountsEnabled(), + ...QUERY_DEFAULTS, + })); + private readonly authorCountQuery = injectQuery(() => ({ + queryKey: AUTHOR_COUNT_QUERY_KEY, + queryFn: () => this.entityCount('authors'), + enabled: this.homeCountsEnabled(), + ...QUERY_DEFAULTS, + })); + + private entityCount(entity: 'series' | 'authors'): Promise { + return lastValueFrom(this.http + .get<{totalElements: number}>(`${API_CONFIG.BASE_URL}/api/v1/app/${entity}`, {params: {page: 0, size: 1}}) + .pipe(map(response => response.totalElements))); + } + readonly sections = computed(() => { this.activeLang(); return [ - ...buildHomeSection(this.translate), + ...buildHomeSection(this.translate, { + series: this.seriesCountQuery.data() ?? 0, + authors: this.authorCountQuery.data() ?? 0, + }), ...buildLibrarySection( this.libraryService.libraries(), this.layoutService.librarySort(), diff --git a/frontend/src/app/shared/layout/layout-sidebar/sidebar-sections.spec.ts b/frontend/src/app/shared/layout/layout-sidebar/sidebar-sections.spec.ts index dc4e7553a..c7a1281a6 100644 --- a/frontend/src/app/shared/layout/layout-sidebar/sidebar-sections.spec.ts +++ b/frontend/src/app/shared/layout/layout-sidebar/sidebar-sections.spec.ts @@ -39,8 +39,8 @@ describe('normalizeSortPref', () => { }); describe('buildHomeSection', () => { - it('emits the standard home items and scopes the all-books count', () => { - const [section] = buildHomeSection(translate); + it('emits the standard home items and attaches counts only to counted entries', () => { + const [section] = buildHomeSection(translate, { series: 3, authors: 7 }); expect(section.id).toBe('home'); expect(section.expandable).toBe(true); @@ -48,8 +48,16 @@ describe('buildHomeSection', () => { 'dashboard', 'allBooks', 'series', 'authors', 'notebook', ]); - expect(section.items?.find((item) => item.id === 'allBooks')?.bookScope).toBeNull(); - expect(section.items?.every((item) => item.bookCount === undefined)).toBe(true); + const counts = Object.fromEntries( + (section.items ?? []).map((item) => [item.id, item.bookCount]), + ); + expect(counts).toEqual({ + dashboard: undefined, + allBooks: undefined, + series: 3, + authors: 7, + notebook: undefined, + }); }); }); diff --git a/frontend/src/app/shared/layout/layout-sidebar/sidebar-sections.ts b/frontend/src/app/shared/layout/layout-sidebar/sidebar-sections.ts index 46e6089fb..8d0d5a3ea 100644 --- a/frontend/src/app/shared/layout/layout-sidebar/sidebar-sections.ts +++ b/frontend/src/app/shared/layout/layout-sidebar/sidebar-sections.ts @@ -13,6 +13,11 @@ import { import { SidebarLeaf, SidebarSection } from '../navigation/nav-item.model'; import { buildHomeNavItems, findPageNavItem, ShellNavPermissions } from '../navigation/nav-catalog'; +export interface HomeCounts { + series: number; + authors: number; +} + export interface LibrarySectionDeps { health: Pick; } @@ -37,7 +42,7 @@ function sortByPref(items: T[], pref: So return pref.order === 'desc' ? sorted.reverse() : sorted; } -export function buildHomeSection(translate: TranslateFn): SidebarSection[] { +export function buildHomeSection(translate: TranslateFn, counts: HomeCounts): SidebarSection[] { return [{ id: 'home', menuKey: 'home', @@ -46,6 +51,7 @@ export function buildHomeSection(translate: TranslateFn): SidebarSection[] { items: buildHomeNavItems(translate).map((item) => ({ ...item, bookScope: item.id === 'allBooks' ? null : undefined, + bookCount: homeItemBookCount(item.id, counts), })), }]; } @@ -71,6 +77,14 @@ export function buildToolsSection( }]; } +function homeItemBookCount(itemId: string, counts: HomeCounts): number | undefined { + switch (itemId) { + case 'series': return counts.series; + case 'authors': return counts.authors; + default: return undefined; + } +} + export function buildLibrarySection( libraries: Library[], sort: SortPref,