feat(ui): book browse field registry, facet and sort query helpers (#2571)
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import {type BrowseFacetSelection} from '../../../shared/browse/facets';
|
||||
import {HttpParams} from '@angular/common/http';
|
||||
|
||||
import {
|
||||
@@ -7,21 +8,18 @@ import {
|
||||
} from '../../../core/data/browse.models';
|
||||
export const BOOK_QUERY_FACET_KEYS = [
|
||||
'author',
|
||||
'series',
|
||||
'genre',
|
||||
'tag',
|
||||
'mood',
|
||||
'language',
|
||||
'series',
|
||||
'publisher',
|
||||
'library',
|
||||
'shelf',
|
||||
'language',
|
||||
'narrator',
|
||||
'file_type',
|
||||
'read_status',
|
||||
'personal_rating',
|
||||
'amazon_rating',
|
||||
'goodreads_rating',
|
||||
'hardcover_rating',
|
||||
'ranobedb_rating',
|
||||
'library',
|
||||
'shelf',
|
||||
'age_rating',
|
||||
'content_rating',
|
||||
'match_score',
|
||||
@@ -29,6 +27,12 @@ export const BOOK_QUERY_FACET_KEYS = [
|
||||
'file_size',
|
||||
'page_count',
|
||||
'shelf_status',
|
||||
'amazon_rating',
|
||||
'goodreads_rating',
|
||||
'hardcover_rating',
|
||||
'ranobedb_rating',
|
||||
'lubimyczytac_rating',
|
||||
'audible_rating',
|
||||
'comic_character',
|
||||
'comic_team',
|
||||
'comic_location',
|
||||
@@ -36,12 +40,23 @@ export const BOOK_QUERY_FACET_KEYS = [
|
||||
] as const;
|
||||
|
||||
export const BOOK_QUERY_SORT_KEYS = [
|
||||
'addedOn',
|
||||
'random',
|
||||
'title',
|
||||
'authorName',
|
||||
'authorSortName',
|
||||
'seriesName',
|
||||
'seriesNumber',
|
||||
'publisher',
|
||||
'addedOn',
|
||||
'lastReadTime',
|
||||
'publishedDate',
|
||||
'personalRating',
|
||||
'readingProgress',
|
||||
'publisher',
|
||||
'seriesNumber',
|
||||
'pageCount',
|
||||
'narrator',
|
||||
'language',
|
||||
'readStatus',
|
||||
'dateFinished',
|
||||
'amazonRating',
|
||||
'amazonReviewCount',
|
||||
'goodreadsRating',
|
||||
@@ -49,20 +64,15 @@ export const BOOK_QUERY_SORT_KEYS = [
|
||||
'hardcoverRating',
|
||||
'hardcoverReviewCount',
|
||||
'ranobedbRating',
|
||||
'narrator',
|
||||
'pageCount',
|
||||
'language',
|
||||
'personalRating',
|
||||
'lastReadTime',
|
||||
'readStatus',
|
||||
'dateFinished',
|
||||
'readingProgress',
|
||||
'lubimyczytacRating',
|
||||
'audibleRating',
|
||||
'audibleReviewCount',
|
||||
] as const;
|
||||
|
||||
export type BookQueryFacetKey = typeof BOOK_QUERY_FACET_KEYS[number];
|
||||
export type BookQuerySortKey = typeof BOOK_QUERY_SORT_KEYS[number];
|
||||
export type FacetLogic = BrowseFacetLogic;
|
||||
export type FacetValueMap = Readonly<Partial<Record<BookQueryFacetKey, readonly string[]>>>;
|
||||
export type FacetValueMap = BrowseFacetSelection<BookQueryFacetKey>;
|
||||
export type SortDirection = BrowseSortDirection;
|
||||
|
||||
export const EMPTY_FACET_SELECTION: FacetValueMap = {};
|
||||
@@ -99,6 +109,60 @@ export function isBookQuerySortKey(value: string): value is BookQuerySortKey {
|
||||
return BOOK_QUERY_SORT_KEY_SET.has(value);
|
||||
}
|
||||
|
||||
export function parseFacetParams(tokens: readonly string[]): FacetValueMap {
|
||||
const facets = new Map<BookQueryFacetKey, string[]>();
|
||||
for (const token of tokens) {
|
||||
const separator = token.indexOf(':');
|
||||
if (separator <= 0) {
|
||||
continue;
|
||||
}
|
||||
const key = token.slice(0, separator);
|
||||
if (!isBookQueryFacetKey(key)) {
|
||||
continue;
|
||||
}
|
||||
const values = facets.get(key) ?? [];
|
||||
values.push(token.slice(separator + 1));
|
||||
facets.set(key, values);
|
||||
}
|
||||
return normalizeFacetValueMap(Object.fromEntries(facets));
|
||||
}
|
||||
|
||||
export function bookFacetQueryParams(facets: FacetValueMap) {
|
||||
const tokens = Object.entries(facets).flatMap(([key, values]) =>
|
||||
values.map(value => `${key}:${value}`),
|
||||
);
|
||||
return {facet: tokens.length > 0 ? tokens : null};
|
||||
}
|
||||
|
||||
export function parseSortTermsToken(token: string | null): BookSortTerm[] {
|
||||
if (!token) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const seen = new Set<string>();
|
||||
const terms: BookSortTerm[] = [];
|
||||
for (const rawTerm of token.split(',')) {
|
||||
const term = rawTerm.trim();
|
||||
if (!term) {
|
||||
continue;
|
||||
}
|
||||
const descending = term.startsWith('-');
|
||||
const key = (descending ? term.slice(1) : term).trim();
|
||||
if (!isBookQuerySortKey(key) || seen.has(key)) {
|
||||
continue;
|
||||
}
|
||||
seen.add(key);
|
||||
terms.push({key, direction: descending ? 'desc' : 'asc'});
|
||||
}
|
||||
return terms;
|
||||
}
|
||||
|
||||
export function sortTermsToken(terms: readonly BookSortTerm[]): string {
|
||||
return terms
|
||||
.map(term => term.direction === 'desc' ? `-${term.key}` : term.key)
|
||||
.join(',');
|
||||
}
|
||||
|
||||
export function normalizeBookPageParams(params: BookPageParams): BookPageParams {
|
||||
return {
|
||||
...normalizeBookQueryParams(params),
|
||||
@@ -116,7 +180,7 @@ export function toIdsHttpParams(params: BookQueryParams): HttpParams {
|
||||
}
|
||||
|
||||
function appendSortParam(httpParams: HttpParams, sort: readonly BookSortTerm[]): HttpParams {
|
||||
return sort.length === 0 ? httpParams : httpParams.set('sort', serializeSort(sort));
|
||||
return sort.length === 0 ? httpParams : httpParams.set('sort', sortTermsToken(sort));
|
||||
}
|
||||
|
||||
export function normalizeBookQueryParams(params: BookQueryParams): BookQueryParams {
|
||||
@@ -174,9 +238,3 @@ function appendFacetParams(httpParams: HttpParams, facets: FacetValueMap): HttpP
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function serializeSort(sort: readonly BookSortTerm[]): string {
|
||||
return sort
|
||||
.map(term => `${term.direction === 'desc' ? '-' : ''}${term.key}`)
|
||||
.join(',');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user