refactor(ui): temp app endpoints for author and series counts (#2781)

This commit is contained in:
Alex
2026-09-27 10:53:34 +01:00
committed by GitHub
parent 1b68d56e22
commit e933da7404
3 changed files with 64 additions and 6 deletions
@@ -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<number> {
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<SidebarSection[]>(() => {
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(),
@@ -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,
});
});
});
@@ -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<LibraryHealthService, 'isUnhealthy'>;
}
@@ -37,7 +42,7 @@ function sortByPref<T extends { id: number; name: string }>(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,