fix(filter): update filter options to use CountedOption for read statuses and file types (#448)

* fix(filter): update filter options to use CountedOption for read statuses and file types

* fix(specification): validate file types and read statuses, throwing APIException for invalid values
This commit is contained in:
brios
2026-04-22 17:27:10 -04:00
committed by Zach Yale
parent 2c26bd26fd
commit 4a68fa28f6
12 changed files with 194 additions and 66 deletions
@@ -318,7 +318,11 @@
></app-book-table>
}
@if (currentViewMode === 'grid') {
<virtual-scroller class="virtual-scroller" #scroll [items]="books" [bufferAmount]="16" (vsEnd)="onScrollEnd()">
<virtual-scroller class="virtual-scroller" #scroll
[items]="books"
[bufferAmount]="16"
[compareItems]="compareBookItems"
(vsEnd)="onScrollEnd()">
<div
class="grid"
#container
@@ -326,7 +330,7 @@
@for (book of scroll.viewPortItems; let i = $index; track book.id) {
<div
class="virtual-scroller-item"
[ngStyle]="{width: currentCardSize.width + 'px', height: getCardHeight(book) + 'px'}">
[ngStyle]="{width: currentCardSize.width + 'px', height: currentCardSize.height + 'px'}">
<app-book-card
[index]="bookIndexById().get(book.id) ?? i"
[book]="book"
@@ -1,4 +1,4 @@
import {AfterViewInit, Component, HostListener, computed, effect, inject, OnDestroy, OnInit, signal, untracked, ViewChild} from '@angular/core';
import {AfterViewInit, ChangeDetectionStrategy, Component, HostListener, computed, effect, inject, OnDestroy, OnInit, signal, untracked, ViewChild} from '@angular/core';
import {toObservable, toSignal} from '@angular/core/rxjs-interop';
import {ActivatedRoute, NavigationStart, Router} from '@angular/router';
import {ConfirmationService, MenuItem, MessageService} from 'primeng/api';
@@ -72,6 +72,7 @@ export enum EntityType {
standalone: true,
templateUrl: './book-browser.component.html',
styleUrls: ['./book-browser.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [
Button, VirtualScrollerModule, BookCardComponent, Menu, InputText, FormsModule,
BookTableComponent, BookFilterComponent, Tooltip, NgClass, NgStyle, Popover,
@@ -226,8 +227,8 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
case 'mood': scopeFilters.mood = strValues; break;
case 'narrator': scopeFilters.narrator = strValues; break;
case 'language': scopeFilters.language = strValues; break;
case 'readStatus': scopeFilters.status = strValues[0]; break;
case 'bookType': scopeFilters.fileType = strValues[0]; break;
case 'readStatus': scopeFilters.status = strValues; break;
case 'bookType': scopeFilters.fileType = strValues; break;
}
}
const mode = this.selectedFilterMode();
@@ -256,34 +257,24 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
return Number.isNaN(entityId) ? entityType : `${entityType}:${entityId}`;
});
private readonly pipelineInputs = computed(() => ({
books: this.appBooksApi.books(),
entity: this.entityInfo(),
search: this.debouncedSearchTerm(),
filter: this.selectedFilter(),
filterMode: this.selectedFilterMode(),
collapsed: this.seriesCollapsed(),
forceExpand: this.forceExpandSeries(),
sort: this.sortCriteria(),
}));
private readonly pipelineInputs = computed(() => this.appBooksApi.books());
private readonly renderBooksEffect = effect((onCleanup) => {
const contextKey = this.booksContextKey();
this.pipelineInputs();
const books = this.pipelineInputs();
const shouldRefreshInPlace = untracked(() => this.hasRenderedBooks()) && contextKey === this.lastBooksContextKey;
this.lastBooksContextKey = contextKey;
if (shouldRefreshInPlace) {
const requestId = this.booksRenderState.begin('refresh');
this.booksRenderState.commit(requestId, this.appBooksApi.books());
this.booksRenderState.update(books);
return;
}
const requestId = this.booksRenderState.begin('reset');
const timeout = globalThis.setTimeout(() => {
this.booksRenderState.commit(requestId, this.appBooksApi.books());
this.booksRenderState.commit(requestId, books);
});
onCleanup(() => {
@@ -302,6 +293,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit, OnDestroy {
}
return map;
});
readonly compareBookItems = (a: Book, b: Book): boolean => a?.id === b?.id;
protected resetFilterSubject = new Subject<void>();
readonly skeletonSlots = Array.from({length: 24}, (_, index) => index);
@@ -152,15 +152,9 @@ export class BookFilterService {
bookCount: lang.count,
}));
case 'readStatus':
return (options.readStatuses ?? []).map((status: string) => ({
value: {id: status, name: status},
bookCount: 0,
}));
return this.countedToFilters(options.readStatuses);
case 'bookType':
return (options.fileTypes ?? []).map((ft: string) => ({
value: {id: ft, name: ft},
bookCount: 0,
}));
return this.countedToFilters(options.fileTypes);
default:
return [];
}
@@ -26,6 +26,12 @@ export class DeferredRenderState<T> {
return this.activeRequestId;
}
/** Direct update without refresh animation used for in-place data swaps. */
update(value: T): void {
this._value.set(value);
this._isRefreshing.set(false);
}
commit(requestId: number, value: T): boolean {
if (requestId !== this.activeRequestId) {
return false;
@@ -30,8 +30,8 @@ export interface AppBookSummary {
export interface AppFilterOptions {
authors: CountedOption[];
languages: LanguageOption[];
readStatuses: string[];
fileTypes: string[];
readStatuses: CountedOption[];
fileTypes: CountedOption[];
categories: CountedOption[];
publishers: CountedOption[];
series: CountedOption[];
@@ -56,9 +56,9 @@ export interface AppBookFilters {
shelfId?: number;
magicShelfId?: number;
unshelved?: boolean;
status?: string;
status?: string[];
search?: string;
fileType?: string;
fileType?: string[];
minRating?: number;
maxRating?: number;
authors?: string[];
@@ -62,6 +62,14 @@ export class AppBooksApiService {
const data = this.booksQuery.data();
if (!data) return [];
return data.pages.flatMap(page => page.content.map(summaryToBook));
}, {
equal: (a, b) => {
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) {
if (a[i].id !== b[i].id) return false;
}
return true;
}
});
readonly totalElements = computed(() => {
@@ -126,8 +134,6 @@ export class AppBooksApiService {
if (filters.shelfId) params = params.set('shelfId', filters.shelfId.toString());
if (filters.magicShelfId) params = params.set('magicShelfId', filters.magicShelfId.toString());
if (filters.unshelved) params = params.set('unshelved', 'true');
if (filters.status) params = params.set('status', filters.status);
if (filters.fileType) params = params.set('fileType', filters.fileType);
if (filters.minRating != null) params = params.set('minRating', filters.minRating.toString());
if (filters.maxRating != null) params = params.set('maxRating', filters.maxRating.toString());
if (filters.filterMode) params = params.set('filterMode', filters.filterMode);
@@ -142,6 +148,8 @@ export class AppBooksApiService {
['tag', filters.tag],
['mood', filters.mood],
['narrator', filters.narrator],
['status', filters.status],
['fileType', filters.fileType],
];
for (const [key, values] of arrayFilters) {
if (values?.length) {
@@ -178,7 +186,7 @@ function summaryToBook(summary: AppBookSummary): Book {
audiobookCoverUpdatedOn: summary.audiobookCoverUpdatedOn,
},
primaryFile: summary.primaryFileType
? {bookType: summary.primaryFileType as BookType}
? {bookType: summary.primaryFileType as BookType, extension: summary.primaryFileType.toLowerCase()}
: null,
pdfProgress: summary.readProgress != null
? {page: 0, percentage: summary.readProgress}