feat(shelf): add shelf filtering options and integrate into book processing (#2268)
* feat(shelf): add shelf filtering options and integrate into book processing Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * fix(book-rule-evaluator): normalize array values to lowercase for rule comparisons Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * feat(filter): enhance library and shelf filtering logic for 'or' and 'and' modes Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * feat(shelf): implement support for multiple shelf IDs in book retrieval and filtering Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> --------- Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
+12
@@ -28,6 +28,8 @@ export interface Filter<T extends FilterValue = FilterValue> {
|
||||
}
|
||||
|
||||
export type FilterType =
|
||||
| 'library'
|
||||
| 'shelf'
|
||||
| 'author'
|
||||
| 'category'
|
||||
| 'series'
|
||||
@@ -205,6 +207,8 @@ export class BookFilterComponent implements OnInit, OnDestroy {
|
||||
private _selectedFilterMode: BookFilterMode = 'and';
|
||||
expandedPanels: number[] = [0];
|
||||
readonly filterLabels: Record<FilterType, string> = {
|
||||
library: 'Library',
|
||||
shelf: 'Shelf',
|
||||
author: 'Author',
|
||||
category: 'Genre',
|
||||
series: 'Series',
|
||||
@@ -249,6 +253,14 @@ export class BookFilterComponent implements OnInit, OnDestroy {
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.subscribe(() => {
|
||||
this.filterStreams = {
|
||||
library: this.getFilterStream(
|
||||
(book) => (book.libraryId ? [{id: book.libraryId, name: book.libraryName}] : []),
|
||||
'id', 'name'
|
||||
),
|
||||
shelf: this.getFilterStream(
|
||||
(book) => (book.shelves ? book.shelves.map(s => ({id: s.id, name: s.name})) : []),
|
||||
'id', 'name'
|
||||
),
|
||||
author: this.getFilterStream(
|
||||
(book: Book) => Array.isArray(book.metadata?.authors) ? book.metadata.authors.map(name => ({id: name, name})) : [],
|
||||
'id', 'name'
|
||||
|
||||
@@ -105,6 +105,14 @@ export class SideBarFilter implements BookFilter {
|
||||
return bookYear ? filterValues.some(val => val == bookYear || val == bookYear.toString()) : false;
|
||||
case 'fileSize':
|
||||
return filterValues.some(range => isFileSizeInRange(book.fileSizeKb, range));
|
||||
case 'library':
|
||||
return mode === 'or'
|
||||
? filterValues.some(val => val == book.libraryId)
|
||||
: filterValues.every(val => val == book.libraryId);
|
||||
case 'shelf':
|
||||
return mode === 'or'
|
||||
? filterValues.some(val => book.shelves?.some(s => s.id == val))
|
||||
: filterValues.every(val => book.shelves?.some(s => s.id == val));
|
||||
case 'shelfStatus':
|
||||
const shelved = book.shelves && book.shelves.length > 0 ? 'shelved' : 'unshelved';
|
||||
return filterValues.includes(shelved);
|
||||
|
||||
@@ -96,6 +96,10 @@ export class ShelfService {
|
||||
return this.shelfStateSubject.value.shelves?.find(s => s.id === id);
|
||||
}
|
||||
|
||||
getShelvesFromState(): Shelf[] {
|
||||
return this.shelfStateSubject.value.shelves ?? [];
|
||||
}
|
||||
|
||||
getBookCount(shelfId: number): Observable<number> {
|
||||
return this.bookService.bookState$.pipe(
|
||||
map(state =>
|
||||
|
||||
@@ -138,6 +138,8 @@
|
||||
<p-multiSelect [options]="fileType" formControlName="value" display="chip" class="value-input" appendTo="body" placeholder="Select File Types"></p-multiSelect>
|
||||
} @else if (ruleCtrl.get('field')?.value === 'library') {
|
||||
<p-multiSelect [options]="libraryOptions" formControlName="value" display="chip" class="value-input" appendTo="body" placeholder="Select Libraries"></p-multiSelect>
|
||||
} @else if (ruleCtrl.get('field')?.value === 'shelf') {
|
||||
<p-multiSelect [options]="shelfOptions" formControlName="value" display="chip" class="value-input" appendTo="body" placeholder="Select Shelves"></p-multiSelect>
|
||||
} @else if (ruleCtrl.get('field')?.value === 'categories') {
|
||||
<p-multiSelect [options]="categoryOptions" formControlName="value" display="chip" class="value-input" appendTo="body" placeholder="Select Genres"></p-multiSelect>
|
||||
} @else {
|
||||
@@ -151,6 +153,8 @@
|
||||
<p-select [options]="fileType" formControlName="value" placeholder="Select File Type" appendTo="body" class="value-input"></p-select>
|
||||
} @else if (ruleCtrl.get('field')?.value === 'library') {
|
||||
<p-select [options]="libraryOptions" formControlName="value" placeholder="Select Library" appendTo="body" class="value-input"></p-select>
|
||||
} @else if (ruleCtrl.get('field')?.value === 'shelf') {
|
||||
<p-select [options]="shelfOptions" formControlName="value" placeholder="Select Shelf" appendTo="body" class="value-input"></p-select>
|
||||
} @else if (ruleCtrl.get('field')?.value === 'categories') {
|
||||
<p-select [options]="categoryOptions" formControlName="value" placeholder="Select Genre" appendTo="body" class="value-input"></p-select>
|
||||
} @else {
|
||||
|
||||
@@ -20,6 +20,8 @@ import {CheckboxChangeEvent, CheckboxModule} from "primeng/checkbox";
|
||||
import {UserService} from "../../settings/user-management/user.service";
|
||||
import {IconDisplayComponent} from '../../../shared/components/icon-display/icon-display.component';
|
||||
import {BookService} from '../../book/service/book.service';
|
||||
import {ShelfService} from '../../book/service/shelf.service';
|
||||
import {Shelf} from '../../book/model/shelf.model';
|
||||
|
||||
export type RuleOperator =
|
||||
| 'equals'
|
||||
@@ -41,6 +43,7 @@ export type RuleOperator =
|
||||
|
||||
export type RuleField =
|
||||
| 'library'
|
||||
| 'shelf'
|
||||
| 'title'
|
||||
| 'subtitle'
|
||||
| 'authors'
|
||||
@@ -116,6 +119,7 @@ export type GroupFormGroup = FormGroup<{
|
||||
|
||||
const FIELD_CONFIGS: Record<RuleField, FullFieldConfig> = {
|
||||
library: {label: 'Library'},
|
||||
shelf: {label: 'Shelf'},
|
||||
readStatus: {label: 'Read Status'},
|
||||
dateFinished: {label: 'Date Finished', type: 'date'},
|
||||
lastReadTime: {label: 'Last Read Time', type: 'date'},
|
||||
@@ -203,6 +207,8 @@ export class MagicShelfComponent implements OnInit {
|
||||
|
||||
libraries: Library[] = [];
|
||||
libraryOptions: { label: string; value: number }[] = [];
|
||||
shelves: Shelf[] = [];
|
||||
shelfOptions: { label: string; value: number }[] = [];
|
||||
categoryOptions: { label: string; value: string }[] = [];
|
||||
|
||||
form = new FormGroup({
|
||||
@@ -217,6 +223,7 @@ export class MagicShelfComponent implements OnInit {
|
||||
editMode!: boolean;
|
||||
|
||||
libraryService = inject(LibraryService);
|
||||
shelfService = inject(ShelfService);
|
||||
bookService = inject(BookService);
|
||||
magicShelfService = inject(MagicShelfService);
|
||||
ref = inject(DynamicDialogRef);
|
||||
@@ -286,6 +293,12 @@ export class MagicShelfComponent implements OnInit {
|
||||
label: lib.name,
|
||||
value: lib.id!
|
||||
}));
|
||||
|
||||
this.shelves = this.shelfService.getShelvesFromState();
|
||||
this.shelfOptions = this.shelves.map(shelf => ({
|
||||
label: shelf.name,
|
||||
value: shelf.id!
|
||||
}));
|
||||
}
|
||||
|
||||
buildGroupFromData(data: GroupRule): GroupFormGroup {
|
||||
@@ -355,14 +368,14 @@ export class MagicShelfComponent implements OnInit {
|
||||
if (!field) return [...baseOperators, ...multiValueOperators];
|
||||
|
||||
const config = FIELD_CONFIGS[field];
|
||||
const isMultiValueField = ['library', 'authors', 'categories', 'moods', 'tags', 'readStatus', 'fileType', 'language', 'title', 'subtitle', 'publisher', 'seriesName', 'isbn13', 'isbn10'].includes(field);
|
||||
const isMultiValueField = ['library', 'shelf', 'authors', 'categories', 'moods', 'tags', 'readStatus', 'fileType', 'language', 'title', 'subtitle', 'publisher', 'seriesName', 'isbn13', 'isbn10'].includes(field);
|
||||
const operators = [...baseOperators];
|
||||
|
||||
if (isMultiValueField) {
|
||||
operators.push(...multiValueOperators);
|
||||
}
|
||||
|
||||
const isTextEligible = !['library', 'readStatus', 'fileType'].includes(field);
|
||||
const isTextEligible = !['library', 'shelf', 'readStatus', 'fileType'].includes(field);
|
||||
|
||||
if (config.type === 'number' || config.type === 'decimal' || config.type === 'date') {
|
||||
operators.push(...comparisonOperators);
|
||||
|
||||
@@ -51,6 +51,8 @@ export class BookRuleEvaluatorService {
|
||||
return [String(this.getFileExtension(book.fileName) ?? '').toLowerCase()];
|
||||
case 'library':
|
||||
return [String(book.libraryId)];
|
||||
case 'shelf':
|
||||
return (book.shelves ?? []).map(s => String(s.id));
|
||||
case 'language':
|
||||
return [String(book.metadata?.language ?? '').toLowerCase()];
|
||||
case 'title':
|
||||
@@ -70,14 +72,15 @@ export class BookRuleEvaluatorService {
|
||||
}
|
||||
};
|
||||
|
||||
const isNumericIdField = rule.field === 'library' || rule.field === 'shelf';
|
||||
const ruleList = Array.isArray(rule.value)
|
||||
? rule.value.map(v => String(v).toLowerCase())
|
||||
: (rule.value ? [String(rule.value).toLowerCase()] : []);
|
||||
? rule.value.map(v => isNumericIdField ? String(v) : String(v).toLowerCase())
|
||||
: (rule.value ? [isNumericIdField ? String(rule.value) : String(rule.value).toLowerCase()] : []);
|
||||
|
||||
switch (rule.operator) {
|
||||
case 'equals':
|
||||
if (Array.isArray(value)) {
|
||||
return value.some(v => ruleList.includes(v));
|
||||
return value.some(v => ruleList.includes(isNumericIdField ? String(v) : String(v).toLowerCase()));
|
||||
}
|
||||
if (value instanceof Date && ruleVal instanceof Date) {
|
||||
return value.getTime() === ruleVal.getTime();
|
||||
@@ -86,7 +89,7 @@ export class BookRuleEvaluatorService {
|
||||
|
||||
case 'not_equals':
|
||||
if (Array.isArray(value)) {
|
||||
return value.every(v => !ruleList.includes(v));
|
||||
return value.every(v => !ruleList.includes(isNumericIdField ? String(v) : String(v).toLowerCase()));
|
||||
}
|
||||
if (value instanceof Date && ruleVal instanceof Date) {
|
||||
return value.getTime() !== ruleVal.getTime();
|
||||
@@ -196,6 +199,8 @@ export class BookRuleEvaluatorService {
|
||||
switch (field) {
|
||||
case 'library':
|
||||
return book.libraryId;
|
||||
case 'shelf':
|
||||
return (book.shelves ?? []).map(s => s.id);
|
||||
case 'readStatus':
|
||||
return book.readStatus ?? 'UNSET';
|
||||
case 'fileType':
|
||||
|
||||
Reference in New Issue
Block a user