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:
@@ -19,6 +19,7 @@ import { CdkDragDrop, DragDropModule, moveItemInArray } from '@angular/cdk/drag-
|
||||
import { Checkbox } from 'primeng/checkbox';
|
||||
import { Select } from 'primeng/select';
|
||||
import { TranslocoDirective, TranslocoPipe, TranslocoService } from '@jsverse/transloco';
|
||||
import { LibraryImportProgressService } from '../../shared/service/library-import-progress.service';
|
||||
|
||||
interface FormatEntry { type: BookType; label: string }
|
||||
|
||||
@@ -45,6 +46,7 @@ export class LibraryCreatorComponent {
|
||||
private readonly router = inject(Router);
|
||||
private readonly iconPicker = inject(IconPickerService);
|
||||
private readonly t = inject(TranslocoService);
|
||||
private readonly libraryImportProgressService = inject(LibraryImportProgressService);
|
||||
|
||||
private readonly activeLang = toSignal(this.t.langChanges$, {
|
||||
initialValue: this.t.getActiveLang(),
|
||||
@@ -302,8 +304,7 @@ export class LibraryCreatorComponent {
|
||||
this.libraryService.scanLibraryPaths(library).pipe(
|
||||
switchMap(count => {
|
||||
if (count >= 500) {
|
||||
console.warn(`Library has ${count} processable files (>500). Will use buffered loading.`);
|
||||
this.libraryService.setLargeLibraryLoading(true, count);
|
||||
this.libraryImportProgressService.start(library.name, count);
|
||||
}
|
||||
return this.libraryService.createLibrary(library).pipe(
|
||||
map(createdLibrary => ({ createdLibrary, count }))
|
||||
@@ -312,6 +313,9 @@ export class LibraryCreatorComponent {
|
||||
).subscribe({
|
||||
next: ({ createdLibrary, count }) => {
|
||||
if (createdLibrary) {
|
||||
if (count >= 500 && createdLibrary.id !== undefined) {
|
||||
this.libraryImportProgressService.attachLibrary(createdLibrary.id);
|
||||
}
|
||||
this.router.navigate(['/library', createdLibrary.id, 'books']);
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
@@ -324,7 +328,7 @@ export class LibraryCreatorComponent {
|
||||
}
|
||||
},
|
||||
error: (e: Error) => {
|
||||
this.libraryService.setLargeLibraryLoading(false, 0);
|
||||
this.libraryImportProgressService.fail();
|
||||
this.messageService.add({ severity: 'error', summary: this.t.translate('libraryCreator.creator.toast.createFailedSummary'), detail: this.t.translate('libraryCreator.creator.toast.createFailedDetail') });
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
import {describe, it} from 'vitest';
|
||||
|
||||
// NOTE(frontend-seam): Replace this skipped spec once the component-factory lifecycle
|
||||
// is wrapped behind a testable seam. The current service hard-codes Angular's
|
||||
// `createComponent()` ESM export plus direct `document.body` mutation, and Vitest in
|
||||
// this browser-mode setup cannot reliably intercept that export to prove attach/detach
|
||||
// behavior without changing runtime code.
|
||||
describe.skip('LibraryLoadingService', () => {
|
||||
it('needs a component-factory seam to verify dynamic creation and teardown', () => {
|
||||
// TODO(seam): Cover component creation, progress updates, and body overflow cleanup
|
||||
// after introducing a testable wrapper around createComponent/document.body behavior.
|
||||
});
|
||||
});
|
||||
@@ -1,44 +0,0 @@
|
||||
import {Injectable, ComponentRef, ApplicationRef, createComponent, EmbeddedViewRef, EnvironmentInjector, inject} from '@angular/core';
|
||||
import { LibraryLoadingComponent } from './library-loading/library-loading.component';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class LibraryLoadingService {
|
||||
private appRef = inject(ApplicationRef);
|
||||
private injector = inject(EnvironmentInjector);
|
||||
private componentRef: ComponentRef<LibraryLoadingComponent> | null = null;
|
||||
|
||||
showBookLoadingProgress(bookTitle: string, current: number, total: number): void {
|
||||
if (this.componentRef) {
|
||||
this.updateProgress(bookTitle, current, total);
|
||||
return;
|
||||
}
|
||||
|
||||
this.componentRef = createComponent(LibraryLoadingComponent, {
|
||||
environmentInjector: this.injector
|
||||
});
|
||||
|
||||
this.componentRef.instance.updateProgress(bookTitle, current, total);
|
||||
|
||||
this.appRef.attachView(this.componentRef.hostView);
|
||||
const domElem = (this.componentRef.hostView as EmbeddedViewRef<unknown>).rootNodes[0] as HTMLElement;
|
||||
document.body.appendChild(domElem);
|
||||
document.body.style.overflow = 'hidden';
|
||||
}
|
||||
|
||||
updateProgress(bookTitle: string, current: number, total: number): void {
|
||||
if (this.componentRef) {
|
||||
this.componentRef.instance.updateProgress(bookTitle, current, total);
|
||||
}
|
||||
}
|
||||
|
||||
hide(): void {
|
||||
if (this.componentRef) {
|
||||
this.appRef.detachView(this.componentRef.hostView);
|
||||
this.componentRef.destroy();
|
||||
this.componentRef = null;
|
||||
document.body.style.overflow = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
-52
@@ -1,52 +0,0 @@
|
||||
<ng-container *transloco="let t; prefix: 'libraryCreator.loading'">
|
||||
<div class="fullscreen-loader">
|
||||
<div class="library-loader-content">
|
||||
<div class="library-loader-header">
|
||||
<i class="pi pi-book"></i>
|
||||
<h2>{{ t('title') }}</h2>
|
||||
</div>
|
||||
|
||||
<div class="library-loader-body">
|
||||
<div class="book-title">
|
||||
@if (!isComplete()) {
|
||||
<ng-container>
|
||||
{{ t('processing') }} <strong>{{ bookTitle() }}</strong>
|
||||
</ng-container>
|
||||
}
|
||||
@if (isComplete()) {
|
||||
<strong class="complete-message">
|
||||
<i class="pi pi-check-circle"></i>
|
||||
{{ t('complete') }}
|
||||
</strong>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div class="progress-info">
|
||||
<span>{{ current() }} / {{ total() }}</span>
|
||||
<span>{{ percentage() }}%</span>
|
||||
</div>
|
||||
|
||||
<div class="progress-bar-container">
|
||||
<div class="progress-bar-fill" [style.width.%]="percentage()"></div>
|
||||
</div>
|
||||
|
||||
@if (!isComplete()) {
|
||||
<div class="loader-animation">
|
||||
<div class="dot"></div>
|
||||
<div class="dot"></div>
|
||||
<div class="dot"></div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
@if (isComplete()) {
|
||||
<div class="library-loader-footer">
|
||||
<button class="reload-button" (click)="onReload()">
|
||||
<i class="pi pi-bookmark"></i>
|
||||
{{ t('openLibrary') }}
|
||||
</button>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</ng-container>
|
||||
-152
@@ -1,152 +0,0 @@
|
||||
.fullscreen-loader {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background: rgb(0 0 0 / 85%);
|
||||
backdrop-filter: blur(8px);
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
z-index: 9999;
|
||||
}
|
||||
|
||||
.library-loader-content {
|
||||
background: var(--card-background);
|
||||
padding: 2.5rem;
|
||||
border-radius: 12px;
|
||||
width: 550px;
|
||||
max-width: 90vw;
|
||||
box-sizing: border-box;
|
||||
border: 1px solid var(--border-color);
|
||||
}
|
||||
|
||||
.library-loader-header {
|
||||
text-align: center;
|
||||
|
||||
i {
|
||||
font-size: 3rem;
|
||||
color: var(--primary-color);
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
h2 {
|
||||
margin: 0 0 0.5rem;
|
||||
color: var(--text-color);
|
||||
font-size: 1.5rem;
|
||||
}
|
||||
}
|
||||
|
||||
.library-loader-body {
|
||||
.book-title {
|
||||
color: var(--text-color-secondary);
|
||||
margin-bottom: 1.5rem;
|
||||
margin-top: 2.5rem;
|
||||
font-size: 0.95rem;
|
||||
overflow-wrap: anywhere;
|
||||
overflow-wrap: break-word;
|
||||
line-height: 1.4;
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
|
||||
strong {
|
||||
color: var(--text-color);
|
||||
}
|
||||
|
||||
.complete-message {
|
||||
color: var(--primary-color);
|
||||
font-size: 1.1rem;
|
||||
|
||||
i {
|
||||
margin-right: 0.5rem;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.progress-info {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 0.5rem;
|
||||
color: var(--text-color-secondary);
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.progress-bar-container {
|
||||
width: 100%;
|
||||
height: 8px;
|
||||
background: var(--card-background);
|
||||
border-radius: 4px;
|
||||
overflow: hidden;
|
||||
|
||||
.progress-bar-fill {
|
||||
height: 100%;
|
||||
background: var(--primary-color);
|
||||
transition: width 0.3s ease;
|
||||
}
|
||||
}
|
||||
|
||||
.loader-animation {
|
||||
margin-top: 1.5rem;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
gap: 0.5rem;
|
||||
|
||||
.dot {
|
||||
width: 8px;
|
||||
height: 8px;
|
||||
background: var(--primary-color);
|
||||
border-radius: 50%;
|
||||
animation: bounce 1.4s infinite ease-in-out both;
|
||||
|
||||
&:nth-child(2) {
|
||||
animation-delay: 0.2s;
|
||||
}
|
||||
|
||||
&:nth-child(3) {
|
||||
animation-delay: 0.4s;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.library-loader-footer {
|
||||
margin-top: 3rem;
|
||||
text-align: center;
|
||||
|
||||
.reload-button {
|
||||
padding: 0.75rem 1.25rem;
|
||||
background: var(--primary-color);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
font-size: 1rem;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
font-weight: 700;
|
||||
|
||||
i {
|
||||
margin-right: 0.5rem;
|
||||
}
|
||||
|
||||
&:hover {
|
||||
background: var(--primary-hover-color);
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 12px rgb(0 0 0 / 20%);
|
||||
}
|
||||
|
||||
&:active {
|
||||
transform: translateY(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes bounce {
|
||||
0%, 80%, 100% {
|
||||
transform: scale(0);
|
||||
}
|
||||
|
||||
40% {
|
||||
transform: scale(1);
|
||||
}
|
||||
}
|
||||
-40
@@ -1,40 +0,0 @@
|
||||
import {describe, expect, it} from 'vitest';
|
||||
|
||||
import {LibraryLoadingComponent} from './library-loading.component';
|
||||
|
||||
describe('LibraryLoadingComponent', () => {
|
||||
it('computes progress percentage and completion state from the latest update', () => {
|
||||
const component = new LibraryLoadingComponent();
|
||||
|
||||
component.updateProgress('Dune', 2, 3);
|
||||
|
||||
expect(component.bookTitle()).toBe('Dune');
|
||||
expect(component.current()).toBe(2);
|
||||
expect(component.total()).toBe(3);
|
||||
expect(component.percentage()).toBe(67);
|
||||
expect(component.isComplete()).toBe(false);
|
||||
});
|
||||
|
||||
it('marks the flow complete when current reaches the total', () => {
|
||||
const component = new LibraryLoadingComponent();
|
||||
|
||||
component.updateProgress('Children of Dune', 5, 5);
|
||||
|
||||
expect(component.isComplete()).toBe(true);
|
||||
expect(component.percentage()).toBe(100);
|
||||
});
|
||||
|
||||
it('truncates very long titles for display', () => {
|
||||
const component = new LibraryLoadingComponent();
|
||||
|
||||
component.updateProgress('A'.repeat(200), 2, 3)
|
||||
|
||||
expect(component.bookTitle()).toBe(`${'A'.repeat(50)}...`);
|
||||
});
|
||||
|
||||
it('returns zero percent when there is no total yet', () => {
|
||||
const component = new LibraryLoadingComponent();
|
||||
|
||||
expect(component.percentage()).toBe(0);
|
||||
});
|
||||
});
|
||||
-40
@@ -1,40 +0,0 @@
|
||||
import { computed, signal, Component } from '@angular/core';
|
||||
import {TranslocoDirective} from '@jsverse/transloco';
|
||||
|
||||
const MAX_TITLE_LENGTH = 50;
|
||||
|
||||
@Component({
|
||||
selector: 'app-library-loading',
|
||||
standalone: true,
|
||||
imports: [TranslocoDirective],
|
||||
templateUrl: './library-loading.component.html',
|
||||
styleUrl: './library-loading.component.scss'
|
||||
})
|
||||
export class LibraryLoadingComponent {
|
||||
bookTitle = signal('');
|
||||
current = signal(0);
|
||||
total = signal(0);
|
||||
isComplete = signal(false);
|
||||
|
||||
readonly percentage = computed(() => this.total() > 0 ? Math.round((this.current() / this.total()) * 100) : 0);
|
||||
|
||||
truncateTitle(title: string): string {
|
||||
if (title.length > MAX_TITLE_LENGTH) {
|
||||
return title.substring(0, MAX_TITLE_LENGTH) + "...";
|
||||
}
|
||||
|
||||
return title;
|
||||
}
|
||||
|
||||
updateProgress(bookTitle: string, current: number, total: number): void {
|
||||
this.bookTitle.set(this.truncateTitle(bookTitle));
|
||||
this.current.set(current);
|
||||
this.total.set(total);
|
||||
this.isComplete.set(current >= total);
|
||||
}
|
||||
|
||||
onReload(): void {
|
||||
window.location.reload();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user