Support per-library and per-shelf sorting and view preferences

This commit is contained in:
aditya.chandel
2025-05-27 21:05:24 -07:00
committed by Aditya Chandel
parent 9c6d45eecd
commit 9acd062634
21 changed files with 559 additions and 135 deletions
@@ -0,0 +1,46 @@
<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>
<i class="pi pi-info-circle text-sky-600"
pTooltip="Configure sorting options for your library and shelves shown 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">Library:
<i class="pi pi-info-circle text-sky-600"
pTooltip="Choose how books are sorted and displayed in the library sidebar."
tooltipPosition="right"
style="cursor: pointer;">
</i>
</p>
<div class="flex gap-4 justify-start">
<p-select
size="small"
[options]="sortingOptions"
[(ngModel)]="selectedLibrarySorting"
(ngModelChange)="onLibrarySortingChange()"
placeholder="Select Sorting">
</p-select>
</div>
<p class="py-4">Shelf:
<i class="pi pi-info-circle text-sky-600"
pTooltip="Choose how books are sorted and displayed in the shelf sidebar."
tooltipPosition="right"
style="cursor: pointer;">
</i>
</p>
<div class="flex gap-4 justify-start">
<p-select
size="small"
[options]="sortingOptions"
[(ngModel)]="selectedShelfSorting"
(ngModelChange)="onShelfSortingChange()"
placeholder="Select Sorting">
</p-select>
</div>
</div>
</div>
@@ -0,0 +1,84 @@
import {Component, inject, OnDestroy, OnInit} from '@angular/core';
import {Select} from 'primeng/select';
import {Tooltip} from 'primeng/tooltip';
import {SidebarLibrarySorting, SidebarShelfSorting, 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-sorting-preferences',
imports: [
Select,
Tooltip,
FormsModule
],
templateUrl: './sidebar-sorting-preferences.component.html',
styleUrl: './sidebar-sorting-preferences.component.scss'
})
export class SidebarSortingPreferencesComponent implements OnInit, OnDestroy {
readonly sortingOptions = [
{label: 'Name | Ascending', value: {field: 'name', order: 'asc'}},
{label: 'Name | Descending', value: {field: 'name', order: 'desc'}},
{label: 'Creation Date | Ascending', value: {field: 'id', order: 'asc'}},
{label: 'Creation Date | Descending', value: {field: 'id', order: 'desc'}},
];
selectedLibrarySorting: SidebarLibrarySorting = {field: 'id', order: 'asc'};
selectedShelfSorting: SidebarShelfSorting = {field: 'id', order: 'asc'};
private readonly userService = inject(UserService);
private readonly messageService = inject(MessageService);
userData$: Observable<User | null> = this.userService.userData$;
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 {
this.selectedLibrarySorting = settings.sidebarLibrarySorting;
this.selectedShelfSorting = settings.sidebarShelfSorting;
}
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
});
}
onLibrarySortingChange() {
this.updatePreference(['sidebarLibrarySorting'], this.selectedLibrarySorting);
}
onShelfSortingChange() {
this.updatePreference(['sidebarShelfSorting'], this.selectedShelfSorting);
}
}
@@ -0,0 +1,5 @@
<div class="w-full h-[calc(100vh-12.1rem)] overflow-y-auto border rounded-lg enclosing-container">
<app-view-preferences></app-view-preferences>
<p-divider></p-divider>
<app-sidebar-sorting-preferences></app-sidebar-sorting-preferences>
</div>
@@ -0,0 +1,3 @@
.enclosing-container {
border-color: var(--p-content-border-color);
}
@@ -0,0 +1,25 @@
import {Component} from '@angular/core';
import {Divider} from 'primeng/divider';
import {FormsModule} from '@angular/forms';
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';
@Component({
selector: 'app-view-preferences-parent',
standalone: true,
imports: [
Divider,
FormsModule,
TableModule,
ToastModule,
ViewPreferencesComponent,
SidebarSortingPreferencesComponent
],
templateUrl: './view-preferences-parent.component.html',
styleUrl: './view-preferences-parent.component.scss'
})
export class ViewPreferencesParentComponent {
}
@@ -0,0 +1,79 @@
<div class="p-4">
<p class="text-lg pb-6 pt-4">
Library and Shelf View & Sort Preferences:
<i class="pi pi-info-circle text-sky-600"
pTooltip="Configure default and per-library/shelf preferences for sorting and view mode (grid or table) in the browser."
tooltipPosition="right"
style="cursor: pointer;"></i>
</p>
<div class="grid grid-cols-[auto,1fr] pl-6 gap-y-4 gap-x-4 items-center">
<p>Default:</p>
<div class="flex gap-4 pl-4">
<p-select size="small" [options]="sortOptions" optionLabel="label" optionValue="field"
[(ngModel)]="selectedSort" placeholder="Sort" class="w-full md:w-72"></p-select>
<p-select size="small" [options]="sortDirectionOptions" [(ngModel)]="selectedSortDir"
placeholder="Direction" class="w-full md:w-44"></p-select>
<p-select size="small" [options]="viewModeOptions" [(ngModel)]="selectedView"
placeholder="View" class="w-full md:w-44"></p-select>
</div>
</div>
<div class="grid grid-cols-[auto,1fr] pl-6 gap-y-4 gap-x-4 items-center mt-10">
<p>Overrides:</p>
<div class="flex gap-4">
<p-button label="Add New Override" outlined size="small" (onClick)="addOverride()"
[disabled]="availableLibraries.length === 0"></p-button>
</div>
</div>
<div class="pl-2 pt-2 min-w-[400px] max-w-[1000px]">
<p-table *ngIf="overrides.length > 0" [value]="overrides" class="relative z-0" [responsiveLayout]="'scroll'">
<ng-template pTemplate="header">
<tr>
<th class="min-w-[150px] max-w-[220px]">Type</th>
<th class="min-w-[220px] max-w-[280px]">Name</th>
<th class="min-w-[190px] max-w-[250px]">Sort By</th>
<th class="min-w-[125px]">Sort Dir</th>
<th class="min-w-[125px]">View</th>
<th class="w-[75px] text-center"></th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-override let-rowIndex="rowIndex">
<tr>
<td><p-select size="small" [options]="entityTypeOptions" [(ngModel)]="override.entityType"
class="w-full" appendTo="body"></p-select></td>
<td>
<ng-container *ngIf="override.entityType">
<p-select size="small"
[options]="getAvailableEntities(rowIndex, override.entityType)"
[(ngModel)]="override.library"
optionLabel="label" optionValue="value"
class="w-full" appendTo="body"></p-select>
</ng-container>
</td>
<td><p-select size="small" [options]="sortOptions" optionLabel="label" optionValue="field"
[(ngModel)]="override.sort" class="w-full" appendTo="body"></p-select></td>
<td><p-select size="small" [options]="sortDirectionOptions" [(ngModel)]="override.sortDir"
class="w-full" appendTo="body"></p-select></td>
<td><p-select size="small" [options]="viewModeOptions" [(ngModel)]="override.view"
class="w-full" appendTo="body"></p-select></td>
<td class="text-center">
<p-button icon="pi pi-trash" severity="danger" class="p-button-sm" [outlined]="true"
(onClick)="removeOverride(rowIndex)"></p-button>
</td>
</tr>
</ng-template>
</p-table>
</div>
<div class="flex justify-start px-6 pt-4">
<p-button label="Save" icon="pi pi-save" outlined size="small" (onClick)="saveSettings()"></p-button>
</div>
</div>
@@ -0,0 +1,175 @@
import {Component, inject, OnInit} from '@angular/core';
import {Button} from 'primeng/button';
import {NgIf} from '@angular/common';
import {MessageService} from 'primeng/api';
import {Select} from 'primeng/select';
import {TableModule} from 'primeng/table';
import {Tooltip} from 'primeng/tooltip';
import {User, UserService} from '../../user-management/user.service';
import {LibraryService} from '../../../book/service/library.service';
import {ShelfService} from '../../../book/service/shelf.service';
import {combineLatest} from 'rxjs';
import {FormsModule} from '@angular/forms';
import {ToastModule} from 'primeng/toast';
@Component({
selector: 'app-view-preferences',
standalone: true,
imports: [
Select,
Tooltip,
FormsModule,
Button,
NgIf,
TableModule,
ToastModule
],
templateUrl: './view-preferences.component.html',
styleUrl: './view-preferences.component.scss'
})
export class ViewPreferencesComponent implements OnInit {
sortOptions = [
{label: 'Title', field: 'title'},
{label: 'Title + Series', field: 'titleSeries'},
{label: 'Author', field: 'author'},
{label: 'Last Read', field: 'lastReadTime'},
{label: 'Added On', field: 'addedOn'},
{label: 'File Size', field: 'fileSizeKb'},
{label: 'Locked', field: 'locked'},
{label: 'Publisher', field: 'publisher'},
{label: 'Published Date', field: 'publishedDate'},
{label: 'Amazon Rating', field: 'amazonRating'},
{label: 'Amazon #', field: 'amazonReviewCount'},
{label: 'Goodreads Rating', field: 'goodreadsRating'},
{label: 'Goodreads #', field: 'goodreadsReviewCount'},
{label: 'Pages', field: 'pageCount'}
];
entityTypeOptions = [
{label: 'Library', value: 'LIBRARY'},
{label: 'Shelf', value: 'SHELF'}
];
sortDirectionOptions = [
{label: 'Ascending', value: 'ASC'},
{label: 'Descending', value: 'DESC'}
];
viewModeOptions = [
{label: 'Grid', value: 'GRID'},
{label: 'Table', value: 'TABLE'}
];
libraryOptions: { label: string; value: number }[] = [];
shelfOptions: { label: string; value: number }[] = [];
selectedSort: string = 'title';
selectedSortDir: 'ASC' | 'DESC' = 'ASC';
selectedView: 'GRID' | 'TABLE' = 'GRID';
overrides: {
entityType: 'LIBRARY' | 'SHELF';
library: number;
sort: string;
sortDir: 'ASC' | 'DESC';
view: 'GRID' | 'TABLE';
}[] = [];
private user: User | null = null;
private libraryService = inject(LibraryService);
private shelfService = inject(ShelfService);
private userService = inject(UserService);
private messageService = inject(MessageService);
ngOnInit(): void {
combineLatest([
this.userService.getMyself(),
this.libraryService.getAllLibrariesFromAPI(),
this.shelfService.shelfState$
]).subscribe(([user, libraries, shelfState]) => {
this.user = user;
const prefs = user.userSettings?.entityViewPreferences;
const global = prefs?.global;
this.selectedSort = global?.sortKey ?? 'title';
this.selectedSortDir = global?.sortDir ?? 'ASC';
this.selectedView = global?.view ?? 'GRID';
this.overrides = (prefs?.overrides ?? []).map(o => ({
entityType: o.entityType,
library: o.entityId,
sort: o.preferences.sortKey,
sortDir: o.preferences.sortDir ?? 'ASC',
view: o.preferences.view ?? 'GRID'
}));
this.libraryOptions = (libraries ?? []).filter(lib => lib.id !== undefined).map(lib => ({
label: lib.name,
value: lib.id!
}));
this.shelfOptions = (shelfState?.shelves ?? []).filter(s => s.id !== undefined).map(s => ({
label: s.name,
value: s.id!
}));
});
}
getAvailableEntities(index: number, type: 'LIBRARY' | 'SHELF') {
const selected = this.overrides.map((o, i) => i !== index ? o.library : null);
const source = type === 'LIBRARY' ? this.libraryOptions : this.shelfOptions;
return source.filter(opt => !selected.includes(opt.value) || this.overrides[index]?.library === opt.value);
}
get availableLibraries() {
const used = new Set(this.overrides.map(o => `${o.entityType}_${o.library}`));
const withEntityType = (options: { label: string; value: number }[], entityType: 'LIBRARY' | 'SHELF') =>
options.map(opt => ({...opt, entityType}));
return [...withEntityType(this.libraryOptions, 'LIBRARY'), ...withEntityType(this.shelfOptions, 'SHELF')]
.filter(opt => !used.has(`${opt.entityType}_${opt.value}`));
}
addOverride(): void {
const next = this.availableLibraries[0];
if (next) {
this.overrides.push({
entityType: next.entityType,
library: next.value,
sort: 'title',
sortDir: 'ASC',
view: 'GRID'
});
}
}
removeOverride(index: number): void {
this.overrides.splice(index, 1);
}
saveSettings(): void {
if (!this.user) return;
const payload = {
global: {
sortKey: this.selectedSort,
sortDir: this.selectedSortDir,
view: this.selectedView
},
overrides: this.overrides.map(o => ({
entityType: o.entityType,
entityId: o.library,
preferences: {
sortKey: o.sort,
sortDir: o.sortDir,
view: o.view
}
}))
};
this.userService.updateUserSetting(this.user.id, 'entityViewPreferences', payload);
this.messageService.add({ severity: 'success', summary: 'Preferences Saved', detail: 'Your sorting and view preferences were saved successfully.' });
}
}