Sidebar filter: Restore Authors & Genres, add Moods & Tags (#1264)

This commit is contained in:
Aditya Chandel
2025-10-05 00:16:22 -06:00
committed by GitHub
parent ff230f34fc
commit f778f1932d
5 changed files with 39 additions and 95 deletions
@@ -117,26 +117,6 @@
{{ coverScalePreferenceService.scaleFactor.toFixed(2) }}x
</div>
</div>
<!--<div class="flex flex-col gap-2">
<label class="font-medium text-sm">Show Sidebar Filters:</label>
<div class="flex gap-4 justify-start">
<p-toggle-switch
[(ngModel)]="sidebarFilterTogglePrefService.selectedShowFilter"
(onChange)="sidebarFilterTogglePrefService.selectedShowFilter = $event.checked">
</p-toggle-switch>
</div>
</div>-->
<div class="flex flex-col gap-2">
<label class="font-medium text-sm">Sidebar Filter Sort:</label>
<div class="flex gap-4 justify-start">
<p-select
size="small"
[options]="filterSortPreferenceService.filterSortingOptions"
[(ngModel)]="filterSortPreferenceService.selectedFilterSorting"
placeholder="Select Sorting">
</p-select>
</div>
</div>
</div>
</p-popover>
</div>
@@ -36,8 +36,6 @@ import {BookDialogHelperService} from './BookDialogHelperService';
import {Checkbox} from 'primeng/checkbox';
import {Popover} from 'primeng/popover';
import {Slider} from 'primeng/slider';
import {Select} from 'primeng/select';
import {FilterSortPreferenceService} from './filters/filter-sorting-preferences.service';
import {Divider} from 'primeng/divider';
import {MultiSelect} from 'primeng/multiselect';
import {TableColumnPreferenceService} from './table-column-preference-service';
@@ -85,7 +83,7 @@ const SORT_DIRECTION = {
imports: [
Button, VirtualScrollerModule, BookCardComponent, AsyncPipe, ProgressSpinner, Menu, InputText, FormsModule,
BookTableComponent, BookFilterComponent, Tooltip, NgClass, PrimeTemplate, NgStyle, Popover,
Checkbox, Slider, Select, Divider, MultiSelect, TieredMenu
Checkbox, Slider, Divider, MultiSelect, TieredMenu
],
providers: [SeriesCollapseFilter],
animations: [
@@ -100,7 +98,6 @@ const SORT_DIRECTION = {
export class BookBrowserComponent implements OnInit {
protected userService = inject(UserService);
protected coverScalePreferenceService = inject(CoverScalePreferenceService);
protected filterSortPreferenceService = inject(FilterSortPreferenceService);
protected columnPreferenceService = inject(TableColumnPreferenceService);
protected sidebarFilterTogglePrefService = inject(SidebarFilterTogglePrefService);
private activatedRoute = inject(ActivatedRoute);
@@ -274,7 +271,6 @@ export class BookBrowserComponent implements OnInit {
const globalPrefs = this.entityViewPreferences?.global;
const currentEntityTypeStr = this.entityType ? this.entityType.toString().toUpperCase() : undefined;
this.coverScalePreferenceService.initScaleValue(this.coverScalePreferenceService.scaleFactor);
this.filterSortPreferenceService.initValue(user.user?.userSettings?.filterSortingMode);
this.columnPreferenceService.initPreferences(user.user?.userSettings?.tableColumnPreference);
this.visibleColumns = this.columnPreferenceService.visibleColumns;
@@ -41,7 +41,11 @@
(click)="handleFilterClick(filterType, filter.value?.id || filter.value)">
{{ filter.value.name || filter.value }}
<p-badge class="filter-value-badge" [value]="filter.bookCount"></p-badge>
<!-- <p class="text-[var(--primary-color)]"> {{ filter.bookCount }}</p> -->
</div>
}
@if (truncatedFilters[filterType]) {
<div class="text-xs text-gray-500 text-center pt-2 border-t border-gray-200">
Showing first 250 items
</div>
}
</div>
@@ -50,5 +54,9 @@
</p-accordion-panel>
}
</p-accordion>
<div class="text-xs text-gray-500 text-center pt-4 px-4 mt-2">
Note: Top 500 items are displayed per filter category
</div>
</div>
</div>
@@ -1,5 +1,5 @@
import {ChangeDetectionStrategy, Component, EventEmitter, inject, Input, OnDestroy, OnInit, Output} from '@angular/core';
import {combineLatest, distinctUntilChanged, Observable, of, shareReplay, Subject, takeUntil} from 'rxjs';
import {combineLatest, Observable, of, shareReplay, Subject, takeUntil} from 'rxjs';
import {map} from 'rxjs/operators';
import {BookService} from '../../../service/book.service';
import {Library} from '../../../model/library.model';
@@ -12,7 +12,6 @@ import {Badge} from 'primeng/badge';
import {FormsModule} from '@angular/forms';
import {SelectButton} from 'primeng/selectbutton';
import {UserService} from '../../../../settings/user-management/user.service';
import {FilterSortPreferenceService} from '../filters/filter-sorting-preferences.service';
import {MagicShelf} from '../../../../magic-shelf.service';
import {GroupRule} from '../../../../magic-shelf-component/magic-shelf-component';
import {BookRuleEvaluatorService} from '../../../../book-rule-evaluator.service';
@@ -161,6 +160,7 @@ export class BookFilterComponent implements OnInit, OnDestroy {
activeFilters: Record<string, any> = {};
filterStreams: Record<string, Observable<Filter<any>[]>> = {};
truncatedFilters: Record<string, boolean> = {};
filterTypes: string[] = [];
filterModeOptions = [
{label: 'AND', value: 'and'},
@@ -190,38 +190,37 @@ export class BookFilterComponent implements OnInit, OnDestroy {
bookService = inject(BookService);
userService = inject(UserService);
filterSortPreferenceService = inject(FilterSortPreferenceService);
bookRuleEvaluatorService = inject(BookRuleEvaluatorService);
ngOnInit(): void {
combineLatest([
this.filterSortPreferenceService.sortMode$.pipe(distinctUntilChanged()),
this.entity$ ?? of(null),
this.entityType$ ?? of(EntityType.ALL_BOOKS)
])
.pipe(takeUntil(this.destroy$))
.subscribe(([sortMode]) => {
this.filterStreams = {
// Temporarily disabled until we can optimize for large libraries
/*author: this.getFilterStream((book: Book) => book.metadata?.authors!.map(name => ({id: name, name})) || [], 'id', 'name', sortMode),
category: this.getFilterStream((book: Book) => book.metadata?.categories!.map(name => ({id: name, name})) || [], 'id', 'name', sortMode),*/
series: this.getFilterStream((book) => (book.metadata?.seriesName ? [{id: book.metadata.seriesName, name: book.metadata.seriesName}] : []), 'id', 'name', sortMode),
publisher: this.getFilterStream((book) => (book.metadata?.publisher ? [{id: book.metadata.publisher, name: book.metadata.publisher}] : []), 'id', 'name', sortMode),
author: this.getFilterStream((book: Book) => book.metadata?.authors!.map(name => ({id: name, name})) || [], 'id', 'name'),
category: this.getFilterStream((book: Book) => book.metadata?.categories!.map(name => ({id: name, name})) || [], 'id', 'name'),
series: this.getFilterStream((book) => (book.metadata?.seriesName ? [{id: book.metadata.seriesName, name: book.metadata.seriesName}] : []), 'id', 'name'),
publisher: this.getFilterStream((book) => (book.metadata?.publisher ? [{id: book.metadata.publisher, name: book.metadata.publisher}] : []), 'id', 'name'),
readStatus: this.getFilterStream((book: Book) => {
let status = book.readStatus;
if (status == null || !(status in readStatusLabels)) {
status = ReadStatus.UNSET;
}
return [{id: status, name: getReadStatusName(status)}];
}, 'id', 'name', sortMode),
}, 'id', 'name'),
mood: this.getFilterStream((book: Book) => book.metadata?.moods!.map(name => ({id: name, name})) || [], 'id', 'name'),
tag: this.getFilterStream((book: Book) => book.metadata?.tags!.map(name => ({id: name, name})) || [], 'id', 'name'),
matchScore: this.getFilterStream((book: Book) => getMatchScoreRangeFilters(book.metadataMatchScore), 'id', 'name', 'sortIndex'),
personalRating: this.getFilterStream((book: Book) => getRatingRangeFilters10(book.metadata?.personalRating!), 'id', 'name', 'sortIndex'),
amazonRating: this.getFilterStream((book: Book) => getRatingRangeFilters(book.metadata?.amazonRating!), 'id', 'name', 'sortIndex'),
goodreadsRating: this.getFilterStream((book: Book) => getRatingRangeFilters(book.metadata?.goodreadsRating!), 'id', 'name', 'sortIndex'),
hardcoverRating: this.getFilterStream((book: Book) => getRatingRangeFilters(book.metadata?.hardcoverRating!), 'id', 'name', 'sortIndex'),
shelfStatus: this.getFilterStream(getShelfStatusFilter, 'id', 'name', sortMode),
publishedDate: this.getFilterStream(extractPublishedYearFilter, 'id', 'name', sortMode),
language: this.getFilterStream(getLanguageFilter, 'id', 'name', sortMode),
shelfStatus: this.getFilterStream(getShelfStatusFilter, 'id', 'name'),
publishedDate: this.getFilterStream(extractPublishedYearFilter, 'id', 'name'),
language: this.getFilterStream(getLanguageFilter, 'id', 'name'),
fileSize: this.getFilterStream((book: Book) => getFileSizeRangeFilters(book.fileSizeKb), 'id', 'name', 'sortIndex'),
pageCount: this.getFilterStream((book: Book) => getPageCountRangeFilters(book.metadata?.pageCount!), 'id', 'name', 'sortIndex'),
};
@@ -266,18 +265,31 @@ export class BookFilterComponent implements OnInit, OnDestroy {
const result = Array.from(filterMap.values());
return result.sort((a, b) => {
const sorted = result.sort((a, b) => {
if (sortMode === 'sortIndex') {
return (a.value.sortIndex ?? 999) - (b.value.sortIndex ?? 999);
}
if (sortMode === 'alphabetical') {
return a.value[nameKey].toString().localeCompare(b.value[nameKey].toString());
}
return (
b.bookCount - a.bookCount ||
a.value[nameKey].toString().localeCompare(b.value[nameKey].toString())
);
});
const isTruncated = sorted.length > 500;
const truncated = sorted.slice(0, 500);
return {items: truncated, isTruncated};
}),
map(({items, isTruncated}) => {
setTimeout(() => {
const filterType = Object.keys(this.filterStreams).find(key =>
this.filterStreams[key] === this.getFilterStream(extractor, idKey, nameKey)
);
if (filterType) {
this.truncatedFilters[filterType] = isTruncated;
}
});
return items;
}),
shareReplay({bufferSize: 1, refCount: true})
);
@@ -1,52 +0,0 @@
import {inject, Injectable} from '@angular/core';
import {BehaviorSubject} from 'rxjs';
import {UserService} from '../../../../settings/user-management/user.service';
import {MessageService} from 'primeng/api';
@Injectable({
providedIn: 'root'
})
export class FilterSortPreferenceService {
readonly filterSortingOptions = [
{ label: 'Alphabetical (A–Z)', value: 'alphabetical' },
{ label: 'Book Count (High to Low)', value: 'count' }
];
private readonly userService = inject(UserService);
private readonly messageService = inject(MessageService);
private readonly sortModeSubject = new BehaviorSubject<'alphabetical' | 'count'>('count');
readonly sortMode$ = this.sortModeSubject.asObservable();
initValue(value: 'alphabetical' | 'count' | null | undefined): void {
const resolved = value ?? 'count';
this.sortModeSubject.next(resolved);
}
get selectedFilterSorting(): 'alphabetical' | 'count' {
return this.sortModeSubject.value;
}
set selectedFilterSorting(value: 'alphabetical' | 'count') {
if (this.sortModeSubject.value !== value) {
this.sortModeSubject.next(value);
this.savePreference(value);
}
}
private savePreference(value: 'alphabetical' | 'count'): void {
const user = this.userService.getCurrentUser();
if (!user) return;
user.userSettings.filterSortingMode = value;
this.userService.updateUserSetting(user.id, 'filterSortingMode', value);
this.messageService.add({
severity: 'success',
summary: 'Preferences Updated',
detail: 'Your filter sorting preference has been saved.',
life: 1500
});
}
}