refactor(ui): updated book browser view preferences (#2574)

This commit is contained in:
Alex
2026-09-26 12:55:45 +01:00
committed by GitHub
parent a2f41b29d8
commit 62bb79e860
24 changed files with 96 additions and 1140 deletions
@@ -26,71 +26,15 @@
<div class="multi-sort-editor">
<span class="multi-sort-label">{{ t('sortOrderLabel') }}</span>
<app-multi-sort-popover
[sortCriteria]="globalSortAsOptions"
[availableSortOptions]="allSortAsOptions"
(criteriaChange)="onGlobalSortCriteriaChange($event)">
</app-multi-sort-popover>
<app-book-browse-multi-sort-editor
class="block max-w-96"
[terms]="globalSortTerms"
[options]="editorSortOptions()"
(termsChange)="onGlobalSortTermsChange($event)" />
</div>
</div>
<div class="setting-item">
<div class="setting-header-standalone">
<span class="setting-label">{{ t('visibleSortFields') }}</span>
</div>
<p class="setting-description">
{{ t('visibleSortFieldsDesc') }}
<span class="sort-field-count">({{ sortFieldSelectionCountText }})</span>
</p>
<div class="sort-field-order-editor">
<div
#sortFieldList
class="sort-field-order-list"
cdkDropList
[cdkDropListData]="visibleSortFields"
(cdkDropListDropped)="onSortFieldDrop($event)">
@for (f of visibleSortFields; track f; let i = $index) {
<div class="sort-field-order-row" cdkDrag>
<div class="sort-field-drag-handle" cdkDragHandle>
<i class="pi pi-bars"></i>
</div>
<span class="sort-field-rank">{{ i + 1 }}.</span>
<span class="sort-field-label">{{ getSortFieldLabel(f) }}</span>
<button
type="button"
class="remove-sort-field-btn"
(click)="removeSortField(i)"
[disabled]="visibleSortFields.length <= minSortFields"
[pTooltip]="t('remove')"
tooltipPosition="top">
<i class="pi pi-times"></i>
</button>
</div>
}
</div>
<div class="sort-field-actions-row">
@if (availableSortFieldsToAdd.length > 0 && visibleSortFields.length < maxSortFields) {
<p-select
size="small"
[options]="availableSortFieldsToAdd"
[(ngModel)]="selectedAddSortField"
optionLabel="label"
optionValue="value"
[placeholder]="t('addSortField')"
[style]="{ width: '200px' }"
appendTo="body"
(onChange)="addSortField()">
</p-select>
}
<button
type="button"
class="reset-sort-fields-btn"
(click)="resetSortFieldsToDefaults()"
[pTooltip]="t('resetToDefaults')"
tooltipPosition="top">
<i class="pi pi-refresh"></i>
</button>
</div>
<div class="format-overlay-editor">
<p-toggleswitch [(ngModel)]="overlayBookType"></p-toggleswitch>
<span class="setting-label">{{ t('formatOverlay') }}</span>
</div>
</div>
@@ -146,15 +90,8 @@
icon="pi pi-sort"
size="small"
[outlined]="true"
(onClick)="overrideSortPop.toggle($event)">
(onClick)="editOverrideSort(rowIndex)">
</p-button>
<p-popover #overrideSortPop [dismissable]="true" appendTo="body">
<app-multi-sort-popover
[sortCriteria]="override.sortCriteriaAsOptions"
[availableSortOptions]="allSortAsOptions"
(criteriaChange)="onOverrideSortCriteriaChange(rowIndex, $event)">
</app-multi-sort-popover>
</p-popover>
</td>
<td>
<p-select size="small" [options]="viewModeOptions" [(ngModel)]="override.view"
@@ -73,148 +73,9 @@
color: var(--text-color);
}
// Visible sort fields editor styles
.sort-field-count {
font-weight: 500;
color: var(--p-primary-color);
}
.sort-field-order-editor {
margin-top: 0.6563rem;
}
.sort-field-order-list {
display: flex;
flex-direction: column;
gap: 0.4375rem;
min-height: 40px;
max-height: 320px;
max-width: 350px;
overflow-y: auto;
margin-bottom: 0.6563rem;
}
.sort-field-order-row {
.format-overlay-editor {
display: flex;
align-items: center;
gap: 0.4375rem;
padding: 0.4375rem 0.6563rem;
background-color: var(--overlay-background);
border: 1px solid var(--border-color);
border-radius: 6px;
cursor: move;
transition: background-color 0.15s ease, box-shadow 0.15s ease;
&:hover {
background-color: var(--ground-background);
}
}
.cdk-drag-preview {
display: flex;
align-items: center;
gap: 0.4375rem;
padding: 0.3063rem 0.6563rem;
background-color: var(--card-background);
border: 1px solid var(--primary-color);
border-radius: 6px;
box-shadow: 0 5px 15px rgb(0 0 0 / 25%);
}
.cdk-drag-placeholder {
opacity: 0.3;
}
.cdk-drag-animating {
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
}
.sort-field-order-list.cdk-drop-list-dragging .sort-field-order-row:not(.cdk-drag-placeholder) {
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
}
.sort-field-drag-handle {
display: flex;
align-items: center;
justify-content: center;
color: var(--text-secondary-color);
cursor: grab;
&:active {
cursor: grabbing;
}
i {
font-size: 0.7438rem;
}
}
.sort-field-rank {
font-weight: 600;
color: var(--primary-color);
min-width: 1.5313rem;
}
.sort-field-label {
flex: 1;
font-size: var(--app-text-sm);
color: var(--text-color);
}
.remove-sort-field-btn {
display: flex;
align-items: center;
justify-content: center;
width: 1.5313rem;
height: 1.5313rem;
padding: 0;
border: none;
border-radius: 4px;
background-color: transparent;
color: var(--p-red-500);
cursor: pointer;
transition: background-color 0.15s ease;
i {
font-size: 0.7438rem;
}
&:hover:not(:disabled) {
background-color: var(--ground-background);
}
&:disabled {
opacity: 0.4;
cursor: not-allowed;
}
}
.sort-field-actions-row {
display: flex;
align-items: center;
gap: 0.4375rem;
}
.reset-sort-fields-btn {
display: flex;
align-items: center;
justify-content: center;
width: 1.5313rem;
height: 1.5313rem;
padding: 0;
border: none;
border-radius: 4px;
background-color: transparent;
color: var(--text-secondary-color);
cursor: pointer;
transition: background-color 0.15s ease, color 0.15s ease;
i {
font-size: 0.7438rem;
}
&:hover {
background-color: var(--ground-background);
color: var(--primary-color);
}
gap: 0.6563rem;
margin-top: 0.875rem;
}
@@ -1,11 +1,12 @@
import {Component, DestroyRef, effect, ElementRef, inject, Injector, OnInit, viewChild} from '@angular/core';
import {Component, computed, DestroyRef, effect, inject, Injector, OnInit} from '@angular/core';
import {injectQuery} from '@tanstack/angular-query-experimental';
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
import {Button} from '@openng/optimus-ui/button';
import {MessageService} from '@openng/optimus-ui/api';
import {Select} from '@openng/optimus-ui/select';
import {TableModule} from '@openng/optimus-ui/table';
import {DEFAULT_VISIBLE_SORT_FIELDS, SortCriterion, User, UserService} from '../../user-management/user.service';
import {User, UserService} from '../../user-management/user.service';
import {LibraryService} from '../../../book/service/library.service';
import {ShelfService} from '../../../book/service/shelf.service';
import {MagicShelfService} from '../../../magic-shelf/service/magic-shelf.service';
@@ -14,10 +15,20 @@ import {FormsModule} from '@angular/forms';
import {Tooltip} from '@openng/optimus-ui/tooltip';
import {ToggleSwitch} from '@openng/optimus-ui/toggleswitch';
import {TranslocoDirective, TranslocoService} from '@jsverse/transloco';
import {SortDirection, SortOption} from '../../../book/model/sort.model';
import {MultiSortPopoverComponent} from '../../../book/components/book-browser/sorting/multi-sort-popover/multi-sort-popover.component';
import {Popover} from '@openng/optimus-ui/popover';
import {CdkDrag, CdkDragDrop, CdkDragHandle, CdkDropList, moveItemInArray} from '@angular/cdk/drag-drop';
import {take} from 'rxjs/operators';
import {DialogLauncherService} from '../../../../shared/services/dialog-launcher.service';
import {browseSortCriteria} from '../../../../shared/browse/sort';
import {
DEFAULT_BOOK_SORT_TERMS,
EMPTY_FACET_SELECTION,
type BookSortTerm,
} from '../../../book/data/book-query-params';
import {BookQueryService} from '../../../book/data/book-query.service';
import {bookSortOptions, bookSortTermsFromCriteria} from '../../../book/browse/book-browse-sort';
import {entityViewSortCriteria, entityViewSortPatch} from '../../user-management/entity-view-preferences';
import {type BookBrowseMultiSortDialogResult} from '../../../book/browse/book-browse-multi-sort-dialog.component';
import {BookBrowseMultiSortEditorComponent} from '../../../book/browse/book-browse-multi-sort-editor.component';
@Component({
selector: 'app-view-preferences',
@@ -30,11 +41,7 @@ import {CdkDrag, CdkDragDrop, CdkDragHandle, CdkDropList, moveItemInArray} from
Tooltip,
ToggleSwitch,
TranslocoDirective,
MultiSortPopoverComponent,
Popover,
CdkDropList,
CdkDrag,
CdkDragHandle
BookBrowseMultiSortEditorComponent
],
templateUrl: './view-preferences.component.html',
styleUrl: './view-preferences.component.scss'
@@ -42,39 +49,6 @@ import {CdkDrag, CdkDragDrop, CdkDragHandle, CdkDropList, moveItemInArray} from
export class ViewPreferencesComponent implements OnInit {
private t = inject(TranslocoService);
private readonly sortOptionDefs: {field: string; translationKey: string}[] = [
{field: 'title', translationKey: 'sortTitle'},
{field: 'fileName', translationKey: 'sortFileName'},
{field: 'filePath', translationKey: 'sortFilePath'},
{field: 'author', translationKey: 'sortAuthor'},
{field: 'authorSurnameVorname', translationKey: 'sortAuthorSurname'},
{field: 'seriesName', translationKey: 'sortSeriesName'},
{field: 'seriesNumber', translationKey: 'sortSeriesNumber'},
{field: 'lastReadTime', translationKey: 'sortLastRead'},
{field: 'personalRating', translationKey: 'sortPersonalRating'},
{field: 'addedOn', translationKey: 'sortAddedOn'},
{field: 'fileSizeKb', translationKey: 'sortFileSize'},
{field: 'locked', translationKey: 'sortLocked'},
{field: 'publisher', translationKey: 'sortPublisher'},
{field: 'publishedDate', translationKey: 'sortPublishedDate'},
{field: 'readStatus', translationKey: 'sortReadStatus'},
{field: 'dateFinished', translationKey: 'sortDateFinished'},
{field: 'readingProgress', translationKey: 'sortReadingProgress'},
{field: 'bookType', translationKey: 'sortBookType'},
{field: 'amazonRating', translationKey: 'sortAmazonRating'},
{field: 'amazonReviewCount', translationKey: 'sortAmazonCount'},
{field: 'goodreadsRating', translationKey: 'sortGoodreadsRating'},
{field: 'goodreadsReviewCount', translationKey: 'sortGoodreadsCount'},
{field: 'hardcoverRating', translationKey: 'sortHardcoverRating'},
{field: 'hardcoverReviewCount', translationKey: 'sortHardcoverCount'},
{field: 'ranobedbRating', translationKey: 'sortRanobedbRating'},
{field: 'narrator', translationKey: 'sortNarrator'},
{field: 'pageCount', translationKey: 'sortPages'},
{field: 'random', translationKey: 'sortRandom'},
];
sortOptions: {label: string; field: string; translationKey: string}[] = [];
entityTypeOptions: {label: string; value: string; translationKey: string}[] = [];
viewModeOptions: {label: string; value: string; translationKey: string}[] = [];
@@ -95,31 +69,21 @@ export class ViewPreferencesComponent implements OnInit {
.map(shelf => ({label: shelf.name, value: shelf.id!}));
}
selectedSort: string = 'title';
selectedSortDir: 'ASC' | 'DESC' = 'ASC';
selectedView: 'GRID' | 'TABLE' = 'GRID';
overlayBookType: boolean = true;
autoSaveMetadata: boolean = false;
sortCriteria: SortCriterion[] = [];
globalSortTerms: readonly BookSortTerm[] = [];
// SortOption[] versions for the multi-sort-popover component
globalSortAsOptions: SortOption[] = [];
allSortAsOptions: SortOption[] = [];
// Visible sort fields configuration
visibleSortFields: string[] = [];
selectedAddSortField: string | null = null;
readonly minSortFields = 3;
readonly maxSortFields = 27;
private readonly sortFieldList = viewChild<ElementRef<HTMLElement>>('sortFieldList');
private readonly bookQuery = inject(BookQueryService);
private readonly sortTokensQuery = injectQuery(() =>
this.bookQuery.facets({facets: EMPTY_FACET_SELECTION, facetLogic: 'or'}));
readonly editorSortOptions = computed(() =>
bookSortOptions(this.sortTokensQuery.data()?.sortTokens ?? []));
overrides: {
entityType: 'LIBRARY' | 'SHELF' | 'MAGIC_SHELF';
library: number;
sort: string;
sortDir: 'ASC' | 'DESC';
sortCriteria: SortCriterion[];
sortCriteriaAsOptions: SortOption[];
sortTerms: readonly BookSortTerm[];
view: 'GRID' | 'TABLE';
}[] = [];
@@ -132,6 +96,7 @@ export class ViewPreferencesComponent implements OnInit {
private magicShelfService = inject(MagicShelfService);
private userService = inject(UserService);
private messageService = inject(MessageService);
private readonly dialogLauncher = inject(DialogLauncherService);
private readonly currentUser = this.userService.currentUser;
private hasInitializedPreferences = false;
@@ -139,15 +104,6 @@ export class ViewPreferencesComponent implements OnInit {
this.rebuildTranslatedLabels();
this.t.langChanges$.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(() => {
this.rebuildTranslatedLabels();
this.allSortAsOptions = this.sortOptions.map(o => ({
label: this.t.translate('settingsView.librarySort.' + o.translationKey),
field: o.field,
direction: SortDirection.ASCENDING
}));
this.globalSortAsOptions = this.toSortOptions(this.sortCriteria);
this.overrides.forEach(o => {
o.sortCriteriaAsOptions = this.toSortOptions(o.sortCriteria);
});
});
effect(() => {
@@ -160,51 +116,23 @@ export class ViewPreferencesComponent implements OnInit {
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.overlayBookType = global?.overlayBookType ?? true;
this.autoSaveMetadata = user.userSettings?.autoSaveMetadata ?? false;
if (global?.sortCriteria && global.sortCriteria.length > 0) {
this.sortCriteria = [...global.sortCriteria];
} else {
this.sortCriteria = [{field: this.selectedSort, direction: this.selectedSortDir}];
}
const storedTerms = bookSortTermsFromCriteria(entityViewSortCriteria(global));
this.globalSortTerms = storedTerms.length > 0 ? storedTerms : DEFAULT_BOOK_SORT_TERMS;
this.allSortAsOptions = this.sortOptions.map(option => ({
label: this.t.translate('settingsView.librarySort.' + option.translationKey),
field: option.field,
direction: SortDirection.ASCENDING
this.overrides = (prefs?.overrides ?? []).map(override => ({
entityType: override.entityType,
library: override.entityId,
sortTerms: bookSortTermsFromCriteria(entityViewSortCriteria(override.preferences)),
view: override.preferences.view
}));
this.globalSortAsOptions = this.toSortOptions(this.sortCriteria);
this.visibleSortFields = user.userSettings?.visibleSortFields
? [...user.userSettings.visibleSortFields]
: [...DEFAULT_VISIBLE_SORT_FIELDS];
this.overrides = (prefs?.overrides ?? []).map(override => {
const sortCriteria = override.preferences.sortCriteria?.length
? [...override.preferences.sortCriteria]
: [{field: override.preferences.sortKey, direction: override.preferences.sortDir ?? 'ASC'} as SortCriterion];
return {
entityType: override.entityType,
library: override.entityId,
sort: override.preferences.sortKey,
sortDir: override.preferences.sortDir ?? 'ASC',
sortCriteria,
sortCriteriaAsOptions: this.toSortOptions(sortCriteria),
view: override.preferences.view ?? 'GRID'
};
});
}, {injector: this.injector});
}
private rebuildTranslatedLabels(): void {
this.sortOptions = this.sortOptionDefs.map(o => ({
label: this.t.translate('settingsView.librarySort.' + o.translationKey),
field: o.field,
translationKey: o.translationKey
}));
this.entityTypeOptions = [
{label: this.t.translate('settingsView.librarySort.entityLibrary'), value: 'LIBRARY', translationKey: 'entityLibrary'},
{label: this.t.translate('settingsView.librarySort.entityShelf'), value: 'SHELF', translationKey: 'entityShelf'},
@@ -250,14 +178,10 @@ export class ViewPreferencesComponent implements OnInit {
addOverride(): void {
const next = this.availableLibraries[0];
if (next) {
const defaultCriteria: SortCriterion[] = [{field: 'title', direction: 'ASC'}];
this.overrides.push({
entityType: next.entityType,
library: next.value,
sort: 'title',
sortDir: 'ASC',
sortCriteria: defaultCriteria,
sortCriteriaAsOptions: this.toSortOptions(defaultCriteria),
sortTerms: DEFAULT_BOOK_SORT_TERMS,
view: 'GRID'
});
}
@@ -267,85 +191,22 @@ export class ViewPreferencesComponent implements OnInit {
this.overrides.splice(index, 1);
}
// Conversion helpers
private toSortOptions(criteria: SortCriterion[]): SortOption[] {
return criteria.map(c => ({
label: this.t.translate('settingsView.librarySort.' + (this.sortOptions.find(o => o.field === c.field)?.translationKey ?? c.field)),
field: c.field,
direction: c.direction === 'ASC' ? SortDirection.ASCENDING : SortDirection.DESCENDING
}));
onGlobalSortTermsChange(terms: readonly BookSortTerm[]): void {
this.globalSortTerms = terms;
}
private toSortCriteria(options: SortOption[]): SortCriterion[] {
return options.map(o => ({
field: o.field,
direction: o.direction === SortDirection.ASCENDING ? 'ASC' as const : 'DESC' as const
}));
}
onGlobalSortCriteriaChange(criteria: SortOption[]): void {
this.globalSortAsOptions = criteria;
this.sortCriteria = this.toSortCriteria(criteria);
this.syncLegacySort();
}
onOverrideSortCriteriaChange(index: number, criteria: SortOption[]): void {
this.overrides[index].sortCriteriaAsOptions = criteria;
this.overrides[index].sortCriteria = this.toSortCriteria(criteria);
this.overrides[index].sort = criteria[0]?.field ?? 'title';
this.overrides[index].sortDir = criteria[0]?.direction === SortDirection.ASCENDING ? 'ASC' : 'DESC';
}
private syncLegacySort(): void {
if (this.sortCriteria.length > 0) {
this.selectedSort = this.sortCriteria[0].field;
this.selectedSortDir = this.sortCriteria[0].direction;
}
}
// Visible sort fields management
getSortFieldLabel(field: string): string {
const key = this.sortOptions.find(opt => opt.field === field)?.translationKey;
return key ? this.t.translate('settingsView.librarySort.' + key) : field;
}
get availableSortFieldsToAdd(): {label: string; value: string}[] {
const used = new Set(this.visibleSortFields);
return this.sortOptions
.filter(opt => !used.has(opt.field))
.map(opt => ({label: this.t.translate('settingsView.librarySort.' + opt.translationKey), value: opt.field}));
}
get sortFieldSelectionCountText(): string {
return this.t.translate('settingsView.librarySort.sortFieldCount', {
count: this.visibleSortFields.length,
total: this.sortOptions.length
async editOverrideSort(index: number): Promise<void> {
const ref = await this.dialogLauncher.openMultiSortDialog({
terms: this.overrides[index].sortTerms,
options: this.editorSortOptions(),
});
ref?.onClose.pipe(take(1)).subscribe((result?: BookBrowseMultiSortDialogResult) => {
if (!result) {
return;
}
this.overrides = this.overrides.map((override, i) =>
i === index ? {...override, sortTerms: result.terms} : override);
});
}
onSortFieldDrop(event: CdkDragDrop<string[]>): void {
moveItemInArray(this.visibleSortFields, event.previousIndex, event.currentIndex);
}
addSortField(): void {
if (this.selectedAddSortField) {
this.visibleSortFields.push(this.selectedAddSortField);
this.selectedAddSortField = null;
requestAnimationFrame(() => {
const el = this.sortFieldList()?.nativeElement;
if (el) el.scrollTop = el.scrollHeight;
});
}
}
removeSortField(index: number): void {
if (this.visibleSortFields.length > this.minSortFields) {
this.visibleSortFields.splice(index, 1);
}
}
resetSortFieldsToDefaults(): void {
this.visibleSortFields = [...DEFAULT_VISIBLE_SORT_FIELDS];
}
saveSettings(): void {
@@ -353,12 +214,13 @@ export class ViewPreferencesComponent implements OnInit {
const prefs = structuredClone(this.user.userSettings.entityViewPreferences ?? {});
const sortPatch = (terms: readonly BookSortTerm[]) =>
entityViewSortPatch(browseSortCriteria(terms.length > 0 ? terms : DEFAULT_BOOK_SORT_TERMS));
prefs.global = {
...prefs.global,
sortKey: this.selectedSort,
sortDir: this.selectedSortDir,
sortCriteria: [...this.sortCriteria],
view: this.selectedView
...sortPatch(this.globalSortTerms),
view: this.selectedView,
overlayBookType: this.overlayBookType
};
prefs.overrides = this.overrides.map(o => {
@@ -370,20 +232,15 @@ export class ViewPreferencesComponent implements OnInit {
entityType: o.entityType,
entityId: o.library,
preferences: {
sortKey: o.sortCriteria[0]?.field ?? o.sort,
sortDir: o.sortCriteria[0]?.direction ?? o.sortDir,
sortCriteria: [...o.sortCriteria],
view: o.view,
coverSize: existing?.coverSize ?? 1.0,
seriesCollapsed: existing?.seriesCollapsed ?? false,
overlayBookType: existing?.overlayBookType ?? true
...(existing ?? prefs.global),
...sortPatch(o.sortTerms),
view: o.view
}
};
});
this.userService.updateUserSetting(this.user.id, 'entityViewPreferences', prefs);
this.userService.updateUserSetting(this.user.id, 'autoSaveMetadata', this.autoSaveMetadata);
this.userService.updateUserSetting(this.user.id, 'visibleSortFields', this.visibleSortFields);
this.messageService.add({
severity: 'success',