refactor(ui): use signals for library import progress (#1458)

This commit is contained in:
Alex
2026-05-23 19:01:48 +01:00
committed by GitHub
parent 9c530f51f1
commit db09c39192
10 changed files with 81 additions and 140 deletions
@@ -101,8 +101,8 @@
>
<span class="sidebar-notification-button-content">
<i class="pi pi-bell" aria-hidden="true"></i>
@if (shouldShowNotificationBadge) {
<span class="sidebar-notification-badge">{{ completedTaskCount }}</span>
@if (shouldShowNotificationBadge()) {
<span class="sidebar-notification-badge">{{ completedTaskCount() }}</span>
}
</span>
</button>
@@ -202,8 +202,8 @@
>
<span class="sidebar-notification-button-content">
<i class="pi pi-bell" aria-hidden="true"></i>
@if (shouldShowNotificationBadge) {
<span class="sidebar-notification-badge">{{ completedTaskCount }}</span>
@if (shouldShowNotificationBadge()) {
<span class="sidebar-notification-badge">{{ completedTaskCount() }}</span>
}
</span>
<span class="sidebar-footer-action-label">{{ t('notifications') }}</span>
@@ -1,4 +1,4 @@
import { computed, signal, WritableSignal } from '@angular/core';
import { computed, Signal, signal, WritableSignal } from '@angular/core';
import { CdkOverlayOrigin } from '@angular/cdk/overlay';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { BehaviorSubject } from 'rxjs';
@@ -43,7 +43,7 @@ describe('AppSidebarComponent', () => {
let activeTasks$: BehaviorSubject<Record<string, MetadataBatchProgressNotification>>;
let progressUpdates$: BehaviorSubject<MetadataBatchProgressNotification>;
let hasPendingFiles$: BehaviorSubject<boolean>;
let hasActiveImport$: BehaviorSubject<boolean>;
let hasActiveImport: WritableSignal<boolean>;
const sidebarCollapsed = signal(false);
const isDesktop = signal(true);
const layoutService = {
@@ -70,7 +70,7 @@ describe('AppSidebarComponent', () => {
review: false,
});
hasPendingFiles$ = new BehaviorSubject(false);
hasActiveImport$ = new BehaviorSubject(false);
hasActiveImport = signal(false);
TestBed.configureTestingModule({
imports: [AppSidebarComponent, getTranslocoModule()],
@@ -100,7 +100,7 @@ describe('AppSidebarComponent', () => {
{ provide: AuthService, useValue: { logout: vi.fn() } },
{ provide: MetadataProgressService, useValue: { activeTasks$, progressUpdates$ } },
{ provide: BookdropFileService, useValue: { hasPendingFiles$ } },
{ provide: LibraryImportProgressService, useValue: { hasActiveImport$ } },
{ provide: LibraryImportProgressService, useValue: { hasActiveImport } },
{ provide: VersionService, useValue: { getVersion: vi.fn(() => versionInfo) } },
{ provide: LayoutService, useValue: layoutService },
{ provide: UserService, useValue: { currentUser } },
@@ -253,8 +253,8 @@ describe('AppSidebarComponent', () => {
it('aggregates metadata tasks, library imports, and pending bookdrop files into the sidebar badge count', () => {
const sidebar = component as unknown as {
completedTaskCount: number;
shouldShowNotificationBadge: boolean;
completedTaskCount: Signal<number>;
shouldShowNotificationBadge: Signal<boolean>;
};
activeTasks$.next({
@@ -276,15 +276,15 @@ describe('AppSidebarComponent', () => {
},
});
hasPendingFiles$.next(true);
hasActiveImport$.next(true);
hasActiveImport.set(true);
expect(sidebar.completedTaskCount).toBe(4);
expect(sidebar.shouldShowNotificationBadge).toBe(true);
expect(sidebar.completedTaskCount()).toBe(4);
expect(sidebar.shouldShowNotificationBadge()).toBe(true);
});
it('hides the badge while metadata progress is actively running', () => {
const sidebar = component as unknown as {
shouldShowNotificationBadge: boolean;
shouldShowNotificationBadge: Signal<boolean>;
};
activeTasks$.next({
@@ -306,6 +306,6 @@ describe('AppSidebarComponent', () => {
review: false,
});
expect(sidebar.shouldShowNotificationBadge).toBe(false);
expect(sidebar.shouldShowNotificationBadge()).toBe(false);
});
});
@@ -1,4 +1,4 @@
import { Component, DestroyRef, computed, inject, signal } from '@angular/core';
import { Component, computed, inject, signal } from '@angular/core';
import { Router, RouterLink } from '@angular/router';
import { AppSidebarSectionComponent } from './app.sidebar-section.component';
import { Menu } from 'primeng/menu';
@@ -23,7 +23,7 @@ import { LayoutService } from '../layout.service';
import { TranslocoDirective, TranslocoPipe, TranslocoService } from '@jsverse/transloco';
import { Tooltip } from 'primeng/tooltip';
import type { MenuItem } from 'primeng/api';
import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop';
import { toSignal } from '@angular/core/rxjs-interop';
import { NavItem, SidebarSection } from '../navigation/nav-item.model';
import { buildCreateActionNavItems } from '../navigation/nav-catalog';
import {
@@ -36,7 +36,7 @@ import {
import { VersionService } from '../../service/version.service';
import { MetadataProgressService } from '../../service/metadata-progress.service';
import { BookdropFileService } from '../../../features/bookdrop/service/bookdrop-file.service';
import { MetadataBatchProgressNotification, MetadataBatchStatus } from '../../model/metadata-batch-progress.model';
import { MetadataBatchStatus } from '../../model/metadata-batch-progress.model';
import { LibraryImportProgressService } from '../../service/library-import-progress.service';
const DOCUMENTATION_URL = 'https://grimmory.org/docs/getting-started';
@@ -137,7 +137,6 @@ export class AppSidebarComponent {
private readonly seriesDataService = inject(SeriesDataService);
private readonly authorService = inject(AuthorService);
private readonly t = inject(TranslocoService);
private readonly destroyRef = inject(DestroyRef);
private readonly metadataProgressService = inject(MetadataProgressService);
private readonly bookdropFileService = inject(BookdropFileService);
private readonly libraryImportProgressService = inject(LibraryImportProgressService);
@@ -230,46 +229,27 @@ export class AppSidebarComponent {
protected readonly notificationPopoverPositions = computed(() =>
this.layoutService.isDesktop() ? RIGHT_ALIGN_TOP : this.notificationPopoverMobilePositions()
);
protected progressHighlight = false;
protected completedTaskCount = 0;
protected hasPendingBookdropFiles = false;
protected hasActiveLibraryImport = false;
private readonly latestTasks: Record<string, MetadataBatchProgressNotification> = {};
constructor() {
this.subscribeToMetadataProgress();
this.metadataProgressService.activeTasks$
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((tasks) => {
this.replaceLatestTasks(tasks);
this.updateCompletedTaskCount();
});
this.bookdropFileService.hasPendingFiles$
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((hasPending) => {
this.hasPendingBookdropFiles = hasPending;
this.updateCompletedTaskCount();
});
this.libraryImportProgressService.hasActiveImport$
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((hasActive) => {
this.hasActiveLibraryImport = hasActive;
this.updateCompletedTaskCount();
});
}
private readonly activeMetadataTasks = toSignal(this.metadataProgressService.activeTasks$, { initialValue: {} });
private readonly latestProgress = toSignal(this.metadataProgressService.progressUpdates$, { initialValue: null });
private readonly progressHighlight = computed(() =>
this.latestProgress()?.status === MetadataBatchStatus.IN_PROGRESS
);
private readonly hasPendingBookdropFiles = toSignal(this.bookdropFileService.hasPendingFiles$, { initialValue: false });
private readonly hasActiveLibraryImport = this.libraryImportProgressService.hasActiveImport;
protected readonly completedTaskCount = computed(() => {
const metadataTaskCount = Object.keys(this.activeMetadataTasks()).length;
const bookdropFileTaskCount = this.hasPendingBookdropFiles() ? 1 : 0;
const libraryImportTaskCount = this.hasActiveLibraryImport() ? 1 : 0;
return metadataTaskCount + bookdropFileTaskCount + libraryImportTaskCount;
});
protected readonly shouldShowNotificationBadge = computed(() =>
this.completedTaskCount() > 0 && !this.progressHighlight()
);
openSearch(): void {
this.commandPaletteService.open();
}
protected get shouldShowNotificationBadge(): boolean {
return this.completedTaskCount > 0 && !this.progressHighlight;
}
closeMobileSidebar(): void {
this.layoutService.closeMobileSidebar();
}
@@ -375,25 +355,4 @@ export class AppSidebarComponent {
this.notificationPopoverOrigin.set(null);
}
private subscribeToMetadataProgress(): void {
this.metadataProgressService.progressUpdates$
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((progress) => {
this.progressHighlight = progress.status === MetadataBatchStatus.IN_PROGRESS;
});
}
private replaceLatestTasks(tasks: Record<string, MetadataBatchProgressNotification>): void {
for (const key of Object.keys(this.latestTasks)) {
delete this.latestTasks[key];
}
Object.assign(this.latestTasks, tasks);
}
private updateCompletedTaskCount(): void {
const metadataTaskCount = Object.keys(this.latestTasks).length;
const bookdropFileTaskCount = this.hasPendingBookdropFiles ? 1 : 0;
const libraryImportTaskCount = this.hasActiveLibraryImport ? 1 : 0;
this.completedTaskCount = metadataTaskCount + bookdropFileTaskCount + libraryImportTaskCount;
}
}