feat(filters): add customizable visible filters setting (#2625)

This commit is contained in:
ACX
2026-02-05 20:59:07 -07:00
committed by GitHub
parent 555cd13f8a
commit f71966a9b6
6 changed files with 164 additions and 12 deletions
@@ -14,7 +14,7 @@
<div class="filter-content">
<p-accordion [value]="expandedPanels" [multiple]="true" (valueChange)="onExpandedPanelsChange($event)">
@for (filterType of filterTypes; track trackByFilterType(i, filterType); let i = $index) {
@for (filterType of visibleFilterTypes; track trackByFilterType(i, filterType); let i = $index) {
@if (filterStreams[filterType] | async; as filters) {
@if (filters.length > 0) {
<p-accordion-panel [value]="i">
@@ -9,10 +9,11 @@ import {AsyncPipe, NgClass, TitleCasePipe} from '@angular/common';
import {Badge} from 'primeng/badge';
import {FormsModule} from '@angular/forms';
import {SelectButton} from 'primeng/selectbutton';
import {BookFilterMode} from '../../../../settings/user-management/user.service';
import {BookFilterMode, DEFAULT_VISIBLE_FILTERS, UserService, VisibleFilterType} from '../../../../settings/user-management/user.service';
import {MagicShelf} from '../../../../magic-shelf/service/magic-shelf.service';
import {Filter, FILTER_LABELS, FilterType} from './book-filter.config';
import {BookFilterService} from './book-filter.service';
import {filter} from 'rxjs/operators';
type FilterModeOption = { label: string; value: BookFilterMode };
@@ -38,6 +39,7 @@ export class BookFilterComponent implements OnInit, OnDestroy {
@Output() filterModeChanged = new EventEmitter<BookFilterMode>();
private readonly filterService = inject(BookFilterService);
private readonly userService = inject(UserService);
private readonly destroy$ = new Subject<void>();
private readonly activeFilters$ = new BehaviorSubject<Record<string, unknown[]> | null>(null);
@@ -46,10 +48,12 @@ export class BookFilterComponent implements OnInit, OnDestroy {
activeFilters: Record<string, unknown[]> = {};
filterStreams: Record<FilterType, Observable<Filter[]>> = {} as Record<FilterType, Observable<Filter[]>>;
filterTypes: FilterType[] = [];
visibleFilterTypes: FilterType[] = [];
expandedPanels: number[] = [0];
truncatedFilters: Record<string, boolean> = {};
private _selectedFilterMode: BookFilterMode = 'and';
private _visibleFilters: VisibleFilterType[] = [...DEFAULT_VISIBLE_FILTERS];
readonly filterLabels = FILTER_LABELS;
readonly filterModeOptions: FilterModeOption[] = [
@@ -71,6 +75,7 @@ export class BookFilterComponent implements OnInit, OnDestroy {
}
ngOnInit(): void {
this.subscribeToUserSettings();
this.initializeFilterStreams();
this.subscribeToReset();
}
@@ -118,23 +123,34 @@ export class BookFilterComponent implements OnInit, OnDestroy {
trackByFilterType = (_: number, type: FilterType): string => type;
trackByFilter = (_: number, filter: Filter): unknown => this.getFilterValueId(filter);
trackByFilter = (_: number, f: Filter): unknown => this.getFilterValueId(f);
getFilterValueId(filter: Filter): unknown {
const value = filter.value;
getFilterValueId(f: Filter): unknown {
const value = f.value;
return typeof value === 'object' && value !== null && 'id' in value
? value.id
: filter.value;
: f.value;
}
getFilterValueDisplay(filter: Filter): string {
const value = filter.value;
getFilterValueDisplay(f: Filter): string {
const value = f.value;
if (typeof value === 'object' && value !== null && 'name' in value) {
return String(value.name ?? '');
}
return String(value ?? '');
}
private subscribeToUserSettings(): void {
this.userService.userState$.pipe(
filter(state => !!state?.user && state.loaded),
takeUntil(this.destroy$)
).subscribe(state => {
const settings = state.user!.userSettings;
this._visibleFilters = settings.visibleFilters ?? [...DEFAULT_VISIBLE_FILTERS];
this.updateVisibleFilterTypes();
});
}
private initializeFilterStreams(): void {
const entity$ = this.entity$ ?? of(null);
const entityType$ = this.entityType$ ?? of(EntityType.ALL_BOOKS);
@@ -146,9 +162,17 @@ export class BookFilterComponent implements OnInit, OnDestroy {
this.filterMode$
);
this.filterTypes = Object.keys(this.filterStreams) as FilterType[];
this.updateVisibleFilterTypes();
this.updateExpandedPanels();
}
private updateVisibleFilterTypes(): void {
// Filter and order filterTypes based on user's visible filter preferences
this.visibleFilterTypes = this._visibleFilters.filter(
vf => this.filterTypes.includes(vf as FilterType)
) as FilterType[];
}
private subscribeToReset(): void {
this.resetFilter$?.pipe(takeUntil(this.destroy$)).subscribe(() => this.clearActiveFilter());
}
@@ -198,7 +222,7 @@ export class BookFilterComponent implements OnInit, OnDestroy {
private updateExpandedPanels(): void {
const panels = new Set(this.expandedPanels);
this.filterTypes.forEach((type, i) => {
this.visibleFilterTypes.forEach((type, i) => {
if (this.activeFilters[type]?.length) panels.add(i);
});
this.expandedPanels = panels.size > 0 ? [...panels] : [0];
@@ -166,6 +166,44 @@ export interface TableColumnPreference {
order: number;
}
export type VisibleFilterType =
| 'author' | 'category' | 'series' | 'bookType' | 'readStatus'
| 'personalRating' | 'publisher' | 'matchScore' | 'library' | 'shelf'
| 'shelfStatus' | 'tag' | 'publishedDate' | 'fileSize' | 'amazonRating'
| 'goodreadsRating' | 'hardcoverRating' | 'language' | 'pageCount' | 'mood'
| 'ageRating' | 'contentRating';
export const DEFAULT_VISIBLE_FILTERS: VisibleFilterType[] = [
'author', 'category', 'series', 'bookType', 'readStatus',
'personalRating', 'library', 'tag', 'ageRating', 'contentRating',
'matchScore', 'publisher', 'publishedDate', 'fileSize'
];
export const ALL_FILTER_OPTIONS: { label: string; value: VisibleFilterType }[] = [
{label: 'Author', value: 'author'},
{label: 'Genre', value: 'category'},
{label: 'Series', value: 'series'},
{label: 'Book Type', value: 'bookType'},
{label: 'Read Status', value: 'readStatus'},
{label: 'Personal Rating', value: 'personalRating'},
{label: 'Library', value: 'library'},
{label: 'Tag', value: 'tag'},
{label: 'Age Rating', value: 'ageRating'},
{label: 'Content Rating', value: 'contentRating'},
{label: 'Metadata Match Score', value: 'matchScore'},
{label: 'Publisher', value: 'publisher'},
{label: 'Published Year', value: 'publishedDate'},
{label: 'File Size', value: 'fileSize'},
{label: 'Shelf', value: 'shelf'},
{label: 'Shelf Status', value: 'shelfStatus'},
{label: 'Language', value: 'language'},
{label: 'Page Count', value: 'pageCount'},
{label: 'Mood', value: 'mood'},
{label: 'Amazon Rating', value: 'amazonRating'},
{label: 'Goodreads Rating', value: 'goodreadsRating'},
{label: 'Hardcover Rating', value: 'hardcoverRating'}
];
export interface UserSettings {
perBookSetting: PerBookSetting;
pdfReaderSetting: PdfReaderSetting;
@@ -177,6 +215,7 @@ export interface UserSettings {
sidebarShelfSorting: SidebarShelfSorting;
sidebarMagicShelfSorting: SidebarMagicShelfSorting;
filterMode: BookFilterMode;
visibleFilters?: VisibleFilterType[];
metadataCenterViewMode: 'route' | 'dialog';
enableSeriesView: boolean;
entityViewPreferences: EntityViewPreferences;
@@ -26,5 +26,33 @@
</div>
</div>
<div class="setting-item">
<div class="setting-header-standalone">
<label class="setting-label">Visible Filters</label>
</div>
<p class="setting-description">
Select which filters appear in the filter sidebar. Choose between {{ minFilters }} and {{ maxFilters }} filters.
<span class="filter-count">({{ selectionCountText }})</span>
</p>
<div class="setting-options">
<p-multiselect
[options]="allFilterOptions"
[(ngModel)]="selectedVisibleFilters"
(ngModelChange)="onVisibleFiltersChange()"
optionLabel="label"
optionValue="value"
[showClear]="false"
[filter]="true"
filterPlaceHolder="Search filters..."
placeholder="Select filters"
display="chip"
appendTo="body"
[maxSelectedLabels]="5"
selectedItemsLabel="{0} filters selected"
styleClass="visible-filters-select">
</p-multiselect>
</div>
</div>
</div>
</div>
@@ -47,4 +47,19 @@
width: 100%;
}
}
p-multiselect {
min-width: 300px;
max-width: 100%;
@media (max-width: 768px) {
min-width: 100%;
width: 100%;
}
}
}
.filter-count {
font-weight: 500;
color: var(--p-primary-color);
}
@@ -1,16 +1,30 @@
import {Component, inject, OnDestroy, OnInit} from '@angular/core';
import {Select} from 'primeng/select';
import {BookFilterMode, User, UserService, UserSettings, UserState} from '../../user-management/user.service';
import {
ALL_FILTER_OPTIONS,
BookFilterMode,
DEFAULT_VISIBLE_FILTERS,
User,
UserService,
UserSettings,
UserState,
VisibleFilterType
} from '../../user-management/user.service';
import {MessageService} from 'primeng/api';
import {Observable, Subject} from 'rxjs';
import {FormsModule} from '@angular/forms';
import {filter, takeUntil} from 'rxjs/operators';
import {MultiSelect} from 'primeng/multiselect';
const MIN_VISIBLE_FILTERS = 5;
const MAX_VISIBLE_FILTERS = 15;
@Component({
selector: 'app-filter-preferences',
imports: [
Select,
FormsModule
FormsModule,
MultiSelect
],
templateUrl: './filter-preferences.component.html',
styleUrl: './filter-preferences.component.scss'
@@ -23,7 +37,12 @@ export class FilterPreferencesComponent implements OnInit, OnDestroy {
{label: 'Single', value: 'single'},
];
readonly allFilterOptions = ALL_FILTER_OPTIONS;
readonly minFilters = MIN_VISIBLE_FILTERS;
readonly maxFilters = MAX_VISIBLE_FILTERS;
selectedFilterMode: BookFilterMode = 'and';
selectedVisibleFilters: VisibleFilterType[] = [...DEFAULT_VISIBLE_FILTERS];
private readonly userService = inject(UserService);
private readonly messageService = inject(MessageService);
@@ -49,6 +68,7 @@ export class FilterPreferencesComponent implements OnInit, OnDestroy {
private loadPreferences(settings: UserSettings): void {
this.selectedFilterMode = settings.filterMode ?? 'and';
this.selectedVisibleFilters = settings.visibleFilters ?? [...DEFAULT_VISIBLE_FILTERS];
}
private updatePreference(path: string[], value: unknown): void {
@@ -71,7 +91,33 @@ export class FilterPreferencesComponent implements OnInit, OnDestroy {
});
}
onFilterModeChange() {
onFilterModeChange(): void {
this.updatePreference(['filterMode'], this.selectedFilterMode);
}
onVisibleFiltersChange(): void {
if (this.selectedVisibleFilters.length < MIN_VISIBLE_FILTERS) {
this.messageService.add({
severity: 'warn',
summary: 'Minimum Filters Required',
detail: `Please select at least ${MIN_VISIBLE_FILTERS} filters.`,
life: 2000
});
return;
}
if (this.selectedVisibleFilters.length > MAX_VISIBLE_FILTERS) {
this.messageService.add({
severity: 'warn',
summary: 'Maximum Filters Exceeded',
detail: `Please select at most ${MAX_VISIBLE_FILTERS} filters.`,
life: 2000
});
return;
}
this.updatePreference(['visibleFilters'], this.selectedVisibleFilters);
}
get selectionCountText(): string {
return `${this.selectedVisibleFilters.length} of ${this.allFilterOptions.length} selected`;
}
}