-
-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathbook-browse-facet-definitions.ts
More file actions
71 lines (65 loc) · 2.22 KB
/
Copy pathbook-browse-facet-definitions.ts
File metadata and controls
71 lines (65 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import {type BrowseFacetDefinitions} from '../../../shared/browse/facets';
import {
BOOK_QUERY_FACET_KEYS,
isBookQueryFacetKey,
type BookQueryFacetKey,
} from '../data/book-query-params';
import {type BookSummary} from '../data/book-response.models';
import {
COLUMNS_BY_KEY,
FACET_FIELDS,
OPEN_RAIL_FACETS,
type BookFacetEntity,
type BookFacetLabelDeps,
} from './book-browse-fields';
export interface BookFacetLink {
readonly key: BookQueryFacetKey;
readonly value: string;
}
interface BookFacetEntityWithId extends BookFacetEntity {
readonly id?: string | number;
}
export function bookFacetLabelDeps(
shelves: readonly BookFacetEntityWithId[],
libraries: readonly BookFacetEntityWithId[],
translate: (key: string) => string,
): BookFacetLabelDeps {
const byId = (entities: readonly BookFacetEntityWithId[]) =>
new Map(entities.map(entity => [`${entity.id}`, entity]));
const shelvesById = byId(shelves);
const librariesById = byId(libraries);
return {
shelf: id => shelvesById.get(id),
library: id => librariesById.get(id),
translate,
};
}
function registeredFacetLabelKey(key: BookQueryFacetKey): string {
return FACET_FIELDS.get(key)!.labelKey;
}
export function bookFacetDefinitions(deps: BookFacetLabelDeps): BrowseFacetDefinitions<BookQueryFacetKey> {
return {
order: BOOK_QUERY_FACET_KEYS,
isKey: isBookQueryFacetKey,
labelKey: registeredFacetLabelKey,
openByDefault: OPEN_RAIL_FACETS,
kind: key => FACET_FIELDS.get(key)?.facet.kind,
valueOrder: key => FACET_FIELDS.get(key)?.facet.order,
valueDomain: key => FACET_FIELDS.get(key)?.facet.domain,
valueBuckets: key => FACET_FIELDS.get(key)?.facet.buckets,
fileSize: key => FACET_FIELDS.get(key)?.facet.fileSize ?? false,
valueLabel: (key, value) => FACET_FIELDS.get(key)?.facet.valueLabel?.(value, deps) ?? null,
valueIcon: (key, value) => FACET_FIELDS.get(key)?.facet.valueIcon?.(value, deps) ?? null,
};
}
export function bookFacetLinks(
book: BookSummary,
columnField: string,
): readonly BookFacetLink[] {
const facet = COLUMNS_BY_KEY.get(columnField)?.facet;
if (!facet?.bookValues) {
return [];
}
const key = facet.key;
return facet.bookValues(book).map(value => ({key, value}));
}