perf(ui): decouple book browser rendering pipeline (#341)
* perf(ui): deferred processing for book browser * perf(ui): Further optimisation + toolbar tweaks * perf(ui): layered pipeline, toolbar and error-state fixes - Layered computed signals for memoized pipeline stages - bookIndexById map replaces O(n) indexOf in template - Remove BookFilterOrchestrationService - Filter sidebar icon: pi-filter / pi-filter-fill - View toggle moved into display settings popover - Toolbar alignment fix - Error state no longer stuck on skeleton * perf(ui): split cache pathways + added specs * perf(ui): final spec updates * perf(ui): revert view toggle change * perf(ui): review followup * perf(ui): make spec loading state explicit
This commit is contained in:
+50
-27
@@ -21,7 +21,7 @@
|
|||||||
{{ entityType }}: {{ entity()?.name }}{{ (isFilterActive || hasSearchTerm) ? ' ' + t('labels.filteredSuffix') : '' }}
|
{{ entityType }}: {{ entity()?.name }}{{ (isFilterActive || hasSearchTerm) ? ' ' + t('labels.filteredSuffix') : '' }}
|
||||||
}
|
}
|
||||||
</p>
|
</p>
|
||||||
@if (seriesCollapseFilter.seriesCollapsed() && books(); as books) {
|
@if (!isBooksLoading() && seriesCollapseFilter.seriesCollapsed() && books(); as books) {
|
||||||
<p class="series-collapsed-info">
|
<p class="series-collapsed-info">
|
||||||
{{ t('labels.seriesCollapsedInfo', { count: books.length, itemWord: books.length === 1 ? t('labels.item') : t('labels.items') }) }}
|
{{ t('labels.seriesCollapsedInfo', { count: books.length, itemWord: books.length === 1 ? t('labels.item') : t('labels.items') }) }}
|
||||||
</p>
|
</p>
|
||||||
@@ -36,7 +36,6 @@
|
|||||||
icon="pi pi-ellipsis-v"
|
icon="pi pi-ellipsis-v"
|
||||||
[rounded]="true"
|
[rounded]="true"
|
||||||
[text]="true"
|
[text]="true"
|
||||||
severity="info"
|
|
||||||
(click)="entitymenu.toggle($event)">
|
(click)="entitymenu.toggle($event)">
|
||||||
</p-button>
|
</p-button>
|
||||||
<p-menu #entitymenu [model]="entityOptions()" [popup]="true" appendTo="body"></p-menu>
|
<p-menu #entitymenu [model]="entityOptions()" [popup]="true" appendTo="body"></p-menu>
|
||||||
@@ -91,6 +90,13 @@
|
|||||||
</p-popover>
|
</p-popover>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
<a class="topbar-items topbar-item" role="button" tabindex="0" (click)="toggleTableGrid()" (keydown.enter)="toggleTableGrid()">
|
||||||
|
<i
|
||||||
|
[ngClass]="viewIcon"
|
||||||
|
[pTooltip]="t('tooltip.toggleView')"
|
||||||
|
tooltipPosition="top">
|
||||||
|
</i>
|
||||||
|
</a>
|
||||||
<div>
|
<div>
|
||||||
<a
|
<a
|
||||||
class="topbar-items topbar-item"
|
class="topbar-items topbar-item"
|
||||||
@@ -189,13 +195,6 @@
|
|||||||
</p-popover>
|
</p-popover>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<a class="topbar-items topbar-item" role="button" tabindex="0" (click)="toggleTableGrid()" (keydown.enter)="toggleTableGrid()">
|
|
||||||
<i
|
|
||||||
[ngClass]="viewIcon"
|
|
||||||
[pTooltip]="t('tooltip.toggleView')"
|
|
||||||
tooltipPosition="top">
|
|
||||||
</i>
|
|
||||||
</a>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- MOBILE SEARCH (only visible below md) -->
|
<!-- MOBILE SEARCH (only visible below md) -->
|
||||||
@@ -256,7 +255,7 @@
|
|||||||
|
|
||||||
<a class="topbar-items topbar-item" role="button" tabindex="0" (click)="toggleSidebar()" (keydown.enter)="toggleSidebar()">
|
<a class="topbar-items topbar-item" role="button" tabindex="0" (click)="toggleSidebar()" (keydown.enter)="toggleSidebar()">
|
||||||
<i
|
<i
|
||||||
[ngClass]="showFilter() ? 'pi pi-angle-double-right' : 'pi pi-angle-double-left'"
|
[ngClass]="showFilter() ? 'pi pi-filter-fill' : 'pi pi-filter'"
|
||||||
[pTooltip]="t('tooltip.toggleSidebar')"
|
[pTooltip]="t('tooltip.toggleSidebar')"
|
||||||
tooltipPosition="top">
|
tooltipPosition="top">
|
||||||
</i>
|
</i>
|
||||||
@@ -264,21 +263,45 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@if (books(); as books) {
|
@if (showTableLoadingPlaceholder) {
|
||||||
<div>
|
<div class="table-skeleton" aria-hidden="true">
|
||||||
@if (isBooksLoading()) {
|
<div class="table-skeleton-header">
|
||||||
<div>
|
@for (column of tableSkeletonColumns; track column) {
|
||||||
<p-progressSpinner></p-progressSpinner>
|
<div class="table-skeleton-cell table-skeleton-cell-header"></div>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
@for (row of tableSkeletonRows; track row) {
|
||||||
|
<div class="table-skeleton-row">
|
||||||
|
@for (column of tableSkeletonColumns; track column) {
|
||||||
|
<div class="table-skeleton-cell" [class.table-skeleton-cell-short]="column !== 0"></div>
|
||||||
|
}
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
<div class="book-state-wrapper">
|
</div>
|
||||||
@if (booksError()) {
|
} @else if (showGridLoadingPlaceholder) {
|
||||||
<div class="no-books-container">
|
<div class="skeleton-grid"
|
||||||
<p class="no-books-text">
|
[ngStyle]="{'grid-template-columns': 'repeat(auto-fill, minmax(' + gridColumnMinWidth + ', 1fr))'}">
|
||||||
{{ entityType() === EntityType.LIBRARY ? t('labels.failedLibrary') : t('labels.failedShelf') }}
|
@for (item of skeletonSlots; track item) {
|
||||||
</p>
|
<div class="skeleton-card"
|
||||||
</div>
|
[ngStyle]="{width: currentCardSize.width + 'px', height: currentCardSize.height + 'px'}">
|
||||||
} @else if (!isBooksLoading() && books.length === 0) {
|
</div>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
} @else if (booksError()) {
|
||||||
|
<div class="no-books-container">
|
||||||
|
<p class="no-books-text">
|
||||||
|
{{ entityType() === EntityType.LIBRARY ? t('labels.failedLibrary') : t('labels.failedShelf') }}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
} @else if (books(); as books) {
|
||||||
|
<div class="book-state-wrapper">
|
||||||
|
<div
|
||||||
|
class="book-state-content"
|
||||||
|
[attr.aria-busy]="isBooksRefreshing() ? 'true' : 'false'"
|
||||||
|
[class.book-state-content-refreshing]="isBooksRefreshing()"
|
||||||
|
[attr.inert]="isBooksRefreshing() ? '' : null">
|
||||||
|
@if (books.length === 0 && !isBooksLoading()) {
|
||||||
<div class="no-books-container">
|
<div class="no-books-container">
|
||||||
<p class="no-books-text">
|
<p class="no-books-text">
|
||||||
{{ t('labels.noBooks') }}
|
{{ t('labels.noBooks') }}
|
||||||
@@ -305,11 +328,11 @@
|
|||||||
class="virtual-scroller-item"
|
class="virtual-scroller-item"
|
||||||
[ngStyle]="{width: currentCardSize.width + 'px', height: getCardHeight(book) + 'px'}">
|
[ngStyle]="{width: currentCardSize.width + 'px', height: getCardHeight(book) + 'px'}">
|
||||||
<app-book-card
|
<app-book-card
|
||||||
[index]="books.indexOf(book)"
|
[index]="bookIndexById().get(book.id) ?? i"
|
||||||
[book]="book"
|
[book]="book"
|
||||||
[isCheckboxEnabled]="true"
|
[isCheckboxEnabled]="true"
|
||||||
[seriesViewEnabled]="seriesViewEnabled"
|
[seriesViewEnabled]="seriesViewEnabled"
|
||||||
[onBookSelect]="handleBookSelect.bind(this)"
|
[onBookSelect]="onBookCardSelect"
|
||||||
[isSeriesCollapsed]="seriesCollapseFilter.seriesCollapsed()"
|
[isSeriesCollapsed]="seriesCollapseFilter.seriesCollapsed()"
|
||||||
(checkboxClick)="onCheckboxClicked($event)"
|
(checkboxClick)="onCheckboxClicked($event)"
|
||||||
[isSelected]="selectedBooks().has(book.id)"
|
[isSelected]="selectedBooks().has(book.id)"
|
||||||
@@ -442,8 +465,8 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="footer-right"></div>
|
<div class="footer-right"></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,74 @@
|
|||||||
|
// Skeleton loader — opacity animation runs on the compositor thread
|
||||||
|
// so it stays smooth while the main thread computes books.
|
||||||
|
.skeleton-grid {
|
||||||
|
display: grid;
|
||||||
|
gap: 21px;
|
||||||
|
align-items: start;
|
||||||
|
width: 100%;
|
||||||
|
|
||||||
|
@media (width <= 767px) {
|
||||||
|
gap: 0.5rem 8px;
|
||||||
|
padding: 0 0.5rem;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeleton-card {
|
||||||
|
background: var(--p-content-border-color);
|
||||||
|
border-radius: 8px;
|
||||||
|
animation: skeleton-pulse 1.5s ease-in-out infinite;
|
||||||
|
will-change: opacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
.table-skeleton {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.75rem;
|
||||||
|
padding: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.table-skeleton-header,
|
||||||
|
.table-skeleton-row {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(5, minmax(0, 1fr));
|
||||||
|
gap: 1rem;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.table-skeleton-row {
|
||||||
|
min-height: 2.75rem;
|
||||||
|
padding-bottom: 0.75rem;
|
||||||
|
border-bottom: 1px solid var(--p-content-border-color);
|
||||||
|
}
|
||||||
|
|
||||||
|
.table-skeleton-cell {
|
||||||
|
height: 0.875rem;
|
||||||
|
border-radius: 999px;
|
||||||
|
background: var(--p-content-border-color);
|
||||||
|
animation: skeleton-pulse 1.5s ease-in-out infinite;
|
||||||
|
|
||||||
|
&.table-skeleton-cell-header {
|
||||||
|
height: 0.75rem;
|
||||||
|
opacity: 0.75;
|
||||||
|
}
|
||||||
|
|
||||||
|
&.table-skeleton-cell-short {
|
||||||
|
width: 70%;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes skeleton-pulse {
|
||||||
|
0%, 100% { opacity: 0.3; }
|
||||||
|
50% { opacity: 0.6; }
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-reduced-motion: reduce) {
|
||||||
|
.skeleton-card,
|
||||||
|
.table-skeleton-cell {
|
||||||
|
animation: none;
|
||||||
|
will-change: auto;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Layout containers
|
// Layout containers
|
||||||
.book-browser-container {
|
.book-browser-container {
|
||||||
position: relative;
|
position: relative;
|
||||||
@@ -16,11 +87,11 @@
|
|||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
width: 100%;
|
|
||||||
gap: 0.25rem;
|
gap: 0.25rem;
|
||||||
padding: 0.35rem;
|
padding: 0.35rem;
|
||||||
border-radius: 0.75rem 0.75rem 0 0;
|
border-radius: 0.75rem 0.75rem 0 0;
|
||||||
margin-bottom: 0.5rem;
|
margin-bottom: 0.5rem;
|
||||||
|
margin-right: 2px;
|
||||||
background-color: var(--card-background);
|
background-color: var(--card-background);
|
||||||
border: 1px solid var(--p-content-border-color);
|
border: 1px solid var(--p-content-border-color);
|
||||||
|
|
||||||
@@ -538,3 +609,7 @@
|
|||||||
.book-state-wrapper {
|
.book-state-wrapper {
|
||||||
position: relative;
|
position: relative;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.book-state-content-refreshing {
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
|||||||
+382
-7
@@ -1,10 +1,385 @@
|
|||||||
import {describe, expect, it} from 'vitest';
|
import {signal, WritableSignal} from '@angular/core';
|
||||||
|
import {TestBed} from '@angular/core/testing';
|
||||||
|
import {ActivatedRoute, convertToParamMap, ParamMap, Router} from '@angular/router';
|
||||||
|
import {BehaviorSubject, Subject} from 'rxjs';
|
||||||
|
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
|
||||||
|
import {ConfirmationService, MessageService} from 'primeng/api';
|
||||||
|
|
||||||
// TODO(seam): This needs a routed integration harness for the query-backed book browser,
|
import {PageTitleService} from '../../../../shared/service/page-title.service';
|
||||||
// PrimeNG overlays, and the persisted filter/sidebar preference services before the
|
import {BookService} from '../../service/book.service';
|
||||||
// control-flow branches can be exercised without brittle template scaffolding.
|
import {BookMetadataManageService} from '../../service/book-metadata-manage.service';
|
||||||
describe.skip('BookBrowserComponent', () => {
|
import {LibraryShelfMenuService} from '../../service/library-shelf-menu.service';
|
||||||
it('needs a browser-level harness for routed query state, filters, and overlay actions', () => {
|
import {Book} from '../../model/book.model';
|
||||||
expect.hasAssertions();
|
import {SortDirection} from '../../model/sort.model';
|
||||||
|
import {UserService} from '../../../settings/user-management/user.service';
|
||||||
|
import {SeriesCollapseFilter} from './filters/SeriesCollapseFilter';
|
||||||
|
import {CoverScalePreferenceService} from './cover-scale-preference.service';
|
||||||
|
import {BookDialogHelperService} from './book-dialog-helper.service';
|
||||||
|
import {TableColumnPreferenceService} from './table-column-preference.service';
|
||||||
|
import {BookMenuService} from '../../service/book-menu.service';
|
||||||
|
import {SidebarFilterTogglePrefService} from './filters/sidebar-filter-toggle-pref.service';
|
||||||
|
import {TaskHelperService} from '../../../settings/task-management/task-helper.service';
|
||||||
|
import {LoadingService} from '../../../../core/services/loading.service';
|
||||||
|
import {LocalStorageService} from '../../../../shared/service/local-storage.service';
|
||||||
|
import {BookNavigationService} from '../../service/book-navigation.service';
|
||||||
|
import {BookCardOverlayPreferenceService} from './book-card-overlay-preference.service';
|
||||||
|
import {BookSelectionService} from './book-selection.service';
|
||||||
|
import {BookBrowserQueryParamsService, VIEW_MODES} from './book-browser-query-params.service';
|
||||||
|
import {BookBrowserEntityService} from './book-browser-entity.service';
|
||||||
|
import {BookBrowserScrollService} from './book-browser-scroll.service';
|
||||||
|
import {AppSettingsService} from '../../../../shared/service/app-settings.service';
|
||||||
|
import {BookBrowserComponent, EntityType} from './book-browser.component';
|
||||||
|
import {SortService} from '../../service/sort.service';
|
||||||
|
import {TranslocoService} from '@jsverse/transloco';
|
||||||
|
|
||||||
|
function makeBook(id: number, libraryId: number, title: string, addedOn: string): Book {
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
libraryId,
|
||||||
|
metadata: {
|
||||||
|
bookId: id,
|
||||||
|
title,
|
||||||
|
},
|
||||||
|
addedOn,
|
||||||
|
} as Book;
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeCurrentUser() {
|
||||||
|
return {
|
||||||
|
id: 1,
|
||||||
|
username: 'tester',
|
||||||
|
name: 'Tester',
|
||||||
|
email: 'tester@example.com',
|
||||||
|
assignedLibraries: [],
|
||||||
|
permissions: {
|
||||||
|
admin: false,
|
||||||
|
canUpload: false,
|
||||||
|
canDownload: false,
|
||||||
|
canEmailBook: false,
|
||||||
|
canDeleteBook: false,
|
||||||
|
canEditMetadata: false,
|
||||||
|
canManageLibrary: false,
|
||||||
|
canManageMetadataConfig: false,
|
||||||
|
canSyncKoReader: false,
|
||||||
|
canSyncKobo: false,
|
||||||
|
canAccessOpds: false,
|
||||||
|
canAccessBookdrop: false,
|
||||||
|
canAccessLibraryStats: false,
|
||||||
|
canAccessUserStats: false,
|
||||||
|
canAccessTaskManager: false,
|
||||||
|
canManageEmailConfig: false,
|
||||||
|
canManageGlobalPreferences: false,
|
||||||
|
canManageIcons: false,
|
||||||
|
canManageFonts: false,
|
||||||
|
demoUser: false,
|
||||||
|
canBulkAutoFetchMetadata: false,
|
||||||
|
canBulkCustomFetchMetadata: false,
|
||||||
|
canBulkEditMetadata: false,
|
||||||
|
canBulkRegenerateCover: false,
|
||||||
|
canMoveOrganizeFiles: false,
|
||||||
|
canBulkLockUnlockMetadata: false,
|
||||||
|
},
|
||||||
|
userSettings: {
|
||||||
|
filterMode: 'and',
|
||||||
|
enableSeriesView: false,
|
||||||
|
visibleSortFields: ['addedOn', 'title'],
|
||||||
|
entityViewPreferences: {
|
||||||
|
global: {
|
||||||
|
sortKey: 'addedOn',
|
||||||
|
sortDir: 'DESC',
|
||||||
|
view: 'GRID',
|
||||||
|
coverSize: 1,
|
||||||
|
seriesCollapsed: false,
|
||||||
|
overlayBookType: true,
|
||||||
|
},
|
||||||
|
overrides: [],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as const;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface BookBrowserHarness {
|
||||||
|
component: BookBrowserComponent;
|
||||||
|
books: WritableSignal<Book[]>;
|
||||||
|
booksError: WritableSignal<string | null>;
|
||||||
|
isBooksLoading: WritableSignal<boolean>;
|
||||||
|
paramMap$: BehaviorSubject<ParamMap>;
|
||||||
|
queryParamsService: {
|
||||||
|
shouldForceExpandSeries: ReturnType<typeof vi.fn>;
|
||||||
|
updateViewMode: ReturnType<typeof vi.fn>;
|
||||||
|
updateFilters: ReturnType<typeof vi.fn>;
|
||||||
|
updateFilterMode: ReturnType<typeof vi.fn>;
|
||||||
|
updateMultiSort: ReturnType<typeof vi.fn>;
|
||||||
|
};
|
||||||
|
routeSnapshot: {
|
||||||
|
routeConfig: {path: string};
|
||||||
|
paramMap: ParamMap;
|
||||||
|
queryParamMap: ParamMap;
|
||||||
|
params: Record<string, string>;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function createHarness(options?: {
|
||||||
|
books?: Book[];
|
||||||
|
booksError?: string | null;
|
||||||
|
isBooksLoading?: boolean;
|
||||||
|
translate?: (key: string) => string;
|
||||||
|
}): BookBrowserHarness {
|
||||||
|
const books = signal<Book[]>(
|
||||||
|
options?.books ?? [
|
||||||
|
makeBook(1, 1, 'Alpha', '2024-01-01T00:00:00Z'),
|
||||||
|
makeBook(2, 1, 'Zulu', '2024-02-01T00:00:00Z'),
|
||||||
|
makeBook(3, 2, 'Bravo', '2024-03-01T00:00:00Z'),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
const booksError = signal<string | null>(options?.booksError ?? null);
|
||||||
|
const isBooksLoading = signal<boolean>(options?.isBooksLoading ?? false);
|
||||||
|
const currentUser = signal(makeCurrentUser());
|
||||||
|
const showFilter = signal(false);
|
||||||
|
const seriesCollapsed = signal(false);
|
||||||
|
const routerEvents$ = new Subject<unknown>();
|
||||||
|
const paramMap$ = new BehaviorSubject(convertToParamMap({libraryId: '1'}));
|
||||||
|
const queryParamMap$ = new BehaviorSubject(convertToParamMap({}));
|
||||||
|
const url$ = new BehaviorSubject<unknown[]>([]);
|
||||||
|
const queryParamsService = {
|
||||||
|
shouldForceExpandSeries: vi.fn(() => false),
|
||||||
|
updateViewMode: vi.fn(),
|
||||||
|
updateFilters: vi.fn(),
|
||||||
|
updateFilterMode: vi.fn(),
|
||||||
|
updateMultiSort: vi.fn(),
|
||||||
|
};
|
||||||
|
const routeSnapshot = {
|
||||||
|
routeConfig: {path: 'library/:libraryId/books'},
|
||||||
|
paramMap: paramMap$.value,
|
||||||
|
queryParamMap: queryParamMap$.value,
|
||||||
|
params: {libraryId: '1'},
|
||||||
|
};
|
||||||
|
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
providers: [
|
||||||
|
SortService,
|
||||||
|
BookSelectionService,
|
||||||
|
BookNavigationService,
|
||||||
|
BookBrowserScrollService,
|
||||||
|
{
|
||||||
|
provide: UserService,
|
||||||
|
useValue: {
|
||||||
|
currentUser: currentUser.asReadonly(),
|
||||||
|
getCurrentUser: vi.fn(() => currentUser()),
|
||||||
|
updateUserSetting: vi.fn(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provide: CoverScalePreferenceService,
|
||||||
|
useValue: {
|
||||||
|
currentCardSize: vi.fn(() => ({width: 120, height: 160})),
|
||||||
|
gridColumnMinWidth: vi.fn(() => '120px'),
|
||||||
|
getCardHeight: vi.fn(() => 160),
|
||||||
|
scaleFactor: vi.fn(() => 1),
|
||||||
|
setScale: vi.fn(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provide: TableColumnPreferenceService,
|
||||||
|
useValue: {
|
||||||
|
allColumns: [],
|
||||||
|
visibleColumns: [],
|
||||||
|
initPreferences: vi.fn(),
|
||||||
|
saveVisibleColumns: vi.fn(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provide: SidebarFilterTogglePrefService,
|
||||||
|
useValue: {
|
||||||
|
showFilter: showFilter.asReadonly(),
|
||||||
|
toggle: vi.fn(() => showFilter.update(value => !value)),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provide: SeriesCollapseFilter,
|
||||||
|
useValue: {
|
||||||
|
seriesCollapsed: seriesCollapsed.asReadonly(),
|
||||||
|
setContext: vi.fn(),
|
||||||
|
collapseBooks: vi.fn((items: Book[]) => items),
|
||||||
|
setCollapsed: vi.fn((value: boolean) => seriesCollapsed.set(value)),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{provide: ConfirmationService, useValue: {confirm: vi.fn()}},
|
||||||
|
{provide: TaskHelperService, useValue: {}},
|
||||||
|
{
|
||||||
|
provide: BookCardOverlayPreferenceService,
|
||||||
|
useValue: {
|
||||||
|
showBookTypePill: vi.fn(() => true),
|
||||||
|
setShowBookTypePill: vi.fn(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{provide: AppSettingsService, useValue: {appSettings: vi.fn(() => null)}},
|
||||||
|
{
|
||||||
|
provide: ActivatedRoute,
|
||||||
|
useValue: {
|
||||||
|
url: url$.asObservable(),
|
||||||
|
paramMap: paramMap$.asObservable(),
|
||||||
|
queryParamMap: queryParamMap$.asObservable(),
|
||||||
|
snapshot: routeSnapshot,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provide: Router,
|
||||||
|
useValue: {
|
||||||
|
events: routerEvents$.asObservable(),
|
||||||
|
navigate: vi.fn(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{provide: MessageService, useValue: {add: vi.fn()}},
|
||||||
|
{
|
||||||
|
provide: BookService,
|
||||||
|
useValue: {
|
||||||
|
books: books.asReadonly(),
|
||||||
|
isBooksLoading: isBooksLoading.asReadonly(),
|
||||||
|
booksError: booksError.asReadonly(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{provide: BookMetadataManageService, useValue: {}},
|
||||||
|
{provide: BookDialogHelperService, useValue: {}},
|
||||||
|
{
|
||||||
|
provide: BookMenuService,
|
||||||
|
useValue: {
|
||||||
|
getMoreActionsMenu: vi.fn(() => []),
|
||||||
|
getMetadataMenuItems: vi.fn(() => []),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provide: LibraryShelfMenuService,
|
||||||
|
useValue: {
|
||||||
|
initializeLibraryMenuItems: vi.fn(() => []),
|
||||||
|
initializeMagicShelfMenuItems: vi.fn(() => []),
|
||||||
|
initializeShelfMenuItems: vi.fn(() => []),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{provide: PageTitleService, useValue: {setPageTitle: vi.fn()}},
|
||||||
|
{provide: LoadingService, useValue: {show: vi.fn(), hide: vi.fn()}},
|
||||||
|
{provide: LocalStorageService, useValue: {get: vi.fn(), set: vi.fn()}},
|
||||||
|
{
|
||||||
|
provide: BookBrowserQueryParamsService,
|
||||||
|
useValue: queryParamsService,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provide: BookBrowserEntityService,
|
||||||
|
useValue: {
|
||||||
|
getEntityInfo: vi.fn((paramMap: ParamMap) => ({
|
||||||
|
entityId: Number(paramMap.get('libraryId')),
|
||||||
|
entityType: EntityType.LIBRARY,
|
||||||
|
})),
|
||||||
|
getEntity: vi.fn((entityId: number) => ({id: entityId, name: `Library ${entityId}`})),
|
||||||
|
getBooksByEntity: vi.fn((items: Book[], entityId: number) =>
|
||||||
|
items.filter(book => book.libraryId === entityId)
|
||||||
|
),
|
||||||
|
isLibrary: vi.fn(() => true),
|
||||||
|
isMagicShelf: vi.fn(() => false),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provide: TranslocoService,
|
||||||
|
useValue: {
|
||||||
|
translate: vi.fn((key: string) => {
|
||||||
|
if (options?.translate) {
|
||||||
|
return options.translate(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
'book.browser.tooltip.toggleView': 'Toggle between Grid and Table view',
|
||||||
|
'book.browser.labels.allBooks': 'All Books',
|
||||||
|
'book.browser.labels.unshelvedBooks': 'Unshelved Books',
|
||||||
|
}[key] ?? key;
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const component = TestBed.runInInjectionContext(() => new BookBrowserComponent());
|
||||||
|
|
||||||
|
return {
|
||||||
|
component,
|
||||||
|
books,
|
||||||
|
booksError,
|
||||||
|
isBooksLoading,
|
||||||
|
paramMap$,
|
||||||
|
queryParamsService,
|
||||||
|
routeSnapshot,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('BookBrowserComponent', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
vi.runOnlyPendingTimers();
|
||||||
|
vi.useRealTimers();
|
||||||
|
TestBed.resetTestingModule();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('switches view mode through the display settings control', () => {
|
||||||
|
const {component, queryParamsService} = createHarness();
|
||||||
|
|
||||||
|
component.currentViewMode = VIEW_MODES.GRID;
|
||||||
|
|
||||||
|
component.onViewModeChange(VIEW_MODES.TABLE);
|
||||||
|
|
||||||
|
expect(component.currentViewMode).toBe(VIEW_MODES.TABLE);
|
||||||
|
expect(queryParamsService.updateViewMode).toHaveBeenCalledWith(VIEW_MODES.TABLE);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps rendered books visible during same-context refreshes', () => {
|
||||||
|
const {component} = createHarness();
|
||||||
|
|
||||||
|
expect(component.books()).toBeUndefined();
|
||||||
|
|
||||||
|
vi.runOnlyPendingTimers();
|
||||||
|
|
||||||
|
expect(component.books()?.map(book => book.id)).toEqual([2, 1]);
|
||||||
|
expect(component.isBooksRefreshing()).toBe(false);
|
||||||
|
|
||||||
|
component.applySortCriteria([
|
||||||
|
{label: 'Title', field: 'title', direction: SortDirection.ASCENDING},
|
||||||
|
]);
|
||||||
|
TestBed.flushEffects();
|
||||||
|
|
||||||
|
expect(component.books()?.map(book => book.id)).toEqual([1, 2]);
|
||||||
|
expect(component.hasRenderedBooks()).toBe(true);
|
||||||
|
expect(component.isBooksRefreshing()).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('clears rendered books until the deferred commit completes after a context change', () => {
|
||||||
|
const {component, paramMap$, routeSnapshot} = createHarness();
|
||||||
|
|
||||||
|
vi.runOnlyPendingTimers();
|
||||||
|
expect(component.books()?.map(book => book.id)).toEqual([2, 1]);
|
||||||
|
|
||||||
|
routeSnapshot.paramMap = convertToParamMap({libraryId: '2'});
|
||||||
|
routeSnapshot.params = {libraryId: '2'};
|
||||||
|
paramMap$.next(routeSnapshot.paramMap);
|
||||||
|
TestBed.flushEffects();
|
||||||
|
|
||||||
|
expect(component.books()).toBeUndefined();
|
||||||
|
expect(component.hasRenderedBooks()).toBe(false);
|
||||||
|
|
||||||
|
vi.runOnlyPendingTimers();
|
||||||
|
|
||||||
|
expect(component.books()?.map(book => book.id)).toEqual([3]);
|
||||||
|
expect(component.hasRenderedBooks()).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('hides loading placeholders when the books query is in an error state', () => {
|
||||||
|
const {component} = createHarness({
|
||||||
|
booksError: 'Failed to load books',
|
||||||
|
isBooksLoading: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(component.showBooksLoadingPlaceholder).toBe(false);
|
||||||
|
expect(component.showGridLoadingPlaceholder).toBe(false);
|
||||||
|
expect(component.showTableLoadingPlaceholder).toBe(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import {AfterViewInit, Component, HostListener, computed, effect, inject, OnDestroy, OnInit, signal, ViewChild} from '@angular/core';
|
import {AfterViewInit, Component, HostListener, computed, effect, inject, OnDestroy, OnInit, signal, untracked, ViewChild} from '@angular/core';
|
||||||
import {toObservable, toSignal} from '@angular/core/rxjs-interop';
|
import {toObservable, toSignal} from '@angular/core/rxjs-interop';
|
||||||
import {ActivatedRoute, NavigationStart, Router} from '@angular/router';
|
import {ActivatedRoute, NavigationStart, Router} from '@angular/router';
|
||||||
import {ConfirmationService, MenuItem, MessageService} from 'primeng/api';
|
import {ConfirmationService, MenuItem, MessageService} from 'primeng/api';
|
||||||
@@ -18,7 +18,7 @@ import {Button} from 'primeng/button';
|
|||||||
import {NgClass, NgStyle} from '@angular/common';
|
import {NgClass, NgStyle} from '@angular/common';
|
||||||
import {VirtualScrollerComponent, VirtualScrollerModule} from '@iharbeck/ngx-virtual-scroller';
|
import {VirtualScrollerComponent, VirtualScrollerModule} from '@iharbeck/ngx-virtual-scroller';
|
||||||
import {BookCardComponent} from './book-card/book-card.component';
|
import {BookCardComponent} from './book-card/book-card.component';
|
||||||
import {ProgressSpinner} from 'primeng/progressspinner';
|
|
||||||
import {Menu} from 'primeng/menu';
|
import {Menu} from 'primeng/menu';
|
||||||
import {InputText} from 'primeng/inputtext';
|
import {InputText} from 'primeng/inputtext';
|
||||||
import {FormsModule} from '@angular/forms';
|
import {FormsModule} from '@angular/forms';
|
||||||
@@ -49,11 +49,15 @@ import {BookCardOverlayPreferenceService} from './book-card-overlay-preference.s
|
|||||||
import {BookSelectionService, CheckboxClickEvent} from './book-selection.service';
|
import {BookSelectionService, CheckboxClickEvent} from './book-selection.service';
|
||||||
import {BookBrowserQueryParamsService, VIEW_MODES} from './book-browser-query-params.service';
|
import {BookBrowserQueryParamsService, VIEW_MODES} from './book-browser-query-params.service';
|
||||||
import {BookBrowserEntityService, EntityInfo} from './book-browser-entity.service';
|
import {BookBrowserEntityService, EntityInfo} from './book-browser-entity.service';
|
||||||
import {BookFilterOrchestrationService} from './book-filter-orchestration.service';
|
|
||||||
import {BookBrowserScrollService} from './book-browser-scroll.service';
|
import {BookBrowserScrollService} from './book-browser-scroll.service';
|
||||||
import {AppSettingsService} from '../../../../shared/service/app-settings.service';
|
import {AppSettingsService} from '../../../../shared/service/app-settings.service';
|
||||||
import {MultiSortPopoverComponent} from './sorting/multi-sort-popover/multi-sort-popover.component';
|
import {MultiSortPopoverComponent} from './sorting/multi-sort-popover/multi-sort-popover.component';
|
||||||
import {TranslocoDirective, TranslocoService} from '@jsverse/transloco';
|
import {TranslocoDirective, TranslocoService} from '@jsverse/transloco';
|
||||||
|
import {DeferredRenderState} from './deferred-render-state';
|
||||||
|
|
||||||
|
import {SortService} from '../../service/sort.service';
|
||||||
|
import {filterBooksBySearchTerm} from './filters/HeaderFilter';
|
||||||
|
import {filterBooksByFilters} from './filters/sidebar-filter';
|
||||||
|
|
||||||
export enum EntityType {
|
export enum EntityType {
|
||||||
LIBRARY = 'Library',
|
LIBRARY = 'Library',
|
||||||
@@ -69,7 +73,7 @@ export enum EntityType {
|
|||||||
templateUrl: './book-browser.component.html',
|
templateUrl: './book-browser.component.html',
|
||||||
styleUrls: ['./book-browser.component.scss'],
|
styleUrls: ['./book-browser.component.scss'],
|
||||||
imports: [
|
imports: [
|
||||||
Button, VirtualScrollerModule, BookCardComponent, ProgressSpinner, Menu, InputText, FormsModule,
|
Button, VirtualScrollerModule, BookCardComponent, Menu, InputText, FormsModule,
|
||||||
BookTableComponent, BookFilterComponent, Tooltip, NgClass, NgStyle, Popover,
|
BookTableComponent, BookFilterComponent, Tooltip, NgClass, NgStyle, Popover,
|
||||||
Checkbox, Slider, Divider, MultiSelect, TieredMenu, BadgeModule, MultiSortPopoverComponent, TranslocoDirective
|
Checkbox, Slider, Divider, MultiSelect, TieredMenu, BadgeModule, MultiSortPopoverComponent, TranslocoDirective
|
||||||
],
|
],
|
||||||
@@ -113,7 +117,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
|
|||||||
private bookNavigationService = inject(BookNavigationService);
|
private bookNavigationService = inject(BookNavigationService);
|
||||||
private queryParamsService = inject(BookBrowserQueryParamsService);
|
private queryParamsService = inject(BookBrowserQueryParamsService);
|
||||||
private entityService = inject(BookBrowserEntityService);
|
private entityService = inject(BookBrowserEntityService);
|
||||||
private filterOrchestrationService = inject(BookFilterOrchestrationService);
|
private sortService = inject(SortService);
|
||||||
private localStorageService = inject(LocalStorageService);
|
private localStorageService = inject(LocalStorageService);
|
||||||
private scrollService = inject(BookBrowserScrollService);
|
private scrollService = inject(BookBrowserScrollService);
|
||||||
private readonly t = inject(TranslocoService);
|
private readonly t = inject(TranslocoService);
|
||||||
@@ -180,25 +184,97 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
|
|||||||
|
|
||||||
return actions;
|
return actions;
|
||||||
});
|
});
|
||||||
readonly books = computed(() => {
|
// --- Layered pipeline: each computed only recomputes when its direct inputs change ---
|
||||||
|
private readonly entityBooks = computed(() => {
|
||||||
const {entityId, entityType} = this.entityInfo();
|
const {entityId, entityType} = this.entityInfo();
|
||||||
const books = this.entityService.getBooksByEntity(this.bookService.books(), entityId, entityType);
|
return this.entityService.getBooksByEntity(this.bookService.books(), entityId, entityType);
|
||||||
|
});
|
||||||
|
private readonly searchedBooks = computed(() =>
|
||||||
|
filterBooksBySearchTerm(this.entityBooks(), this.debouncedSearchTerm())
|
||||||
|
);
|
||||||
|
private readonly filteredBooks = computed(() =>
|
||||||
|
filterBooksByFilters(this.searchedBooks(), this.selectedFilter(), this.selectedFilterMode())
|
||||||
|
);
|
||||||
|
private readonly forceExpandSeries = computed(() =>
|
||||||
|
this.queryParamsService.shouldForceExpandSeries(this.queryParamMap())
|
||||||
|
);
|
||||||
|
private readonly collapsedBooks = computed(() =>
|
||||||
|
this.seriesCollapseFilter.collapseBooks(
|
||||||
|
this.filteredBooks(), this.forceExpandSeries(), this.seriesCollapsed()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
private readonly sortedBooks = computed(() =>
|
||||||
|
this.sortService.applyMultiSort(this.collapsedBooks(), this.sortCriteria())
|
||||||
|
);
|
||||||
|
|
||||||
return this.filterOrchestrationService.applyFilters(
|
// --- Deferred render: lets skeleton/refresh indicator paint before committing ---
|
||||||
books,
|
private readonly booksRenderState = new DeferredRenderState<Book[]>();
|
||||||
this.debouncedSearchTerm(),
|
readonly books = this.booksRenderState.value;
|
||||||
this.selectedFilter(),
|
readonly hasRenderedBooks = this.booksRenderState.hasValue;
|
||||||
this.selectedFilterMode(),
|
readonly isBooksRefreshing = this.booksRenderState.isRefreshing;
|
||||||
this.seriesCollapseFilter,
|
|
||||||
this.seriesCollapsed(),
|
private lastBooksContextKey: string | null = null;
|
||||||
this.filterOrchestrationService.shouldForceExpandSeries(this.queryParamMap()),
|
private readonly booksContextKey = computed(() => {
|
||||||
this.sortCriteria()
|
const {entityId, entityType} = this.entityInfo();
|
||||||
);
|
return Number.isNaN(entityId) ? entityType : `${entityType}:${entityId}`;
|
||||||
|
});
|
||||||
|
|
||||||
|
private readonly pipelineInputs = computed(() => ({
|
||||||
|
books: this.bookService.books(),
|
||||||
|
entity: this.entityInfo(),
|
||||||
|
search: this.debouncedSearchTerm(),
|
||||||
|
filter: this.selectedFilter(),
|
||||||
|
filterMode: this.selectedFilterMode(),
|
||||||
|
collapsed: this.seriesCollapsed(),
|
||||||
|
forceExpand: this.forceExpandSeries(),
|
||||||
|
sort: this.sortCriteria(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
private readonly renderBooksEffect = effect((onCleanup) => {
|
||||||
|
const contextKey = this.booksContextKey();
|
||||||
|
this.pipelineInputs();
|
||||||
|
|
||||||
|
const shouldRefreshInPlace = untracked(() => this.hasRenderedBooks()) && contextKey === this.lastBooksContextKey;
|
||||||
|
this.lastBooksContextKey = contextKey;
|
||||||
|
|
||||||
|
if (shouldRefreshInPlace) {
|
||||||
|
// Same context (sort/filter/search change within the same entity).
|
||||||
|
// Commit synchronously — the memoized computed chain is fast and
|
||||||
|
// skipping the setTimeout avoids a one-frame skeleton flash.
|
||||||
|
const requestId = this.booksRenderState.begin('refresh');
|
||||||
|
this.booksRenderState.commit(requestId, this.sortedBooks());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Context changed (navigation between entities or first load).
|
||||||
|
// Defer so the skeleton can paint before the pipeline evaluates.
|
||||||
|
const requestId = this.booksRenderState.begin('reset');
|
||||||
|
|
||||||
|
const timeout = globalThis.setTimeout(() => {
|
||||||
|
this.booksRenderState.commit(requestId, this.sortedBooks());
|
||||||
|
});
|
||||||
|
|
||||||
|
onCleanup(() => {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
this.booksRenderState.cancel(requestId);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
readonly isBooksLoading = this.bookService.isBooksLoading;
|
readonly isBooksLoading = this.bookService.isBooksLoading;
|
||||||
readonly booksError = this.bookService.booksError;
|
readonly booksError = this.bookService.booksError;
|
||||||
|
readonly bookIndexById = computed(() => {
|
||||||
|
const books = this.books();
|
||||||
|
if (!books) return new Map<number, number>();
|
||||||
|
const map = new Map<number, number>();
|
||||||
|
for (let i = 0; i < books.length; i++) {
|
||||||
|
map.set(books[i].id, i);
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
});
|
||||||
protected resetFilterSubject = new Subject<void>();
|
protected resetFilterSubject = new Subject<void>();
|
||||||
|
|
||||||
|
readonly skeletonSlots = Array.from({length: 24}, (_, index) => index);
|
||||||
|
readonly tableSkeletonRows = Array.from({length: 8}, (_, index) => index);
|
||||||
|
readonly tableSkeletonColumns = Array.from({length: 5}, (_, index) => index);
|
||||||
parsedFilters: Record<string, string[]> = {};
|
parsedFilters: Record<string, string[]> = {};
|
||||||
bookTitle = '';
|
bookTitle = '';
|
||||||
dynamicDialogRef: DynamicDialogRef | undefined | null;
|
dynamicDialogRef: DynamicDialogRef | undefined | null;
|
||||||
@@ -224,6 +300,9 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
|
|||||||
private destroy$ = new Subject<void>();
|
private destroy$ = new Subject<void>();
|
||||||
protected metadataMenuItems: MenuItem[] | undefined;
|
protected metadataMenuItems: MenuItem[] | undefined;
|
||||||
protected moreActionsMenuItems: MenuItem[] | undefined;
|
protected moreActionsMenuItems: MenuItem[] | undefined;
|
||||||
|
protected readonly onBookCardSelect = (book: Book, selected: boolean): void => {
|
||||||
|
this.handleBookSelect(book, selected);
|
||||||
|
};
|
||||||
|
|
||||||
protected bookSorter = new BookSorter(
|
protected bookSorter = new BookSorter(
|
||||||
sortCriteria => this.onMultiSortChange(sortCriteria),
|
sortCriteria => this.onMultiSortChange(sortCriteria),
|
||||||
@@ -270,6 +349,12 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
|
|||||||
});
|
});
|
||||||
private readonly syncBooksEffect = effect(() => {
|
private readonly syncBooksEffect = effect(() => {
|
||||||
const books = this.books();
|
const books = this.books();
|
||||||
|
if (!books) {
|
||||||
|
this.bookSelectionService.setCurrentBooks([]);
|
||||||
|
this.bookNavigationService.setAvailableBookIds([]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this.bookSelectionService.setCurrentBooks(books);
|
this.bookSelectionService.setCurrentBooks(books);
|
||||||
this.bookNavigationService.setAvailableBookIds(books.map(book => book.id));
|
this.bookNavigationService.setAvailableBookIds(books.map(book => book.id));
|
||||||
});
|
});
|
||||||
@@ -338,8 +423,20 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
|
|||||||
return this.coverScalePreferenceService.getCardHeight(_book);
|
return this.coverScalePreferenceService.getCardHeight(_book);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get showBooksLoadingPlaceholder(): boolean {
|
||||||
|
return !this.booksError() && (this.isBooksLoading() || !this.hasRenderedBooks());
|
||||||
|
}
|
||||||
|
|
||||||
|
get showTableLoadingPlaceholder(): boolean {
|
||||||
|
return this.showBooksLoadingPlaceholder && this.currentViewMode === VIEW_MODES.TABLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
get showGridLoadingPlaceholder(): boolean {
|
||||||
|
return this.showBooksLoadingPlaceholder && this.currentViewMode !== VIEW_MODES.TABLE;
|
||||||
|
}
|
||||||
|
|
||||||
get viewIcon(): string {
|
get viewIcon(): string {
|
||||||
return this.currentViewMode === VIEW_MODES.GRID ? 'pi pi-objects-column' : 'pi pi-table';
|
return this.currentViewMode === VIEW_MODES.TABLE ? 'pi pi-table' : 'pi pi-objects-column';
|
||||||
}
|
}
|
||||||
|
|
||||||
get isFilterActive(): boolean {
|
get isFilterActive(): boolean {
|
||||||
@@ -783,6 +880,13 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
|
|||||||
this.queryParamsService.updateViewMode(this.currentViewMode as 'grid' | 'table');
|
this.queryParamsService.updateViewMode(this.currentViewMode as 'grid' | 'table');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onViewModeChange(mode: string): void {
|
||||||
|
if (mode && mode !== this.currentViewMode) {
|
||||||
|
this.currentViewMode = mode;
|
||||||
|
this.queryParamsService.updateViewMode(mode as 'grid' | 'table');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
unshelfBooks(): void {
|
unshelfBooks(): void {
|
||||||
const entity = this.entity();
|
const entity = this.entity();
|
||||||
if (!entity) return;
|
if (!entity) return;
|
||||||
|
|||||||
-66
@@ -1,66 +0,0 @@
|
|||||||
import {TestBed} from '@angular/core/testing';
|
|
||||||
import {convertToParamMap} from '@angular/router';
|
|
||||||
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
|
|
||||||
|
|
||||||
import {BookFilterOrchestrationService} from './book-filter-orchestration.service';
|
|
||||||
import {SortService} from '../../service/sort.service';
|
|
||||||
|
|
||||||
describe('BookFilterOrchestrationService', () => {
|
|
||||||
let service: BookFilterOrchestrationService;
|
|
||||||
let sortService: {applyMultiSort: ReturnType<typeof vi.fn>};
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
sortService = {
|
|
||||||
applyMultiSort: vi.fn((books: unknown[]) => books),
|
|
||||||
};
|
|
||||||
|
|
||||||
TestBed.configureTestingModule({
|
|
||||||
providers: [
|
|
||||||
BookFilterOrchestrationService,
|
|
||||||
{provide: SortService, useValue: sortService},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
service = TestBed.inject(BookFilterOrchestrationService);
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(() => {
|
|
||||||
TestBed.resetTestingModule();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('passes searched and collapsed books through the sorter', () => {
|
|
||||||
const books = [
|
|
||||||
{id: 1, metadata: {title: 'Alpha Flight'}},
|
|
||||||
{id: 2, metadata: {title: 'Beta Test'}},
|
|
||||||
] as never[];
|
|
||||||
const seriesCollapseFilter = {
|
|
||||||
collapseBooks: vi.fn((items: unknown[]) => items),
|
|
||||||
};
|
|
||||||
const sortCriteria = [{property: 'title', direction: 'ASC'}] as never[];
|
|
||||||
|
|
||||||
const result = service.applyFilters(
|
|
||||||
books,
|
|
||||||
'alpha',
|
|
||||||
null,
|
|
||||||
'and',
|
|
||||||
seriesCollapseFilter as never,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
sortCriteria
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(seriesCollapseFilter.collapseBooks).toHaveBeenCalledWith(
|
|
||||||
[books[0]],
|
|
||||||
false,
|
|
||||||
false
|
|
||||||
);
|
|
||||||
expect(sortService.applyMultiSort).toHaveBeenCalledWith([books[0]], sortCriteria);
|
|
||||||
expect(result).toEqual([books[0]]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('forces series expansion only when a series filter is present in the query string', () => {
|
|
||||||
expect(service.shouldForceExpandSeries(convertToParamMap({filter: 'author:42,series:Saga'}))).toBe(true);
|
|
||||||
expect(service.shouldForceExpandSeries(convertToParamMap({filter: 'author:42'}))).toBe(false);
|
|
||||||
expect(service.shouldForceExpandSeries(convertToParamMap({}))).toBe(false);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
-36
@@ -1,36 +0,0 @@
|
|||||||
import {inject, Injectable} from '@angular/core';
|
|
||||||
import {ParamMap} from '@angular/router';
|
|
||||||
import {Book} from '../../model/book.model';
|
|
||||||
import {SortOption} from '../../model/sort.model';
|
|
||||||
import {SortService} from '../../service/sort.service';
|
|
||||||
import {filterBooksBySearchTerm} from './filters/HeaderFilter';
|
|
||||||
import {filterBooksByFilters} from './filters/sidebar-filter';
|
|
||||||
import {SeriesCollapseFilter} from './filters/SeriesCollapseFilter';
|
|
||||||
import {QUERY_PARAMS} from './book-browser-query-params.service';
|
|
||||||
import {BookFilterMode} from '../../../settings/user-management/user.service';
|
|
||||||
|
|
||||||
@Injectable({providedIn: 'root'})
|
|
||||||
export class BookFilterOrchestrationService {
|
|
||||||
private sortService = inject(SortService);
|
|
||||||
|
|
||||||
applyFilters(
|
|
||||||
books: Book[],
|
|
||||||
searchTerm: string,
|
|
||||||
activeFilters: Record<string, unknown[]> | null,
|
|
||||||
filterMode: BookFilterMode,
|
|
||||||
seriesCollapseFilter: SeriesCollapseFilter,
|
|
||||||
isSeriesCollapsed: boolean,
|
|
||||||
forceExpandSeries: boolean,
|
|
||||||
sortCriteria: SortOption[]
|
|
||||||
): Book[] {
|
|
||||||
const searchedBooks = filterBooksBySearchTerm(books, searchTerm);
|
|
||||||
const filteredBooks = filterBooksByFilters(searchedBooks, activeFilters, filterMode);
|
|
||||||
const collapsedBooks = seriesCollapseFilter.collapseBooks(filteredBooks, forceExpandSeries, isSeriesCollapsed);
|
|
||||||
return this.sortService.applyMultiSort(collapsedBooks, sortCriteria);
|
|
||||||
}
|
|
||||||
|
|
||||||
shouldForceExpandSeries(queryParamMap: ParamMap): boolean {
|
|
||||||
const filterParam = queryParamMap.get(QUERY_PARAMS.FILTER);
|
|
||||||
return !!filterParam && filterParam.split(',').some(pair => pair.trim().startsWith('series:'));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
import {describe, expect, it} from 'vitest';
|
||||||
|
|
||||||
|
import {DeferredRenderState} from './deferred-render-state';
|
||||||
|
|
||||||
|
describe('DeferredRenderState', () => {
|
||||||
|
it('clears the current value for reset runs', () => {
|
||||||
|
const state = new DeferredRenderState<number[]>();
|
||||||
|
|
||||||
|
const initialRequest = state.begin('reset');
|
||||||
|
state.commit(initialRequest, [1, 2, 3]);
|
||||||
|
|
||||||
|
expect(state.value()).toEqual([1, 2, 3]);
|
||||||
|
expect(state.hasValue()).toBe(true);
|
||||||
|
|
||||||
|
state.begin('reset');
|
||||||
|
|
||||||
|
expect(state.value()).toBeUndefined();
|
||||||
|
expect(state.hasValue()).toBe(false);
|
||||||
|
expect(state.isRefreshing()).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps the current value visible during refresh runs', () => {
|
||||||
|
const state = new DeferredRenderState<number[]>();
|
||||||
|
|
||||||
|
const initialRequest = state.begin('reset');
|
||||||
|
state.commit(initialRequest, [1, 2, 3]);
|
||||||
|
|
||||||
|
const refreshRequest = state.begin('refresh');
|
||||||
|
|
||||||
|
expect(state.value()).toEqual([1, 2, 3]);
|
||||||
|
expect(state.isRefreshing()).toBe(true);
|
||||||
|
|
||||||
|
state.commit(refreshRequest, [4, 5]);
|
||||||
|
|
||||||
|
expect(state.value()).toEqual([4, 5]);
|
||||||
|
expect(state.isRefreshing()).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('ignores stale commits from older requests', () => {
|
||||||
|
const state = new DeferredRenderState<number[]>();
|
||||||
|
|
||||||
|
const initialRequest = state.begin('reset');
|
||||||
|
state.commit(initialRequest, [1]);
|
||||||
|
|
||||||
|
const staleRefresh = state.begin('refresh');
|
||||||
|
const latestRefresh = state.begin('refresh');
|
||||||
|
|
||||||
|
expect(state.commit(staleRefresh, [2])).toBe(false);
|
||||||
|
expect(state.value()).toEqual([1]);
|
||||||
|
expect(state.isRefreshing()).toBe(true);
|
||||||
|
|
||||||
|
expect(state.commit(latestRefresh, [3])).toBe(true);
|
||||||
|
expect(state.value()).toEqual([3]);
|
||||||
|
expect(state.isRefreshing()).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('ignores cancel with a stale request id', () => {
|
||||||
|
const state = new DeferredRenderState<number[]>();
|
||||||
|
|
||||||
|
const first = state.begin('reset');
|
||||||
|
state.commit(first, [1]);
|
||||||
|
|
||||||
|
const stale = state.begin('refresh');
|
||||||
|
const current = state.begin('refresh');
|
||||||
|
|
||||||
|
state.cancel(stale);
|
||||||
|
|
||||||
|
// Cancel was a no-op — isRefreshing still true from the current request
|
||||||
|
expect(state.isRefreshing()).toBe(true);
|
||||||
|
expect(state.value()).toEqual([1]);
|
||||||
|
|
||||||
|
state.commit(current, [2]);
|
||||||
|
expect(state.isRefreshing()).toBe(false);
|
||||||
|
expect(state.value()).toEqual([2]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('cancel during a reset leaves value undefined', () => {
|
||||||
|
const state = new DeferredRenderState<number[]>();
|
||||||
|
|
||||||
|
const request = state.begin('reset');
|
||||||
|
|
||||||
|
expect(state.value()).toBeUndefined();
|
||||||
|
expect(state.isRefreshing()).toBe(false);
|
||||||
|
|
||||||
|
state.cancel(request);
|
||||||
|
|
||||||
|
expect(state.value()).toBeUndefined();
|
||||||
|
expect(state.isRefreshing()).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not set isRefreshing when no value exists yet', () => {
|
||||||
|
const state = new DeferredRenderState<number[]>();
|
||||||
|
|
||||||
|
const request = state.begin('refresh');
|
||||||
|
|
||||||
|
// No prior value, so nothing to "refresh" — isRefreshing stays false
|
||||||
|
expect(state.isRefreshing()).toBe(false);
|
||||||
|
expect(state.value()).toBeUndefined();
|
||||||
|
expect(state.hasValue()).toBe(false);
|
||||||
|
|
||||||
|
state.commit(request, [1]);
|
||||||
|
expect(state.value()).toEqual([1]);
|
||||||
|
expect(state.isRefreshing()).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
import {computed, signal} from '@angular/core';
|
||||||
|
|
||||||
|
export type DeferredRenderMode = 'reset' | 'refresh';
|
||||||
|
|
||||||
|
export class DeferredRenderState<T> {
|
||||||
|
private readonly _value = signal<T | undefined>(undefined);
|
||||||
|
readonly value = this._value.asReadonly();
|
||||||
|
|
||||||
|
private readonly _isRefreshing = signal(false);
|
||||||
|
readonly isRefreshing = this._isRefreshing.asReadonly();
|
||||||
|
|
||||||
|
readonly hasValue = computed(() => this._value() !== undefined);
|
||||||
|
|
||||||
|
private activeRequestId = 0;
|
||||||
|
|
||||||
|
begin(mode: DeferredRenderMode): number {
|
||||||
|
this.activeRequestId += 1;
|
||||||
|
|
||||||
|
if (mode === 'reset') {
|
||||||
|
this._value.set(undefined);
|
||||||
|
this._isRefreshing.set(false);
|
||||||
|
} else {
|
||||||
|
this._isRefreshing.set(this._value() !== undefined);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.activeRequestId;
|
||||||
|
}
|
||||||
|
|
||||||
|
commit(requestId: number, value: T): boolean {
|
||||||
|
if (requestId !== this.activeRequestId) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._value.set(value);
|
||||||
|
this._isRefreshing.set(false);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
cancel(requestId: number): void {
|
||||||
|
if (requestId !== this.activeRequestId) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._isRefreshing.set(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user