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
@@ -1,5 +1,5 @@
<ng-container *transloco="let t">
@if (progressService.state$ | async; as progress) {
@if (progressService.state(); 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">
@@ -1,4 +1,3 @@
import {AsyncPipe} from '@angular/common';
import {ChangeDetectionStrategy, Component, inject} from '@angular/core';
import {Router} from '@angular/router';
import {ButtonDirective} from 'primeng/button';
@@ -13,7 +12,7 @@ import {LibraryImportProgressService, LibraryImportProgressState, LibraryImportP
templateUrl: './library-import-progress-widget-component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
imports: [AsyncPipe, ButtonDirective, ProgressBar, Tag, TranslocoDirective]
imports: [ButtonDirective, ProgressBar, Tag, TranslocoDirective]
})
export class LibraryImportProgressWidgetComponent {
protected readonly progressService = inject(LibraryImportProgressService);
@@ -1,12 +1,12 @@
<div class="metadata-progress-box">
<app-live-notification-box/>
@if (hasMetadataTasks$ | async) {
@if (hasMetadataTasks()) {
<app-metadata-progress-widget/>
}
@if (hasActiveLibraryImport$ | async) {
@if (hasActiveLibraryImport()) {
<app-library-import-progress-widget/>
}
@if (hasPendingBookdropFiles$ | async) {
@if (hasPendingBookdropFiles()) {
<app-bookdrop-files-widget-component/>
}
</div>
@@ -1,3 +1,4 @@
import {Signal, signal, WritableSignal} from '@angular/core';
import {BehaviorSubject} from 'rxjs';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
@@ -14,12 +15,12 @@ describe('UnifiedNotificationBoxComponent', () => {
let component: UnifiedNotificationBoxComponent;
let activeTasks$: BehaviorSubject<Record<string, unknown>>;
let hasPendingFiles$: BehaviorSubject<boolean>;
let hasActiveImport$: BehaviorSubject<boolean>;
let hasActiveImport: WritableSignal<boolean>;
beforeEach(async () => {
activeTasks$ = new BehaviorSubject<Record<string, unknown>>({});
hasPendingFiles$ = new BehaviorSubject(false);
hasActiveImport$ = new BehaviorSubject(false);
hasActiveImport = signal(false);
await TestBed.configureTestingModule({
imports: [UnifiedNotificationBoxComponent, getTranslocoModule()],
@@ -34,7 +35,7 @@ describe('UnifiedNotificationBoxComponent', () => {
},
{
provide: LibraryImportProgressService,
useValue: {hasActiveImport$},
useValue: {hasActiveImport},
},
],
}).compileComponents();
@@ -48,12 +49,9 @@ describe('UnifiedNotificationBoxComponent', () => {
});
it('reports whether there are metadata tasks to show', () => {
let hasMetadataTasks: boolean | undefined;
component.hasMetadataTasks$.subscribe(value => {
hasMetadataTasks = value;
});
const popover = component as unknown as {hasMetadataTasks: Signal<boolean>};
expect(hasMetadataTasks).toBe(false);
expect(popover.hasMetadataTasks()).toBe(false);
activeTasks$.next({
'task-1': {
@@ -66,28 +64,22 @@ describe('UnifiedNotificationBoxComponent', () => {
},
});
expect(hasMetadataTasks).toBe(true);
expect(popover.hasMetadataTasks()).toBe(true);
});
it('forwards the bookdrop pending-file signal', () => {
let hasPendingFiles: boolean | undefined;
component.hasPendingBookdropFiles$.subscribe(value => {
hasPendingFiles = value;
});
const popover = component as unknown as {hasPendingBookdropFiles: Signal<boolean>};
expect(hasPendingFiles).toBe(false);
expect(popover.hasPendingBookdropFiles()).toBe(false);
hasPendingFiles$.next(true);
expect(hasPendingFiles).toBe(true);
expect(popover.hasPendingBookdropFiles()).toBe(true);
});
it('forwards the active library import signal', () => {
let hasActiveImport: boolean | undefined;
component.hasActiveLibraryImport$.subscribe(value => {
hasActiveImport = value;
});
const popover = component as unknown as {hasActiveLibraryImport: Signal<boolean>};
expect(hasActiveImport).toBe(false);
hasActiveImport$.next(true);
expect(hasActiveImport).toBe(true);
expect(popover.hasActiveLibraryImport()).toBe(false);
hasActiveImport.set(true);
expect(popover.hasActiveLibraryImport()).toBe(true);
});
});
@@ -1,8 +1,7 @@
import {Component, inject} from '@angular/core';
import {ChangeDetectionStrategy, Component, computed, inject} from '@angular/core';
import {LiveNotificationBoxComponent} from '../live-notification-box/live-notification-box.component';
import {MetadataProgressService} from '../../service/metadata-progress.service';
import {map} from 'rxjs/operators';
import {AsyncPipe} from '@angular/common';
import {toSignal} from '@angular/core/rxjs-interop';
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';
@@ -15,22 +14,20 @@ import {LibraryImportProgressWidgetComponent} from '../library-import-progress-w
LiveNotificationBoxComponent,
MetadataProgressWidgetComponent,
LibraryImportProgressWidgetComponent,
AsyncPipe,
BookdropFilesWidgetComponent
],
templateUrl: './unified-notification-popover-component.html',
standalone: true,
styleUrl: './unified-notification-popover-component.scss'
styleUrl: './unified-notification-popover-component.scss',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class UnifiedNotificationBoxComponent {
metadataProgressService = inject(MetadataProgressService);
bookdropFileService = inject(BookdropFileService);
libraryImportProgressService = inject(LibraryImportProgressService);
private readonly metadataProgressService = inject(MetadataProgressService);
private readonly bookdropFileService = inject(BookdropFileService);
private readonly libraryImportProgressService = inject(LibraryImportProgressService);
hasMetadataTasks$ = this.metadataProgressService.activeTasks$.pipe(
map(tasks => Object.keys(tasks).length > 0)
);
hasPendingBookdropFiles$ = this.bookdropFileService.hasPendingFiles$;
hasActiveLibraryImport$ = this.libraryImportProgressService.hasActiveImport$;
private readonly activeMetadataTasks = toSignal(this.metadataProgressService.activeTasks$, {initialValue: {}});
protected readonly hasMetadataTasks = computed(() => Object.keys(this.activeMetadataTasks()).length > 0);
protected readonly hasPendingBookdropFiles = toSignal(this.bookdropFileService.hasPendingFiles$, {initialValue: false});
protected readonly hasActiveLibraryImport = this.libraryImportProgressService.hasActiveImport;
}