refactor(ui): move large library processing screen to notifications (#1428)

Co-authored-by: Alex <25013571+alexhb1@users.noreply.github.com>
This commit is contained in:
brios
2026-05-22 15:57:07 +01:00
committed by GitHub
co-authored by Alex
parent 0e76b381aa
commit dbbc7c3878
20 changed files with 334 additions and 427 deletions
@@ -0,0 +1,65 @@
<ng-container *transloco="let t">
@if (progressService.state$ | async; as progress) {
@if (progress.active) {
<div class="rounded-lg border border-[var(--primary-color)] bg-[var(--card-background)] p-4">
<div class="mb-2 flex items-center justify-between gap-3">
<div class="flex min-w-0 items-center gap-2 text-[var(--primary-color)]">
<i class="pi pi-book" aria-hidden="true"></i>
<p class="m-0 text-sm font-semibold">{{ t('libraryCreator.loading.title') }}</p>
</div>
<p-tag
[value]="t(getStatusLabelKey(progress.status))"
[severity]="getTagSeverity(progress.status)">
</p-tag>
</div>
<div class="mb-2 break-words text-base text-zinc-200">
@if (progress.status === 'COMPLETED') {
{{ t('libraryCreator.loading.complete') }}
} @else if (progress.status === 'ERROR') {
{{ t('libraryCreator.creator.toast.createFailedSummary') }}
} @else {
{{ t('libraryCreator.loading.processing') }} <strong>{{ progress.currentBookTitle || progress.libraryName }}</strong>
}
</div>
<div class="mb-2 text-sm text-zinc-300 tabular-nums">
<span [innerHTML]="t('shared.metadataProgress.bookProgress', { current: getCurrentCount(progress), total: progress.expectedCount })"></span>
</div>
<p-progressBar
[style]="{ height: '15px' }"
[value]="getProgressPercent(progress)"
[showValue]="false">
</p-progressBar>
@if (progress.status !== 'IN_PROGRESS') {
<div class="mt-4 flex flex-wrap justify-end gap-2">
@if (progress.libraryId !== undefined) {
<button
pButton
type="button"
[label]="t('libraryCreator.loading.openLibrary')"
icon="pi pi-book"
severity="info"
size="small"
outlined
(click)="openLibrary(progress)">
</button>
}
<button
pButton
type="button"
[label]="t('shared.metadataProgress.dismissBtn')"
icon="pi pi-times"
severity="secondary"
size="small"
outlined
(click)="dismiss()">
</button>
</div>
}
</div>
}
}
</ng-container>
@@ -0,0 +1,65 @@
import {AsyncPipe} from '@angular/common';
import {ChangeDetectionStrategy, Component, inject} from '@angular/core';
import {Router} from '@angular/router';
import {ButtonDirective} from 'primeng/button';
import {ProgressBar} from 'primeng/progressbar';
import {Tag} from 'primeng/tag';
import {TranslocoDirective} from '@jsverse/transloco';
import {LibraryImportProgressService, LibraryImportProgressState, LibraryImportProgressStatus} from '../../service/library-import-progress.service';
@Component({
selector: 'app-library-import-progress-widget',
templateUrl: './library-import-progress-widget-component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
imports: [AsyncPipe, ButtonDirective, ProgressBar, Tag, TranslocoDirective]
})
export class LibraryImportProgressWidgetComponent {
protected readonly progressService = inject(LibraryImportProgressService);
private readonly router = inject(Router);
getProgressPercent(state: LibraryImportProgressState): number {
if (state.expectedCount <= 0) return 0;
if (state.status === 'COMPLETED') return 100;
const percent = Math.round((state.processedCount / state.expectedCount) * 100);
return Math.min(100, Math.max(0, percent));
}
getCurrentCount(state: LibraryImportProgressState): number {
return Math.min(state.processedCount, state.expectedCount);
}
getStatusLabelKey(status: LibraryImportProgressStatus): string {
return {
IN_PROGRESS: 'shared.metadataProgress.statusInProgress',
COMPLETED: 'shared.metadataProgress.statusCompleted',
ERROR: 'shared.metadataProgress.statusError',
}[status];
}
getTagSeverity(status: LibraryImportProgressStatus): 'info' | 'success' | 'danger' {
switch (status) {
case 'COMPLETED':
return 'success';
case 'ERROR':
return 'danger';
case 'IN_PROGRESS':
default:
return 'info';
}
}
dismiss(): void {
this.progressService.clear();
}
openLibrary(state: LibraryImportProgressState): void {
if (state.libraryId === undefined) return;
this.router.navigate(['/library', state.libraryId, 'books']).then(navigated => {
if (navigated) {
this.dismiss();
}
});
}
}
@@ -3,6 +3,9 @@
@if (hasMetadataTasks$ | async) {
<app-metadata-progress-widget/>
}
@if (hasActiveLibraryImport$ | async) {
<app-library-import-progress-widget/>
}
@if (hasPendingBookdropFiles$ | async) {
<app-bookdrop-files-widget-component/>
}
@@ -6,6 +6,7 @@ import {BookdropFileService} from '../../../features/bookdrop/service/bookdrop-f
import {getTranslocoModule} from '../../../core/testing/transloco-testing';
import {MetadataBatchStatus} from '../../model/metadata-batch-progress.model';
import {MetadataProgressService} from '../../service/metadata-progress.service';
import {LibraryImportProgressService} from '../../service/library-import-progress.service';
import {UnifiedNotificationBoxComponent} from './unified-notification-popover-component';
describe('UnifiedNotificationBoxComponent', () => {
@@ -13,10 +14,12 @@ describe('UnifiedNotificationBoxComponent', () => {
let component: UnifiedNotificationBoxComponent;
let activeTasks$: BehaviorSubject<Record<string, unknown>>;
let hasPendingFiles$: BehaviorSubject<boolean>;
let hasActiveImport$: BehaviorSubject<boolean>;
beforeEach(async () => {
activeTasks$ = new BehaviorSubject<Record<string, unknown>>({});
hasPendingFiles$ = new BehaviorSubject(false);
hasActiveImport$ = new BehaviorSubject(false);
await TestBed.configureTestingModule({
imports: [UnifiedNotificationBoxComponent, getTranslocoModule()],
@@ -29,6 +32,10 @@ describe('UnifiedNotificationBoxComponent', () => {
provide: BookdropFileService,
useValue: {hasPendingFiles$},
},
{
provide: LibraryImportProgressService,
useValue: {hasActiveImport$},
},
],
}).compileComponents();
@@ -72,4 +79,15 @@ describe('UnifiedNotificationBoxComponent', () => {
hasPendingFiles$.next(true);
expect(hasPendingFiles).toBe(true);
});
it('forwards the active library import signal', () => {
let hasActiveImport: boolean | undefined;
component.hasActiveLibraryImport$.subscribe(value => {
hasActiveImport = value;
});
expect(hasActiveImport).toBe(false);
hasActiveImport$.next(true);
expect(hasActiveImport).toBe(true);
});
});
@@ -6,12 +6,15 @@ import {AsyncPipe} from '@angular/common';
import {BookdropFileService} from '../../../features/bookdrop/service/bookdrop-file.service';
import {BookdropFilesWidgetComponent} from '../../../features/bookdrop/component/bookdrop-files-widget/bookdrop-files-widget.component';
import {MetadataProgressWidgetComponent} from '../metadata-progress-widget/metadata-progress-widget-component';
import {LibraryImportProgressService} from '../../service/library-import-progress.service';
import {LibraryImportProgressWidgetComponent} from '../library-import-progress-widget/library-import-progress-widget-component';
@Component({
selector: 'app-unified-notification-popover-component',
imports: [
LiveNotificationBoxComponent,
MetadataProgressWidgetComponent,
LibraryImportProgressWidgetComponent,
AsyncPipe,
BookdropFilesWidgetComponent
],
@@ -22,10 +25,12 @@ import {MetadataProgressWidgetComponent} from '../metadata-progress-widget/metad
export class UnifiedNotificationBoxComponent {
metadataProgressService = inject(MetadataProgressService);
bookdropFileService = inject(BookdropFileService);
libraryImportProgressService = inject(LibraryImportProgressService);
hasMetadataTasks$ = this.metadataProgressService.activeTasks$.pipe(
map(tasks => Object.keys(tasks).length > 0)
);
hasPendingBookdropFiles$ = this.bookdropFileService.hasPendingFiles$;
hasActiveLibraryImport$ = this.libraryImportProgressService.hasActiveImport$;
}
@@ -21,6 +21,7 @@ import { BookdropFileService } from '../../../features/bookdrop/service/bookdrop
import { AuthService } from '../../service/auth.service';
import { MetadataBatchProgressNotification, MetadataBatchStatus } from '../../model/metadata-batch-progress.model';
import { MetadataProgressService } from '../../service/metadata-progress.service';
import { LibraryImportProgressService } from '../../service/library-import-progress.service';
import { AppVersion, VersionService } from '../../service/version.service';
import { DialogLauncherService } from '../../services/dialog-launcher.service';
import { LayoutService } from '../layout.service';
@@ -42,6 +43,7 @@ describe('AppSidebarComponent', () => {
let activeTasks$: BehaviorSubject<Record<string, MetadataBatchProgressNotification>>;
let progressUpdates$: BehaviorSubject<MetadataBatchProgressNotification>;
let hasPendingFiles$: BehaviorSubject<boolean>;
let hasActiveImport$: BehaviorSubject<boolean>;
const sidebarCollapsed = signal(false);
const isDesktop = signal(true);
const layoutService = {
@@ -68,6 +70,7 @@ describe('AppSidebarComponent', () => {
review: false,
});
hasPendingFiles$ = new BehaviorSubject(false);
hasActiveImport$ = new BehaviorSubject(false);
TestBed.configureTestingModule({
imports: [AppSidebarComponent, getTranslocoModule()],
@@ -97,6 +100,7 @@ describe('AppSidebarComponent', () => {
{ provide: AuthService, useValue: { logout: vi.fn() } },
{ provide: MetadataProgressService, useValue: { activeTasks$, progressUpdates$ } },
{ provide: BookdropFileService, useValue: { hasPendingFiles$ } },
{ provide: LibraryImportProgressService, useValue: { hasActiveImport$ } },
{ provide: VersionService, useValue: { getVersion: vi.fn(() => versionInfo) } },
{ provide: LayoutService, useValue: layoutService },
{ provide: UserService, useValue: { currentUser } },
@@ -247,7 +251,7 @@ describe('AppSidebarComponent', () => {
expect(sidebar.notificationPopoverOrigin()).toBeNull();
});
it('aggregates metadata tasks and pending bookdrop files into the sidebar badge count', () => {
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;
@@ -272,8 +276,9 @@ describe('AppSidebarComponent', () => {
},
});
hasPendingFiles$.next(true);
hasActiveImport$.next(true);
expect(sidebar.completedTaskCount).toBe(3);
expect(sidebar.completedTaskCount).toBe(4);
expect(sidebar.shouldShowNotificationBadge).toBe(true);
});
@@ -37,6 +37,7 @@ 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 { LibraryImportProgressService } from '../../service/library-import-progress.service';
const DOCUMENTATION_URL = 'https://grimmory.org/docs/getting-started';
const ABOVE_ALIGN_LEFT: ConnectedPosition[] = [
@@ -139,6 +140,7 @@ export class AppSidebarComponent {
private readonly destroyRef = inject(DestroyRef);
private readonly metadataProgressService = inject(MetadataProgressService);
private readonly bookdropFileService = inject(BookdropFileService);
private readonly libraryImportProgressService = inject(LibraryImportProgressService);
readonly currentUser = this.userService.currentUser;
private readonly allAuthors = this.authorService.allAuthors;
@@ -231,6 +233,7 @@ export class AppSidebarComponent {
protected progressHighlight = false;
protected completedTaskCount = 0;
protected hasPendingBookdropFiles = false;
protected hasActiveLibraryImport = false;
private readonly latestTasks: Record<string, MetadataBatchProgressNotification> = {};
@@ -250,6 +253,13 @@ export class AppSidebarComponent {
this.hasPendingBookdropFiles = hasPending;
this.updateCompletedTaskCount();
});
this.libraryImportProgressService.hasActiveImport$
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((hasActive) => {
this.hasActiveLibraryImport = hasActive;
this.updateCompletedTaskCount();
});
}
openSearch(): void {
@@ -383,6 +393,7 @@ export class AppSidebarComponent {
private updateCompletedTaskCount(): void {
const metadataTaskCount = Object.keys(this.latestTasks).length;
const bookdropFileTaskCount = this.hasPendingBookdropFiles ? 1 : 0;
this.completedTaskCount = metadataTaskCount + bookdropFileTaskCount;
const libraryImportTaskCount = this.hasActiveLibraryImport ? 1 : 0;
this.completedTaskCount = metadataTaskCount + bookdropFileTaskCount + libraryImportTaskCount;
}
}
@@ -0,0 +1,47 @@
import {TestBed} from '@angular/core/testing';
import {afterEach, describe, expect, it} from 'vitest';
import {LibraryImportProgressService, LibraryImportProgressState} from './library-import-progress.service';
describe('LibraryImportProgressService', () => {
afterEach(() => {
TestBed.resetTestingModule();
});
it('tracks progress from start through completion and clear', () => {
TestBed.configureTestingModule({providers: [LibraryImportProgressService]});
const service = TestBed.inject(LibraryImportProgressService);
let latest: LibraryImportProgressState | undefined;
service.state$.subscribe(state => {
latest = state;
});
service.start('Main Library', 2);
service.attachLibrary(7);
service.recordBookAdded('First');
expect(latest).toEqual({
active: true,
libraryId: 7,
libraryName: 'Main Library',
expectedCount: 2,
processedCount: 1,
currentBookTitle: 'First',
status: 'IN_PROGRESS',
});
service.fail();
expect(latest?.status).toBe('ERROR');
service.start('Main Library', 2);
service.recordBookAdded('First');
service.recordBookAdded('Second');
expect(latest?.status).toBe('COMPLETED');
service.clear();
expect(latest?.active).toBe(false);
});
});
@@ -0,0 +1,82 @@
import {Injectable} from '@angular/core';
import {BehaviorSubject} from 'rxjs';
import {map} from 'rxjs/operators';
export type LibraryImportProgressStatus = 'IN_PROGRESS' | 'COMPLETED' | 'ERROR';
export interface LibraryImportProgressState {
active: boolean;
libraryId?: number;
libraryName: string;
expectedCount: number;
processedCount: number;
currentBookTitle?: string;
status: LibraryImportProgressStatus;
}
const EMPTY_STATE: LibraryImportProgressState = {
active: false,
libraryName: '',
expectedCount: 0,
processedCount: 0,
status: 'COMPLETED',
};
@Injectable({providedIn: 'root'})
export class LibraryImportProgressService {
private readonly stateSubject = new BehaviorSubject<LibraryImportProgressState>(EMPTY_STATE);
readonly state$ = this.stateSubject.asObservable();
readonly hasActiveImport$ = this.state$.pipe(map(state => state.active));
start(libraryName: string, expectedCount: number): void {
if (expectedCount <= 0) {
this.clear();
return;
}
this.stateSubject.next({
active: false,
libraryName,
expectedCount,
processedCount: 0,
status: 'IN_PROGRESS',
});
}
attachLibrary(libraryId: number): void {
const state = this.stateSubject.value;
if (state.expectedCount <= 0) return;
this.stateSubject.next({...state, libraryId});
}
recordBookAdded(bookTitle: string): void {
const state = this.stateSubject.value;
if (state.expectedCount <= 0 || state.status !== 'IN_PROGRESS') return;
const processedCount = Math.min(state.processedCount + 1, state.expectedCount);
this.stateSubject.next({
...state,
active: true,
processedCount,
currentBookTitle: bookTitle,
status: processedCount >= state.expectedCount
? 'COMPLETED'
: 'IN_PROGRESS',
});
}
fail(): void {
const state = this.stateSubject.value;
if (state.expectedCount <= 0 || state.status !== 'IN_PROGRESS') return;
if (!state.active) {
this.clear();
return;
}
this.stateSubject.next({...state, status: 'ERROR'});
}
clear(): void {
this.stateSubject.next(EMPTY_STATE);
}
}