Implement user-configurable sidebar filter sorting
This commit is contained in:
committed by
Aditya Chandel
parent
49b56b78e9
commit
47d8aa9ef0
+42
-26
@@ -1,6 +1,6 @@
|
||||
import {Component, EventEmitter, inject, Input, OnInit, Output} from '@angular/core';
|
||||
import {combineLatest, Observable, of, Subject} from 'rxjs';
|
||||
import {map} from 'rxjs/operators';
|
||||
import {filter, map, take} from 'rxjs/operators';
|
||||
import {BookService} from '../../../service/book.service';
|
||||
import {Library} from '../../../model/library.model';
|
||||
import {Shelf} from '../../../model/shelf.model';
|
||||
@@ -11,6 +11,7 @@ import {AsyncPipe, NgClass, TitleCasePipe} from '@angular/common';
|
||||
import {Badge} from 'primeng/badge';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {SelectButton} from 'primeng/selectbutton';
|
||||
import {UserService} from '../../../../settings/user-management/user.service';
|
||||
|
||||
type Filter<T> = { value: T; bookCount: number };
|
||||
|
||||
@@ -148,41 +149,54 @@ export class BookFilterComponent implements OnInit {
|
||||
};
|
||||
|
||||
bookService = inject(BookService);
|
||||
userService = inject(UserService);
|
||||
|
||||
ngOnInit(): void {
|
||||
if (this.entity$ && this.entityType$) {
|
||||
combineLatest([
|
||||
this.userService.userState$.pipe(
|
||||
filter((u): u is NonNullable<typeof u> => !!u),
|
||||
take(1)
|
||||
),
|
||||
this.entity$ ?? of(null),
|
||||
this.entityType$ ?? of(EntityType.ALL_BOOKS)
|
||||
]).subscribe(([user]) => {
|
||||
const sortMode = user.userSettings?.filterSortingMode ?? 'count';
|
||||
this.filterStreams = {
|
||||
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'),
|
||||
matchScore: this.getFilterStream((book: Book) => getMatchScoreRangeFilters(book.metadataMatchScore), 'id', 'name', true),
|
||||
publisher: this.getFilterStream((book) => (book.metadata?.publisher ? [{id: book.metadata.publisher, name: book.metadata.publisher}] : []), 'id', 'name'),
|
||||
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'),
|
||||
amazonRating: this.getFilterStream((book: Book) => getRatingRangeFilters(book.metadata?.amazonRating!), 'id', 'name'),
|
||||
goodreadsRating: this.getFilterStream((book: Book) => getRatingRangeFilters(book.metadata?.goodreadsRating!), 'id', 'name'),
|
||||
hardcoverRating: this.getFilterStream((book: Book) => getRatingRangeFilters(book.metadata?.hardcoverRating!), 'id', 'name'),
|
||||
pageCount: this.getFilterStream((book: Book) => getPageCountRangeFilters(book.metadata?.pageCount!), 'id', 'name'),
|
||||
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),
|
||||
matchScore: this.getFilterStream((book: Book) => getMatchScoreRangeFilters(book.metadataMatchScore), 'id', 'name', 'sortIndex'),
|
||||
publisher: this.getFilterStream((book) => (book.metadata?.publisher ? [{id: book.metadata.publisher, name: book.metadata.publisher}] : []), 'id', 'name', sortMode),
|
||||
shelfStatus: this.getFilterStream(getShelfStatusFilter, 'id', 'name', sortMode),
|
||||
publishedDate: this.getFilterStream(extractPublishedYearFilter, 'id', 'name', sortMode),
|
||||
language: this.getFilterStream(getLanguageFilter, 'id', 'name', sortMode),
|
||||
fileSize: this.getFilterStream((book: Book) => getFileSizeRangeFilters(book.fileSizeKb), 'id', 'name', sortMode),
|
||||
amazonRating: this.getFilterStream((book: Book) => getRatingRangeFilters(book.metadata?.amazonRating!), 'id', 'name', sortMode),
|
||||
goodreadsRating: this.getFilterStream((book: Book) => getRatingRangeFilters(book.metadata?.goodreadsRating!), 'id', 'name', sortMode),
|
||||
hardcoverRating: this.getFilterStream((book: Book) => getRatingRangeFilters(book.metadata?.hardcoverRating!), 'id', 'name', sortMode),
|
||||
pageCount: this.getFilterStream((book: Book) => getPageCountRangeFilters(book.metadata?.pageCount!), 'id', 'name', sortMode),
|
||||
};
|
||||
|
||||
this.filterTypes = Object.keys(this.filterStreams);
|
||||
}
|
||||
this.setExpandedPanels();
|
||||
});
|
||||
|
||||
if (this.resetFilter$) {
|
||||
this.resetFilter$.subscribe(() => this.clearActiveFilter());
|
||||
}
|
||||
|
||||
this.setExpandedPanels();
|
||||
}
|
||||
|
||||
private getFilterStream<T>(
|
||||
extractor: (book: Book) => T[] | undefined,
|
||||
idKey: keyof T,
|
||||
nameKey: keyof T,
|
||||
sortByIndex = false
|
||||
sortMode: 'count' | 'alphabetical' | 'sortIndex' = 'count'
|
||||
): Observable<Filter<T[keyof T]>[]> {
|
||||
return combineLatest([this.bookService.bookState$, this.entity$ ?? of(null), this.entityType$ ?? of(EntityType.ALL_BOOKS)]).pipe(
|
||||
return combineLatest([
|
||||
this.bookService.bookState$,
|
||||
this.entity$ ?? of(null),
|
||||
this.entityType$ ?? of(EntityType.ALL_BOOKS)
|
||||
]).pipe(
|
||||
map(([state, entity, entityType]) => {
|
||||
const filteredBooks = this.filterBooksByEntityType(state.books || [], entity, entityType);
|
||||
const filterMap = new Map<any, Filter<any>>();
|
||||
@@ -200,14 +214,16 @@ export class BookFilterComponent implements OnInit {
|
||||
const result = Array.from(filterMap.values());
|
||||
|
||||
return result.sort((a, b) => {
|
||||
if (sortByIndex) {
|
||||
if (sortMode === 'sortIndex') {
|
||||
return (a.value.sortIndex ?? 999) - (b.value.sortIndex ?? 999);
|
||||
} else {
|
||||
return (
|
||||
b.bookCount - a.bookCount ||
|
||||
a.value[nameKey].toString().localeCompare(b.value[nameKey].toString())
|
||||
);
|
||||
}
|
||||
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())
|
||||
);
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
-4
@@ -159,10 +159,6 @@ export class MetadataViewerComponent implements OnInit, OnChanges {
|
||||
this.bookService.readBook(bookId!, reader);
|
||||
}
|
||||
|
||||
getAuthorNames(authors: string[]): string {
|
||||
return authors.join(', ');
|
||||
}
|
||||
|
||||
download(bookId: number) {
|
||||
this.bookService.downloadFile(bookId);
|
||||
}
|
||||
|
||||
@@ -75,6 +75,7 @@ export interface UserSettings {
|
||||
newPdfReaderSetting: NewPdfReaderSetting;
|
||||
sidebarLibrarySorting: SidebarLibrarySorting;
|
||||
sidebarShelfSorting: SidebarShelfSorting;
|
||||
filterSortingMode: 'alphabetical' | 'count';
|
||||
entityViewPreferences: EntityViewPreferences;
|
||||
}
|
||||
|
||||
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
<div class="p-4">
|
||||
<div class="flex items-center gap-2 pb-6 pt-4">
|
||||
<p class="text-lg">Sidebar Filter Sorting:</p>
|
||||
<i class="pi pi-info-circle text-sky-600"
|
||||
pTooltip="This setting controls the order in which filter values (like authors, categories, etc.) appear in the sidebar."
|
||||
tooltipPosition="right"
|
||||
style="cursor: pointer;">
|
||||
</i>
|
||||
</div>
|
||||
|
||||
<div class="grid grid-cols-[auto,1fr] pl-6 gap-y-4 gap-x-4 items-center">
|
||||
<p class="py-1">Sorting Mode:
|
||||
<i class="pi pi-info-circle text-sky-600"
|
||||
pTooltip="You can sort filter options alphabetically (A–Z) or by the number of books that match each filter value. For example, categories with more books can appear at the top."
|
||||
tooltipPosition="right"
|
||||
style="cursor: pointer;">
|
||||
</i>
|
||||
</p>
|
||||
<div class="flex gap-4 justify-start">
|
||||
<p-select
|
||||
size="small"
|
||||
[options]="filterSortingOptions"
|
||||
[(ngModel)]="selectedFilterSorting"
|
||||
(ngModelChange)="onFilterSortingChange()"
|
||||
placeholder="Select Sorting">
|
||||
</p-select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
import {Component, inject, OnDestroy, OnInit} from '@angular/core';
|
||||
import {Select} from 'primeng/select';
|
||||
import {Tooltip} from 'primeng/tooltip';
|
||||
import {User, UserService, UserSettings} from '../../user-management/user.service';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {Observable, Subscription} from 'rxjs';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
|
||||
@Component({
|
||||
selector: 'app-sidebar-filter-sorting-prefs-component',
|
||||
imports: [
|
||||
Select,
|
||||
Tooltip,
|
||||
FormsModule
|
||||
],
|
||||
templateUrl: './sidebar-filter-sorting-prefs-component.html',
|
||||
styleUrl: './sidebar-filter-sorting-prefs-component.scss'
|
||||
})
|
||||
export class SidebarFilterSortingPrefsComponent implements OnInit, OnDestroy {
|
||||
|
||||
readonly filterSortingOptions = [
|
||||
{label: 'Alphabetical (A–Z)', value: 'alphabetical'},
|
||||
{label: 'Book Count (High to Low)', value: 'count'}
|
||||
];
|
||||
|
||||
selectedFilterSorting: 'alphabetical' | 'count' = 'count';
|
||||
|
||||
private readonly userService = inject(UserService);
|
||||
private readonly messageService = inject(MessageService);
|
||||
|
||||
userData$: Observable<User | null> = this.userService.userState$;
|
||||
private subscription?: Subscription;
|
||||
private currentUser: User | null = null;
|
||||
|
||||
ngOnInit(): void {
|
||||
this.subscription = this.userData$.subscribe(user => {
|
||||
if (user) {
|
||||
this.currentUser = user;
|
||||
this.loadPreferences(user.userSettings);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.subscription?.unsubscribe();
|
||||
}
|
||||
|
||||
private loadPreferences(settings: UserSettings): void {
|
||||
console.log(settings.filterSortingMode)
|
||||
this.selectedFilterSorting = settings.filterSortingMode;
|
||||
}
|
||||
|
||||
private updatePreference(path: string[], value: any): void {
|
||||
if (!this.currentUser) return;
|
||||
let target: any = this.currentUser.userSettings;
|
||||
for (let i = 0; i < path.length - 1; i++) {
|
||||
target = target[path[i]] ||= {};
|
||||
}
|
||||
target[path.at(-1)!] = value;
|
||||
|
||||
const [rootKey] = path;
|
||||
const updatedValue = this.currentUser.userSettings[rootKey as keyof UserSettings];
|
||||
this.userService.updateUserSetting(this.currentUser.id, rootKey, updatedValue);
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'Preferences Updated',
|
||||
detail: 'Your preferences have been saved successfully.',
|
||||
life: 2000
|
||||
});
|
||||
}
|
||||
|
||||
onFilterSortingChange(): void {
|
||||
this.updatePreference(['filterSortingMode'], this.selectedFilterSorting);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
<div class="p-4">
|
||||
<div class="flex items-center gap-2 pb-6 pt-4">
|
||||
<p class="text-lg ">Sidebar Library and Shelf Sorting Perference</p>
|
||||
<p class="text-lg">Sidebar Library and Shelf Sorting Preference</p>
|
||||
<i class="pi pi-info-circle text-sky-600"
|
||||
pTooltip="Configure sorting options for your library and shelves shown in the sidebar."
|
||||
tooltipPosition="right"
|
||||
|
||||
-2
@@ -53,7 +53,6 @@ export class SidebarSortingPreferencesComponent implements OnInit, OnDestroy {
|
||||
this.selectedShelfSorting = settings.sidebarShelfSorting;
|
||||
}
|
||||
|
||||
|
||||
private updatePreference(path: string[], value: any): void {
|
||||
if (!this.currentUser) return;
|
||||
let target: any = this.currentUser.userSettings;
|
||||
@@ -80,5 +79,4 @@ export class SidebarSortingPreferencesComponent implements OnInit, OnDestroy {
|
||||
onShelfSortingChange() {
|
||||
this.updatePreference(['sidebarShelfSorting'], this.selectedShelfSorting);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
@@ -2,4 +2,6 @@
|
||||
<app-view-preferences></app-view-preferences>
|
||||
<p-divider></p-divider>
|
||||
<app-sidebar-sorting-preferences></app-sidebar-sorting-preferences>
|
||||
<p-divider></p-divider>
|
||||
<app-sidebar-filter-sorting-prefs-component></app-sidebar-filter-sorting-prefs-component>
|
||||
</div>
|
||||
|
||||
+3
-1
@@ -5,6 +5,7 @@ import {TableModule} from 'primeng/table';
|
||||
import {ToastModule} from 'primeng/toast';
|
||||
import {ViewPreferencesComponent} from './view-preferences/view-preferences.component';
|
||||
import {SidebarSortingPreferencesComponent} from './sidebar-sorting-preferences/sidebar-sorting-preferences.component';
|
||||
import {SidebarFilterSortingPrefsComponent} from './sidebar-filter-sorting-prefs-component/sidebar-filter-sorting-prefs-component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-view-preferences-parent',
|
||||
@@ -15,7 +16,8 @@ import {SidebarSortingPreferencesComponent} from './sidebar-sorting-preferences/
|
||||
TableModule,
|
||||
ToastModule,
|
||||
ViewPreferencesComponent,
|
||||
SidebarSortingPreferencesComponent
|
||||
SidebarSortingPreferencesComponent,
|
||||
SidebarFilterSortingPrefsComponent
|
||||
],
|
||||
templateUrl: './view-preferences-parent.component.html',
|
||||
styleUrl: './view-preferences-parent.component.scss'
|
||||
|
||||
Reference in New Issue
Block a user