fix(dashboard): improve performance with OnPush change detection (#440)

* fix(dashboard): improve performance with OnPush change detection

* fix(dashboard): refactor book card component for improved readability and performance

* fix(tests): mock matchMedia for improved test compatibility

* fix(tests): matchMedia mock for better CI compatibility and support

* fix(dashboard): add clearAll method to BookBrowserScrollService and update tests

* fix(dashboard): improve performance with content visibility and refactor styles

* fix(dashboard): improve scroll performance with estimated total height and refactor layout

* fix(tests): add mock for getCurrentUser in userService for improved test coverage

* fix(dashboard): implement infinite scroll for book loading and scroll restoration

* fix(tests): fix lint error

* fix(tests): mock ResizeObserver and IntersectionObserver

* fix(tests): mock ResizeObserver and IntersectionObserver (again)

* fix(dashboard): improve accessibility and performance by updating aria attributes and optimizing animations

* fix(dashboard): improve submenu accessibility and optimize virtual scroller performance

* fix(dashboard): clean up imports and improve component structure

* fix(dashboard): correct mobile check in author browser component

* fix(dashboard): add OnDestroy lifecycle hook to BookBrowserComponent
This commit is contained in:
brios
2026-04-22 17:27:11 -04:00
committed by Zach Yale
parent a779de8f57
commit 85e8d235b6
140 changed files with 1546 additions and 1233 deletions
@@ -47,7 +47,7 @@
}
</button>
@if (!isMobile) {
@if (!isMobile()) {
<button class="display-settings-btn" (click)="displaySettingsPopover.toggle($event)">
<i class="pi pi-sliders-h"></i>
</button>
@@ -193,21 +193,20 @@
<p>{{ t('noAuthorsFound') }}</p>
</div>
} @else {
<virtual-scroller
<cdk-virtual-scroll-viewport autosize
[minBufferPx]="600" [maxBufferPx]="1200"
#scroll
[items]="authorList"
[bufferAmount]="16"
class="virtual-scroller">
<div class="author-grid"
#container
[ngStyle]="{'grid-template-columns': 'repeat(auto-fill, minmax(' + gridColumnMinWidth + ', 1fr))'}">
@for (author of scroll.viewPortItems; track author.id; let i = $index) {
*cdkVirtualFor="let row of authorRows(); let rowIndex = index"
[ngStyle]="{'grid-template-columns': 'repeat(' + gridColumns() + ', 1fr)'}">
@for (author of row; track author.id; let colIdx = $index) {
<div class="virtual-scroller-item"
[ngStyle]="{width: cardWidth + 'px', height: cardHeight + 'px'}"
[ngStyle]="{width: cardWidth() + 'px', height: cardHeight() + 'px'}"
[style.--card-scale]="authorScaleService.scaleFactor()">
<app-author-card
[author]="author"
[index]="authorList.indexOf(author)"
[index]="rowIndex * gridColumns() + colIdx"
[cacheBuster]="thumbnailCacheBusters.get(author.id) || 0"
[canQuickMatch]="canEditMetadata"
[canDelete]="canDeleteBook"
@@ -223,7 +222,7 @@
</div>
}
</div>
</virtual-scroller>
</cdk-virtual-scroll-viewport>
}
}
@@ -1,4 +1,5 @@
import {Component, computed, DestroyRef, effect, HostListener, inject, OnInit, signal, ViewChild} from '@angular/core';
import {computeGridColumns} from '../../../../shared/util/viewport.util';
import {NgStyle} from '@angular/common';
import {FormsModule} from '@angular/forms';
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
@@ -12,12 +13,14 @@ import {Button} from 'primeng/button';
import {Divider} from 'primeng/divider';
import {Tooltip} from 'primeng/tooltip';
import {TranslocoDirective, TranslocoService} from '@jsverse/transloco';
import {VirtualScrollerComponent, VirtualScrollerModule} from '@iharbeck/ngx-virtual-scroller';
import {CdkVirtualScrollViewport, CdkVirtualForOf} from '@angular/cdk/scrolling';
import {CdkAutoSizeVirtualScroll} from '@angular/cdk-experimental/scrolling';
import {BookBrowserScrollService} from '../../../book/components/book-browser/book-browser-scroll.service';
import {MessageService} from 'primeng/api';
import {AuthorService} from '../../service/author.service';
import {AuthorSummary, EnrichedAuthor, AuthorFilters, DEFAULT_AUTHOR_FILTERS} from '../../model/author.model';
import {AuthorCardComponent} from '../author-card/author-card.component';
import {chunk} from '../../../../shared/util/array.util';
import {AuthorScalePreferenceService} from '../../service/author-scale-preference.service';
import {AuthorSelectionService, AuthorCheckboxClickEvent} from '../../service/author-selection.service';
import {PageTitleService} from '../../../../shared/service/page-title.service';
@@ -68,7 +71,9 @@ const DEFAULT_SORT_DIRECTIONS: Record<string, SortDirection> = {
Tooltip,
TranslocoDirective,
AuthorCardComponent,
VirtualScrollerModule
CdkVirtualScrollViewport,
CdkVirtualForOf,
CdkAutoSizeVirtualScroll,
]
})
export class AuthorBrowserComponent implements OnInit {
@@ -91,10 +96,27 @@ export class AuthorBrowserComponent implements OnInit {
protected authorScaleService = inject(AuthorScalePreferenceService);
protected selectionService = inject(AuthorSelectionService);
@ViewChild('scroll')
virtualScroller: VirtualScrollerComponent | undefined;
virtualScroller: CdkVirtualScrollViewport | undefined;
screenWidth = window.innerWidth;
@ViewChild('scroll')
set scrollViewport(vp: CdkVirtualScrollViewport | undefined) {
this.virtualScroller = vp;
this.viewportResizeObserver?.disconnect();
if (vp) {
const el = vp.elementRef.nativeElement as HTMLElement;
this.viewportWidth.set(el.clientWidth);
this.viewportResizeObserver = new ResizeObserver(entries => {
this.viewportWidth.set(entries[0]?.contentRect.width ?? el.clientWidth);
});
this.viewportResizeObserver.observe(el);
}
}
private static readonly GRID_GAP = 20;
private readonly viewportWidth = signal(0);
private viewportResizeObserver: ResizeObserver | undefined;
readonly screenWidth = signal(typeof window !== 'undefined' ? window.innerWidth : 1024);
thumbnailCacheBusters = new Map<number, number>();
private selectedAuthors = this.selectionService.selectedAuthors;
private allAuthorsState = signal<AuthorSummary[] | null>(null);
@@ -178,30 +200,34 @@ export class AuthorBrowserComponent implements OnInit {
@HostListener('window:resize')
onResize(): void {
this.screenWidth = window.innerWidth;
this.screenWidth.set(window.innerWidth);
}
get isMobile(): boolean {
return this.screenWidth <= 767;
}
readonly isMobile = computed(() => this.screenWidth() <= 767);
get cardWidth(): number {
const base = this.isMobile
readonly cardWidth = computed(() => {
const base = this.isMobile()
? AuthorBrowserComponent.MOBILE_BASE_WIDTH
: AuthorBrowserComponent.BASE_WIDTH;
return Math.round(base * this.authorScaleService.scaleFactor());
}
});
get cardHeight(): number {
const base = this.isMobile
readonly cardHeight = computed(() => {
const base = this.isMobile()
? AuthorBrowserComponent.MOBILE_BASE_HEIGHT
: AuthorBrowserComponent.BASE_HEIGHT;
return Math.round(base * this.authorScaleService.scaleFactor());
}
});
get gridColumnMinWidth(): string {
return `${this.cardWidth}px`;
}
readonly gridColumnMinWidth = computed(() => `${this.cardWidth()}px`);
readonly gridColumns = computed(() => {
return computeGridColumns(this.viewportWidth(), this.cardWidth() || 165, AuthorBrowserComponent.GRID_GAP);
});
readonly authorRows = computed(() => {
return chunk(this.filteredAuthors(), this.gridColumns());
});
sortOptions: SortOption[] = [];
@@ -212,6 +238,7 @@ export class AuthorBrowserComponent implements OnInit {
ngOnInit(): void {
this.pageTitle.setPageTitle(this.t.translate('authorBrowser.pageTitle'));
this.destroyRef.onDestroy(() => this.viewportResizeObserver?.disconnect());
this.sortOptions = [
{label: this.t.translate('authorBrowser.sort.name'), value: 'name'},
@@ -236,6 +263,8 @@ export class AuthorBrowserComponent implements OnInit {
this.destroyRef.onDestroy(() => this.selectionService.deselectAll());
}
onSearchChange(value: string): void {
this.searchTerm.set(value);
}
@@ -515,9 +544,9 @@ export class AuthorBrowserComponent implements OnInit {
}
private saveScrollPosition(): void {
if (this.virtualScroller?.viewPortInfo) {
if (this.virtualScroller) {
const key = this.getScrollPositionKey();
const position = this.virtualScroller.viewPortInfo.scrollStartPosition ?? 0;
const position = this.virtualScroller.measureScrollOffset('top') ?? 0;
this.scrollService.savePosition(key, position);
}
}
@@ -1,3 +1,8 @@
:host {
content-visibility: auto;
contain-intrinsic-size: auto 250px 300px;
}
.author-card {
background-color: var(--card-background);
border-radius: 8px;
@@ -1,7 +1,7 @@
import {ChangeDetectorRef, Component, EventEmitter, inject, Input, OnChanges, Output, SimpleChanges} from '@angular/core';
import {TranslocoPipe, TranslocoService} from '@jsverse/transloco';
import {Tooltip} from 'primeng/tooltip';
import {CheckboxChangeEvent, CheckboxModule} from 'primeng/checkbox';
import {CheckboxChangeEvent, Checkbox} from 'primeng/checkbox';
import {FormsModule} from '@angular/forms';
import {TieredMenu} from 'primeng/tieredmenu';
import {Button} from 'primeng/button';
@@ -14,7 +14,7 @@ import {AuthorService} from '../../service/author.service';
standalone: true,
templateUrl: './author-card.component.html',
styleUrls: ['./author-card.component.scss'],
imports: [TranslocoPipe, Tooltip, CheckboxModule, FormsModule, TieredMenu, Button]
imports: [TranslocoPipe, Tooltip, Checkbox, FormsModule, TieredMenu, Button]
})
export class AuthorCardComponent implements OnChanges {
@@ -104,14 +104,14 @@
<p>{{ t('noBooksFound') }}</p>
</div>
} @else {
<virtual-scroller #scroll [items]="books" [bufferAmount]="16" class="books-virtual-scroller">
<div class="books-grid" #container
[ngStyle]="{'grid-template-columns': 'repeat(auto-fill, minmax(' + gridColumnMinWidth + ', 1fr))'}">
@for (book of scroll.viewPortItems; track book.id; let i = $index) {
<cdk-virtual-scroll-viewport autosize [minBufferPx]="600" [maxBufferPx]="1200" class="books-virtual-scroller">
<div *cdkVirtualFor="let row of bookRows(); let rowIdx = index" class="books-grid"
[ngStyle]="{'grid-template-columns': 'repeat(' + gridColumns() + ', 1fr)'}">
@for (book of row; let j = $index; track book.id) {
<div class="virtual-scroller-item"
[ngStyle]="{width: currentCardSize.width + 'px', height: currentCardSize.height + 'px'}">
<app-book-card
[index]="books.indexOf(book)"
[index]="rowIdx * gridColumns() + j"
[book]="book"
[isSeriesCollapsed]="false"
[overlayPreferenceService]="bookCardOverlayPreferenceService">
@@ -119,7 +119,7 @@
</div>
}
</div>
</virtual-scroller>
</cdk-virtual-scroll-viewport>
}
</div>
}
@@ -161,7 +161,6 @@
width: 100%;
height: calc(100dvh - 22rem);
box-sizing: border-box;
overflow-y: auto;
scrollbar-width: thin;
scrollbar-color: var(--primary-color) rgb(20 20 30 / 50%);
@@ -1,4 +1,5 @@
import {AfterViewChecked, Component, computed, ElementRef, inject, OnInit, signal, ViewChild} from '@angular/core';
import {AfterViewChecked, Component, computed, DestroyRef, ElementRef, inject, OnInit, signal, ViewChild} from '@angular/core';
import {computeGridColumns} from '../../../../shared/util/viewport.util';
import {ActivatedRoute, Router} from '@angular/router';
import {NgClass, NgStyle} from '@angular/common';
import {Tab, TabList, TabPanel, TabPanels, Tabs} from 'primeng/tabs';
@@ -6,7 +7,8 @@ import {ProgressSpinner} from 'primeng/progressspinner';
import {Button} from 'primeng/button';
import {Tag} from 'primeng/tag';
import {TranslocoDirective, TranslocoService} from '@jsverse/transloco';
import {VirtualScrollerModule} from '@iharbeck/ngx-virtual-scroller';
import {CdkVirtualScrollViewport, CdkVirtualForOf} from '@angular/cdk/scrolling';
import {CdkAutoSizeVirtualScroll} from '@angular/cdk-experimental/scrolling';
import {MessageService} from 'primeng/api';
import {Tooltip} from 'primeng/tooltip';
import {AuthorService} from '../../service/author.service';
@@ -19,6 +21,7 @@ import {UserService} from '../../../settings/user-management/user.service';
import {AuthorMatchComponent} from '../author-match/author-match.component';
import {AuthorEditorComponent} from '../author-editor/author-editor.component';
import {PageTitleService} from '../../../../shared/service/page-title.service';
import {chunk} from '../../../../shared/util/array.util';
@Component({
selector: 'app-author-detail',
@@ -38,7 +41,9 @@ import {PageTitleService} from '../../../../shared/service/page-title.service';
Tag,
TranslocoDirective,
Tooltip,
VirtualScrollerModule,
CdkVirtualScrollViewport,
CdkVirtualForOf,
CdkAutoSizeVirtualScroll,
BookCardComponent,
AuthorMatchComponent,
AuthorEditorComponent
@@ -46,6 +51,8 @@ import {PageTitleService} from '../../../../shared/service/page-title.service';
})
export class AuthorDetailComponent implements OnInit, AfterViewChecked {
private static readonly GRID_GAP = 21;
private route = inject(ActivatedRoute);
private router = inject(Router);
private authorService = inject(AuthorService);
@@ -56,8 +63,27 @@ export class AuthorDetailComponent implements OnInit, AfterViewChecked {
protected userService = inject(UserService);
private pageTitle = inject(PageTitleService);
private t = inject(TranslocoService);
private destroyRef = inject(DestroyRef);
@ViewChild('descriptionContent') descriptionContentRef?: ElementRef<HTMLElement>;
virtualScroller?: CdkVirtualScrollViewport;
@ViewChild(CdkVirtualScrollViewport)
set scrollViewport(vp: CdkVirtualScrollViewport | undefined) {
this.virtualScroller = vp;
this.viewportResizeObserver?.disconnect();
if (vp) {
const el = vp.elementRef.nativeElement as HTMLElement;
this.viewportWidth.set(el.clientWidth);
this.viewportResizeObserver = new ResizeObserver(entries => {
this.viewportWidth.set(entries[0]?.contentRect.width ?? el.clientWidth);
});
this.viewportResizeObserver.observe(el);
}
}
private readonly viewportWidth = signal(0);
private viewportResizeObserver: ResizeObserver | undefined;
loading = true;
tab = 'books';
@@ -87,6 +113,14 @@ export class AuthorDetailComponent implements OnInit, AfterViewChecked {
return this.coverScalePreferenceService.gridColumnMinWidth();
}
readonly gridColumns = computed(() => {
return computeGridColumns(this.viewportWidth(), parseInt(this.gridColumnMinWidth, 10) || 180, AuthorDetailComponent.GRID_GAP);
});
readonly bookRows = computed(() => {
return chunk(this.authorBooks(), this.gridColumns());
});
get photoUrl(): string {
const author = this.author();
if (!author) return '';
@@ -105,8 +139,11 @@ export class AuthorDetailComponent implements OnInit, AfterViewChecked {
this.tab = tabParam;
}
this.loadAuthor(authorId);
this.destroyRef.onDestroy(() => this.viewportResizeObserver?.disconnect());
}
ngAfterViewChecked(): void {
if (!this.isExpanded && this.descriptionContentRef) {
const el = this.descriptionContentRef.nativeElement;