@internetarchive/collection-browser 4.3.1-alpha-webdev8165.0 → 4.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/index.js.map +1 -1
  2. package/dist/src/data-source/collection-browser-data-source.js.map +1 -1
  3. package/dist/src/manage/manage-bar.js +77 -77
  4. package/dist/src/manage/manage-bar.js.map +1 -1
  5. package/dist/src/models.d.ts +6 -0
  6. package/dist/src/models.js +16 -7
  7. package/dist/src/models.js.map +1 -1
  8. package/dist/src/restoration-state-handler.js +3 -1
  9. package/dist/src/restoration-state-handler.js.map +1 -1
  10. package/dist/src/tiles/base-tile-component.js.map +1 -1
  11. package/dist/src/tiles/grid/item-tile.js +138 -138
  12. package/dist/src/tiles/grid/item-tile.js.map +1 -1
  13. package/dist/src/tiles/models.js.map +1 -1
  14. package/dist/src/tiles/tile-dispatcher.js +216 -216
  15. package/dist/src/tiles/tile-dispatcher.js.map +1 -1
  16. package/dist/src/tiles/tile-display-value-provider.js.map +1 -1
  17. package/dist/test/data-source/collection-browser-data-source.test.js +2 -2
  18. package/dist/test/data-source/collection-browser-data-source.test.js.map +1 -1
  19. package/dist/test/manage/manage-bar.test.js +33 -33
  20. package/dist/test/manage/manage-bar.test.js.map +1 -1
  21. package/dist/test/restoration-state-handler.test.js +0 -70
  22. package/dist/test/restoration-state-handler.test.js.map +1 -1
  23. package/dist/test/tiles/list/tile-list-compact-header.test.js +12 -12
  24. package/dist/test/tiles/list/tile-list-compact-header.test.js.map +1 -1
  25. package/dist/test/tiles/list/tile-list.test.js +134 -134
  26. package/dist/test/tiles/list/tile-list.test.js.map +1 -1
  27. package/index.ts +28 -28
  28. package/package.json +2 -2
  29. package/src/data-source/collection-browser-data-source.ts +1465 -1465
  30. package/src/manage/manage-bar.ts +276 -276
  31. package/src/models.ts +895 -879
  32. package/src/restoration-state-handler.ts +550 -546
  33. package/src/tiles/base-tile-component.ts +65 -65
  34. package/src/tiles/grid/item-tile.ts +346 -346
  35. package/src/tiles/models.ts +8 -8
  36. package/src/tiles/tile-dispatcher.ts +527 -527
  37. package/src/tiles/tile-display-value-provider.ts +134 -134
  38. package/test/data-source/collection-browser-data-source.test.ts +193 -193
  39. package/test/manage/manage-bar.test.ts +347 -347
  40. package/test/restoration-state-handler.test.ts +480 -569
  41. package/test/tiles/list/tile-list-compact-header.test.ts +43 -43
  42. package/test/tiles/list/tile-list.test.ts +576 -576
package/src/models.ts CHANGED
@@ -1,879 +1,895 @@
1
- import type { TemplateResult } from 'lit';
2
- import { msg } from '@lit/localize';
3
- import type { MediaType } from '@internetarchive/field-parsers';
4
- import {
5
- AggregationSortType,
6
- HitType,
7
- SearchReview,
8
- SearchResult,
9
- SortDirection,
10
- } from '@internetarchive/search-service';
11
- import { collapseRepeatedQuotes } from './utils/collapse-repeated-quotes';
12
- import { resolveMediatype } from './utils/resolve-mediatype';
13
-
14
- import { loginRequiredIcon } from './assets/img/icons/login-required';
15
- import { restrictedIcon } from './assets/img/icons/restricted';
16
-
17
- /**
18
- * Flags that can affect the visibility of content on a tile
19
- */
20
- interface TileFlags {
21
- loginRequired: boolean;
22
- contentWarning: boolean;
23
- }
24
-
25
- /**
26
- * Different types of tile overlays, corresponding to the above flags.
27
- */
28
- export type TileOverlayType = 'login-required' | 'content-warning';
29
-
30
- export const TILE_OVERLAY_TEXT: Record<TileOverlayType, string> = {
31
- 'login-required': msg('Log in to view this item'),
32
- 'content-warning': msg('Content may be inappropriate'),
33
- };
34
-
35
- export const TILE_OVERLAY_ICONS: Record<TileOverlayType, TemplateResult> = {
36
- 'login-required': loginRequiredIcon,
37
- 'content-warning': restrictedIcon,
38
- };
39
-
40
- /**
41
- * What type of request produced a given set of hits:
42
- * - `search_query`: Hits produced by an explicit user query and/or applied filters on any page
43
- * - `collection_members`: Hits produced for a collection page without any query or filters
44
- * - `profile_tab`: Hits produced for a tab of the profile page without any query or filters
45
- * - `unknown`: Hits produced via any other means
46
- */
47
- export type HitRequestSource =
48
- | 'search_query'
49
- | 'collection_members'
50
- | 'profile_tab'
51
- | 'unknown';
52
-
53
- /**
54
- * Class for converting & storing raw search results in the correct format for UI tiles.
55
- */
56
- export class TileModel {
57
- /** For TV hits. List of identifiers for any commercials contained. */
58
- adIds?: string[];
59
-
60
- averageRating?: number;
61
-
62
- /** For Web Archive hits on profile pages. List of capture dates for the current URL. */
63
- captureDates?: Date[];
64
-
65
- /** Whether this tile is currently checked for item management functions */
66
- checked: boolean;
67
-
68
- collectionIdentifier?: string;
69
-
70
- collectionName?: string;
71
-
72
- collectionFilesCount: number;
73
-
74
- collections: string[];
75
-
76
- collectionSize: number;
77
-
78
- commentCount: number;
79
-
80
- creator?: string;
81
-
82
- creators: string[];
83
-
84
- /** A string representation of the publication date, used strictly for passing preformatted dates to the parent */
85
- dateStr?: string;
86
-
87
- /** Date added to public search (software-defined) [from MD field: addeddate] */
88
- dateAdded?: Date;
89
-
90
- /** Date archived (software-defined) item created on archive.org [from MD field: publicdate] */
91
- dateArchived?: Date;
92
-
93
- /** Date work published in the world (user-defined) [from MD field: date] */
94
- datePublished?: Date;
95
-
96
- /** Date reviewed (user-created) most recent review [from MD field: reviewdate] */
97
- dateReviewed?: Date;
98
-
99
- description?: string;
100
-
101
- /** For TV hits. List of URLs for any fact-checks of the contained clips. */
102
- factChecks?: string[];
103
-
104
- favCount: number;
105
-
106
- hitRequestSource: HitRequestSource;
107
-
108
- hitType?: HitType;
109
-
110
- href?: string;
111
-
112
- identifier?: string;
113
-
114
- /** Whether this model represents a TV clip */
115
- isClip?: boolean;
116
-
117
- issue?: string;
118
-
119
- itemCount: number;
120
-
121
- mediatype: MediaType;
122
-
123
- review?: SearchReview;
124
-
125
- source?: string;
126
-
127
- snippets?: string[];
128
-
129
- subjects: string[];
130
-
131
- thumbnailUrl?: string;
132
-
133
- title: string;
134
-
135
- tvClipCount?: number;
136
-
137
- viewCount?: number;
138
-
139
- volume?: string;
140
-
141
- weeklyViewCount?: number;
142
-
143
- loginRequired: boolean;
144
-
145
- contentWarning: boolean;
146
-
147
- constructor(
148
- result: SearchResult,
149
- hitRequestSource: HitRequestSource = 'unknown',
150
- ) {
151
- const flags = this.getFlags(result);
152
-
153
- this.adIds = result.ad_id?.values;
154
- this.averageRating = result.avg_rating?.value;
155
- this.captureDates = result.capture_dates?.values;
156
- this.checked = false;
157
- this.collections = result.collection?.values ?? [];
158
- this.collectionFilesCount = result.collection_files_count?.value ?? 0;
159
- this.collectionSize = result.collection_size?.value ?? 0;
160
- this.commentCount = result.num_reviews?.value ?? 0;
161
- this.creator = result.creator?.value;
162
- this.creators = result.creator?.values ?? [];
163
- this.dateAdded = result.addeddate?.value;
164
- this.dateArchived = result.publicdate?.value;
165
- this.datePublished = result.date?.value;
166
- this.dateReviewed = result.reviewdate?.value;
167
- this.description = result.description?.values.join('\n');
168
- this.factChecks = result.factcheck?.values;
169
- this.favCount = result.num_favorites?.value ?? 0;
170
- this.hitRequestSource = hitRequestSource;
171
- this.hitType = result.rawMetadata?.hit_type;
172
- this.href = collapseRepeatedQuotes(
173
- result.review?.__href__ ?? result.__href__?.value,
174
- );
175
- this.identifier = TileModel.cleanIdentifier(result.identifier);
176
- this.isClip = result.is_clip?.value;
177
- this.issue = result.issue?.value;
178
- this.itemCount = result.item_count?.value ?? 0;
179
- this.mediatype = resolveMediatype(result);
180
- this.review = result.review;
181
- this.snippets = result.highlight?.values ?? [];
182
- this.source = result.source?.value;
183
- this.subjects = result.subject?.values ?? [];
184
- this.thumbnailUrl = result.__img__?.value;
185
- this.title = result.title?.value ?? '';
186
- this.tvClipCount = result.num_clips?.value ?? 0;
187
- this.volume = result.volume?.value;
188
- this.viewCount = result.downloads?.value;
189
- this.weeklyViewCount = result.week?.value;
190
- this.loginRequired = flags.loginRequired;
191
- this.contentWarning = flags.contentWarning;
192
- }
193
-
194
- /**
195
- * Copies the contents of this TileModel onto a new instance
196
- */
197
- clone(): TileModel {
198
- const cloned = new TileModel({});
199
- cloned.adIds = this.adIds;
200
- cloned.averageRating = this.averageRating;
201
- cloned.captureDates = this.captureDates;
202
- cloned.checked = this.checked;
203
- cloned.collections = this.collections;
204
- cloned.collectionFilesCount = this.collectionFilesCount;
205
- cloned.collectionSize = this.collectionSize;
206
- cloned.commentCount = this.commentCount;
207
- cloned.creator = this.creator;
208
- cloned.creators = this.creators;
209
- cloned.dateStr = this.dateStr;
210
- cloned.dateAdded = this.dateAdded;
211
- cloned.dateArchived = this.dateArchived;
212
- cloned.datePublished = this.datePublished;
213
- cloned.dateReviewed = this.dateReviewed;
214
- cloned.description = this.description;
215
- cloned.factChecks = this.factChecks;
216
- cloned.favCount = this.favCount;
217
- cloned.hitRequestSource = this.hitRequestSource;
218
- cloned.hitType = this.hitType;
219
- cloned.href = this.href;
220
- cloned.identifier = this.identifier;
221
- cloned.isClip = this.isClip;
222
- cloned.issue = this.issue;
223
- cloned.itemCount = this.itemCount;
224
- cloned.mediatype = this.mediatype;
225
- cloned.snippets = this.snippets;
226
- cloned.source = this.source;
227
- cloned.subjects = this.subjects;
228
- cloned.thumbnailUrl = this.thumbnailUrl;
229
- cloned.title = this.title;
230
- cloned.tvClipCount = this.tvClipCount;
231
- cloned.volume = this.volume;
232
- cloned.viewCount = this.viewCount;
233
- cloned.weeklyViewCount = this.weeklyViewCount;
234
- cloned.loginRequired = this.loginRequired;
235
- cloned.contentWarning = this.contentWarning;
236
- return cloned;
237
- }
238
-
239
- /**
240
- * Whether this model represents the result of a TV search query.
241
- */
242
- get isTvSearchResult(): boolean {
243
- return (
244
- this.hitType === 'tv_clip' && this.hitRequestSource === 'search_query'
245
- );
246
- }
247
-
248
- /**
249
- * Determines the appropriate tile flags for the given search result
250
- * (login required and/or content warning)
251
- */
252
- private getFlags(result: SearchResult): TileFlags {
253
- const flags: TileFlags = {
254
- loginRequired: false,
255
- contentWarning: false,
256
- };
257
-
258
- // Check if item and item in "modifying" collection, setting above flags
259
- if (
260
- result.collection?.values.length &&
261
- result.mediatype?.value !== 'collection'
262
- ) {
263
- for (const collection of result.collection?.values ?? []) {
264
- if (collection === 'loggedin') {
265
- flags.loginRequired = true;
266
- if (flags.contentWarning) break;
267
- }
268
- if (collection === 'no-preview') {
269
- flags.contentWarning = true;
270
- if (flags.loginRequired) break;
271
- }
272
- }
273
- }
274
-
275
- return flags;
276
- }
277
-
278
- private static cleanIdentifier(
279
- identifier: string | undefined,
280
- ): string | undefined {
281
- // Some identifiers (e.g., from Whisper) represent documents rather than items, and
282
- // are suffixed with values that need to be stripped. Those values are separated
283
- // from the item identifier itself with '|'.
284
- const barIndex = identifier?.indexOf('|') ?? -1;
285
- const cleaned = barIndex > 0 ? identifier?.slice(0, barIndex) : identifier;
286
- return cleaned;
287
- }
288
- }
289
-
290
- export type RequestKind = 'full' | 'hits' | 'aggregations';
291
-
292
- export type CollectionDisplayMode = 'grid' | 'list-compact' | 'list-detail';
293
-
294
- export type TileDisplayMode =
295
- | 'grid'
296
- | 'list-compact'
297
- | 'list-detail'
298
- | 'list-header';
299
-
300
- /**
301
- * This is mainly used to set the cookies for the collection display mode.
302
- *
303
- * It allows the user to set different modes for different contexts (collection page, search page, profile page etc).
304
- */
305
- export type CollectionBrowserContext = 'collection' | 'search' | 'profile';
306
-
307
- /**
308
- * The sort fields shown in the sort filter bar
309
- */
310
- export enum SortField {
311
- 'default' = 'default',
312
- 'unrecognized' = 'unrecognized',
313
- 'relevance' = 'relevance',
314
- 'alltimeview' = 'alltimeview',
315
- 'weeklyview' = 'weeklyview',
316
- 'title' = 'title',
317
- 'date' = 'date',
318
- 'datearchived' = 'datearchived',
319
- 'datereviewed' = 'datereviewed',
320
- 'dateadded' = 'dateadded',
321
- 'datefavorited' = 'datefavorited',
322
- 'creator' = 'creator',
323
- }
324
-
325
- /**
326
- * Views-related sort fields
327
- */
328
- export const ALL_VIEWS_SORT_FIELDS = [
329
- SortField.weeklyview,
330
- SortField.alltimeview,
331
- ] as const;
332
- export type ViewsSortField = (typeof ALL_VIEWS_SORT_FIELDS)[number];
333
-
334
- /**
335
- * Date-related sort fields
336
- */
337
- export const ALL_DATE_SORT_FIELDS = [
338
- SortField.datefavorited,
339
- SortField.date,
340
- SortField.datearchived,
341
- SortField.datereviewed,
342
- SortField.dateadded,
343
- ] as const;
344
- export type DateSortField = (typeof ALL_DATE_SORT_FIELDS)[number];
345
-
346
- export interface SortOption {
347
- /**
348
- * The SortField enum member corresponding to this option.
349
- */
350
- field: SortField;
351
-
352
- /**
353
- * The default sort direction to apply when this sort option is first selected.
354
- */
355
- defaultSortDirection: SortDirection | null;
356
-
357
- /**
358
- * Whether this sort option allows its sort direction to be changed from the default.
359
- */
360
- canSetDirection: boolean;
361
-
362
- /**
363
- * Whether this sort option may appear in the sort bar.
364
- */
365
- shownInSortBar: boolean;
366
-
367
- /**
368
- * Whether this sort option is passed to the search service.
369
- * If false, then no sort param will be passed to the search service at all when
370
- * this sort option is selected.
371
- */
372
- handledBySearchService: boolean;
373
-
374
- /**
375
- * The string identifying this sort field to the search service & backend API.
376
- */
377
- searchServiceKey?: string;
378
-
379
- /**
380
- * The human-readable name to use for this option in the sort bar (if applicable).
381
- */
382
- displayName: string;
383
-
384
- /**
385
- * A list of URL param keys that should be mapped to this sort option.
386
- * E.g., both `title` and `titleSorter` in the URL map to the `SortField.title` option.
387
- */
388
- urlNames: (string | null | undefined)[];
389
- }
390
-
391
- export const SORT_OPTIONS: Record<SortField, SortOption> = {
392
- // Default sort is the case where the user has not specified a sort option via the sort bar or URL.
393
- // In these cases, we defer to whatever sort the backend chooses.
394
- // For the search page, the default is always relevance sort.
395
- // For collection pages _without a query_, the default is usually weekly views, but this can be
396
- // overridden by the collection's `sort-by` metadata entry. If a query _is_ specified, then the
397
- // default is again relevance sort.
398
- // For fav-* collections only, the default is instead sorting by date favorited.
399
- [SortField.default]: {
400
- field: SortField.default,
401
- defaultSortDirection: null,
402
- canSetDirection: false,
403
- shownInSortBar: false,
404
- handledBySearchService: false, // We rely on the PPS default sort handling in these cases
405
- displayName: '',
406
- urlNames: ['', null, undefined], // Empty or nullish sort params result in default sorting
407
- },
408
- // Unrecognized sort is the case where the user has specified a sort in the URL, but it is not
409
- // one of the options listed in this map. We still want these unrecognized sorts to be applied
410
- // when searching, but they are not displayed in the sort bar and we do not actively manage
411
- // their URL param beyond flipping the direction as needed.
412
- [SortField.unrecognized]: {
413
- field: SortField.unrecognized,
414
- defaultSortDirection: null,
415
- canSetDirection: true,
416
- shownInSortBar: false,
417
- handledBySearchService: true, // The unrecognized sort param is passed along as-is
418
- displayName: '',
419
- urlNames: [],
420
- },
421
- // Relevance sort is only available when there is a user-specified query that relevancy can be
422
- // scored against. Therefore, it does not appear as a sort bar option when browsing a collection
423
- // with no query set. When relevance is the page default (e.g., for regular searches), it is
424
- // remapped to SortField.default and does not produce a URL param. When it is NOT the default
425
- // (e.g., TV searches), it writes `sort=relevance` to the URL. The `_score` alias is retained
426
- // for backwards compatibility (it's what the PPS uses under the hood).
427
- [SortField.relevance]: {
428
- field: SortField.relevance,
429
- defaultSortDirection: null,
430
- canSetDirection: false,
431
- shownInSortBar: true,
432
- handledBySearchService: false,
433
- displayName: 'Relevance',
434
- urlNames: ['relevance', '_score'],
435
- },
436
- [SortField.alltimeview]: {
437
- field: SortField.alltimeview,
438
- defaultSortDirection: 'desc',
439
- canSetDirection: true,
440
- shownInSortBar: true,
441
- handledBySearchService: true,
442
- searchServiceKey: 'downloads',
443
- displayName: 'All-time views',
444
- urlNames: ['downloads'],
445
- },
446
- [SortField.weeklyview]: {
447
- field: SortField.weeklyview,
448
- defaultSortDirection: 'desc',
449
- canSetDirection: true,
450
- shownInSortBar: true,
451
- handledBySearchService: true,
452
- searchServiceKey: 'week',
453
- displayName: 'Weekly views',
454
- urlNames: ['week'],
455
- },
456
- [SortField.title]: {
457
- field: SortField.title,
458
- defaultSortDirection: 'asc',
459
- canSetDirection: true,
460
- shownInSortBar: true,
461
- handledBySearchService: true,
462
- searchServiceKey: 'titleSorter',
463
- displayName: 'Title',
464
- urlNames: ['title', 'titleSorter'],
465
- },
466
- [SortField.date]: {
467
- field: SortField.date,
468
- defaultSortDirection: 'desc',
469
- canSetDirection: true,
470
- shownInSortBar: true,
471
- handledBySearchService: true,
472
- searchServiceKey: 'date',
473
- displayName: 'Date published',
474
- urlNames: ['date'],
475
- },
476
- [SortField.datearchived]: {
477
- field: SortField.datearchived,
478
- defaultSortDirection: 'desc',
479
- canSetDirection: true,
480
- shownInSortBar: true,
481
- handledBySearchService: true,
482
- searchServiceKey: 'publicdate',
483
- displayName: 'Date archived',
484
- urlNames: ['publicdate'],
485
- },
486
- [SortField.datereviewed]: {
487
- field: SortField.datereviewed,
488
- defaultSortDirection: 'desc',
489
- canSetDirection: true,
490
- shownInSortBar: true,
491
- handledBySearchService: true,
492
- searchServiceKey: 'reviewdate',
493
- displayName: 'Date reviewed',
494
- urlNames: ['reviewdate'],
495
- },
496
- [SortField.dateadded]: {
497
- field: SortField.dateadded,
498
- defaultSortDirection: 'desc',
499
- canSetDirection: true,
500
- shownInSortBar: true,
501
- handledBySearchService: true,
502
- searchServiceKey: 'addeddate',
503
- displayName: 'Date added',
504
- urlNames: ['addeddate'],
505
- },
506
- [SortField.datefavorited]: {
507
- field: SortField.datefavorited,
508
- defaultSortDirection: 'desc',
509
- canSetDirection: false,
510
- shownInSortBar: true, // But only when viewing fav-* collections
511
- handledBySearchService: false,
512
- searchServiceKey: 'favoritedate',
513
- displayName: 'Date favorited',
514
- urlNames: ['favoritedate'],
515
- },
516
- [SortField.creator]: {
517
- field: SortField.creator,
518
- defaultSortDirection: 'asc',
519
- canSetDirection: true,
520
- shownInSortBar: true,
521
- handledBySearchService: true,
522
- searchServiceKey: 'creatorSorter',
523
- displayName: 'Creator',
524
- urlNames: ['creator', 'creatorSorter'],
525
- },
526
- };
527
-
528
- /**
529
- * Returns the SortOption corresponding to the given API sort name, or
530
- * the "unrecognized" SortOption if none matches.
531
- */
532
- export function sortOptionFromAPIString(sortName?: string | null): SortOption {
533
- return (
534
- Object.values(SORT_OPTIONS).find(opt =>
535
- opt.urlNames.some(name => sortName === name),
536
- ) ?? SORT_OPTIONS[SortField.unrecognized]
537
- );
538
- }
539
-
540
- export const defaultSortAvailability: Record<SortField, boolean> = {
541
- [SortField.relevance]: true,
542
- [SortField.weeklyview]: true,
543
- [SortField.alltimeview]: true,
544
- [SortField.title]: true,
545
- [SortField.datefavorited]: false,
546
- [SortField.date]: true,
547
- [SortField.datearchived]: true,
548
- [SortField.datereviewed]: true,
549
- [SortField.dateadded]: true,
550
- [SortField.creator]: true,
551
- [SortField.default]: false,
552
- [SortField.unrecognized]: false,
553
- };
554
-
555
- export const favoritesSortAvailability: Record<SortField, boolean> = {
556
- ...defaultSortAvailability,
557
- [SortField.datefavorited]: true,
558
- };
559
-
560
- export const tvSortAvailability: Record<SortField, boolean> = {
561
- ...defaultSortAvailability,
562
- [SortField.date]: false,
563
- [SortField.datereviewed]: false,
564
- [SortField.dateadded]: false,
565
- };
566
-
567
- export const defaultProfileElementSorts: Record<
568
- string,
569
- Exclude<SortField, SortField.default>
570
- > = {
571
- uploads: SortField.datearchived,
572
- reviews: SortField.datereviewed,
573
- collections: SortField.datearchived,
574
- web_archives: SortField.datearchived,
575
- favorites: SortField.datefavorited,
576
- };
577
-
578
- /** A union of the fields that permit prefix filtering (e.g., alphabetical filtering) */
579
- export type PrefixFilterType = 'title' | 'creator';
580
-
581
- /** A map from prefixes (e.g., initial letters) to the number of items matching that prefix */
582
- export type PrefixFilterCounts = Record<string, number>;
583
-
584
- /**
585
- * A map from prefix filter types to the corresponding aggregation keys
586
- * that are needed to fetch the filter counts from the backend.
587
- */
588
- export const prefixFilterAggregationKeys: Record<PrefixFilterType, string> = {
589
- title: 'firstTitle',
590
- creator: 'firstCreator',
591
- };
592
-
593
- /**
594
- * Different facet loading strategies that can be used with collection browser.
595
- * - `eager`: Facet data is always loaded as soon as a search is performed
596
- * - `lazy-mobile`: In the desktop layout, functions exactly as `eager`.
597
- * In the mobile layout, facet data will only be loaded once the "Filters" accordion is opened.
598
- * - `opt-in-or-login`: Same as `opt-in` for guest users not logged into an account, but same as `eager` for
599
- * any logged in user.
600
- * - `opt-in`: In the desktop layout, facet data will only be loaded after the user presses a "Load Facets" button.
601
- * In the mobile layout, functions exactly as `lazy-mobile`.
602
- * - `off`: Facet data will never be loaded, and a message will be displayed in place of facets
603
- * indicating that they are unavailable.
604
- */
605
- export type FacetLoadStrategy =
606
- | 'eager'
607
- | 'lazy-mobile'
608
- | 'opt-in-or-login'
609
- | 'opt-in'
610
- | 'off';
611
-
612
- /**
613
- * Union of the facet types that are available in the sidebar.
614
- */
615
- export type FacetOption =
616
- | 'subject'
617
- | 'lending'
618
- | 'mediatype'
619
- | 'language'
620
- | 'creator'
621
- | 'collection'
622
- | 'year'
623
- // TV-specific facet options:
624
- | 'clip_type'
625
- | 'program'
626
- | 'person'
627
- | 'sponsor';
628
-
629
- export type SelectedFacetState = 'selected' | 'hidden';
630
-
631
- export type FacetState = SelectedFacetState | 'none';
632
-
633
- export interface FacetBucket {
634
- key: string;
635
- count: number;
636
- state: FacetState;
637
- // for some facets, we augment the key with a display value
638
- displayText?: string;
639
- // for TV channel facets, we add a parenthesized secondary name
640
- extraNote?: string;
641
- }
642
-
643
- export interface FacetGroup {
644
- title: string;
645
- key: FacetOption;
646
- buckets: FacetBucket[];
647
- }
648
-
649
- /**
650
- * Information about a user interaction event on a facet.
651
- */
652
- export type FacetEventDetails = {
653
- /**
654
- * The type of facet that was interacted with (e.g., 'mediatype', 'language', ...).
655
- */
656
- facetType: FacetOption;
657
- /**
658
- * The bucket corresponding to the facet that was interacted with, including the
659
- * updated state of the facet after the interaction.
660
- */
661
- bucket: FacetBucket;
662
- /**
663
- * Whether the interaction occurred on a negative facet.
664
- */
665
- negative: boolean;
666
- };
667
-
668
- export type FacetValue = string;
669
-
670
- export type SelectedFacets = Partial<
671
- Record<FacetOption, Record<FacetValue, FacetBucket>>
672
- >;
673
-
674
- export const getDefaultSelectedFacets = (): Required<SelectedFacets> => ({
675
- subject: {},
676
- lending: {},
677
- mediatype: {},
678
- language: {},
679
- creator: {},
680
- collection: {},
681
- year: {},
682
- clip_type: {},
683
- program: {},
684
- person: {},
685
- sponsor: {},
686
- });
687
-
688
- /**
689
- * For TV search results, what types of TV clips to restrict the results to.
690
- */
691
- export type TvClipFilterType = 'commercial' | 'fact check' | 'quote';
692
-
693
- /**
694
- * Map from allowed TV filtering parameters in the URL to their corresponding filter type
695
- */
696
- export const tvClipURLParamsToFilters: Record<string, TvClipFilterType> = {
697
- only_commercials: 'commercial',
698
- only_factchecks: 'fact check',
699
- only_quotes: 'quote',
700
- };
701
-
702
- /**
703
- * Facet display order when presenting results for all search types *except* TV (see below).
704
- */
705
- export const defaultFacetDisplayOrder: FacetOption[] = [
706
- 'mediatype',
707
- // 'lending', Commenting this out removes the lending facet from the sidebar for now
708
- 'year',
709
- 'subject',
710
- 'collection',
711
- 'creator',
712
- 'language',
713
- ];
714
-
715
- /**
716
- * Specialized facet ordering when displaying TV search results
717
- */
718
- export const tvFacetDisplayOrder: FacetOption[] = [
719
- 'program',
720
- 'creator',
721
- 'year',
722
- 'subject',
723
- // 'person', Omitting the Person facet group for now, though it may be re-added later with new semantics
724
- 'language',
725
- 'clip_type',
726
- ];
727
-
728
- /**
729
- * Human-readable titles for each facet group.
730
- */
731
- export const facetTitles: Record<FacetOption, string> = {
732
- subject: 'Subject',
733
- lending: 'Availability',
734
- mediatype: 'Media Type',
735
- language: 'Language',
736
- creator: 'Creator',
737
- collection: 'Collection',
738
- year: 'Year',
739
- clip_type: 'Clip Type',
740
- program: 'Program',
741
- person: 'Person',
742
- sponsor: 'Sponsor',
743
- };
744
-
745
- /**
746
- * The default sort type to use for each facet type
747
- */
748
- export const defaultFacetSort: Record<FacetOption, AggregationSortType> = {
749
- subject: AggregationSortType.COUNT,
750
- lending: AggregationSortType.COUNT,
751
- mediatype: AggregationSortType.COUNT,
752
- language: AggregationSortType.COUNT,
753
- creator: AggregationSortType.COUNT,
754
- collection: AggregationSortType.COUNT,
755
- year: AggregationSortType.NUMERIC, // Year facets are ordered by their numeric value by default
756
- clip_type: AggregationSortType.COUNT,
757
- program: AggregationSortType.COUNT,
758
- person: AggregationSortType.COUNT,
759
- sponsor: AggregationSortType.COUNT,
760
- };
761
-
762
- /**
763
- * The default sort type to use for each facet type in TV search More... dialogs only
764
- */
765
- export const tvMoreFacetSort: Record<FacetOption, AggregationSortType> = {
766
- ...defaultFacetSort,
767
- creator: AggregationSortType.ALPHABETICAL,
768
- program: AggregationSortType.ALPHABETICAL,
769
- };
770
-
771
- /**
772
- * The sort type corresponding to facet bucket values, for each facet type
773
- * (i.e., the opposite of "sort by count" for that type).
774
- */
775
- export const valueFacetSort: Record<FacetOption, AggregationSortType> = {
776
- subject: AggregationSortType.ALPHABETICAL,
777
- lending: AggregationSortType.ALPHABETICAL,
778
- mediatype: AggregationSortType.ALPHABETICAL,
779
- language: AggregationSortType.ALPHABETICAL,
780
- creator: AggregationSortType.ALPHABETICAL,
781
- collection: AggregationSortType.ALPHABETICAL,
782
- year: AggregationSortType.NUMERIC, // Year facets' values should be compared numerically, not lexicographically (year 2001 > year 3)
783
- clip_type: AggregationSortType.ALPHABETICAL,
784
- program: AggregationSortType.ALPHABETICAL,
785
- person: AggregationSortType.ALPHABETICAL,
786
- sponsor: AggregationSortType.ALPHABETICAL,
787
- };
788
-
789
- export type LendingFacetKey =
790
- | 'is_lendable'
791
- | 'is_borrowable'
792
- | 'available_to_borrow'
793
- | 'is_browsable'
794
- | 'available_to_browse'
795
- | 'is_readable'
796
- | 'available_to_waitlist';
797
-
798
- /**
799
- * Maps valid lending keys to whether they should be visible in the facet sidebar
800
- */
801
- export const lendingFacetKeysVisibility: Record<LendingFacetKey, boolean> = {
802
- is_lendable: true,
803
- is_borrowable: false,
804
- available_to_borrow: true,
805
- is_browsable: false,
806
- available_to_browse: false,
807
- is_readable: true,
808
- available_to_waitlist: false,
809
- };
810
-
811
- /**
812
- * Most facet options allow any string as keys, while some others can only take on a
813
- * limited set of strings. This type just defines those restrictions for the specific
814
- * facet types where they apply.
815
- */
816
- export type AllowedFacetKey<T extends FacetOption> = T extends 'lending'
817
- ? LendingFacetKey
818
- : T extends 'clip_type'
819
- ? TvClipFilterType
820
- : string;
821
-
822
- /**
823
- * A type mapping FacetOptions to objects that define custom display names for some
824
- * or all of their valid keys.
825
- */
826
- export type FacetDisplayNameMap = {
827
- [K in FacetOption]?: Partial<Record<AllowedFacetKey<K>, string>>;
828
- };
829
-
830
- export const customFacetDisplayNames: FacetDisplayNameMap = {
831
- lending: {
832
- is_lendable: 'Lending Library',
833
- available_to_borrow: 'Borrow 14 Days',
834
- is_readable: 'Always Available',
835
- },
836
- clip_type: {
837
- quote: 'Quote',
838
- commercial: 'Political Ad',
839
- 'fact check': 'Fact Check',
840
- },
841
- };
842
-
843
- /**
844
- * A record of which admin-only collections should be suppressed from being displayed
845
- * as facets or in an item's list of collections.
846
- */
847
- export const suppressedCollections: Record<string, boolean> = {
848
- deemphasize: true,
849
- community: true,
850
- stream_only: true,
851
- samples_only: true,
852
- test_collection: true,
853
- printdisabled: true,
854
- 'openlibrary-ol': true,
855
- nationalemergencylibrary: true,
856
- china: true,
857
- americana: true,
858
- toronto: true,
859
- };
860
-
861
- /**
862
- * A record of manageable item
863
- */
864
- export interface ManageableItem {
865
- identifier: string;
866
- title?: string;
867
- dateStr?: string;
868
- date?: string;
869
- }
870
-
871
- /**
872
- * Possible states for whether & how the user has overridden their user preference
873
- * for blurring behavior on tiles with sensitive content.
874
- * - `no-override`: The user has not overridden their user preference, so simply
875
- * respect the preference as given.
876
- * - `on`: The user has overridden their preference and wants tile blurring enabled.
877
- * - `off`: The user has overridden their preference and wants tile blurring disabled.
878
- */
879
- export type TileBlurOverrideState = 'no-override' | 'on' | 'off';
1
+ import type { TemplateResult } from 'lit';
2
+ import { msg } from '@lit/localize';
3
+ import type { MediaType } from '@internetarchive/field-parsers';
4
+ import {
5
+ AggregationSortType,
6
+ HitType,
7
+ SearchReview,
8
+ SearchResult,
9
+ SortDirection,
10
+ } from '@internetarchive/search-service';
11
+ import { collapseRepeatedQuotes } from './utils/collapse-repeated-quotes';
12
+ import { resolveMediatype } from './utils/resolve-mediatype';
13
+
14
+ import { loginRequiredIcon } from './assets/img/icons/login-required';
15
+ import { restrictedIcon } from './assets/img/icons/restricted';
16
+
17
+ /**
18
+ * Flags that can affect the visibility of content on a tile
19
+ */
20
+ interface TileFlags {
21
+ loginRequired: boolean;
22
+ contentWarning: boolean;
23
+ }
24
+
25
+ /**
26
+ * Different types of tile overlays, corresponding to the above flags.
27
+ */
28
+ export type TileOverlayType = 'login-required' | 'content-warning';
29
+
30
+ export const TILE_OVERLAY_TEXT: Record<TileOverlayType, string> = {
31
+ 'login-required': msg('Log in to view this item'),
32
+ 'content-warning': msg('Content may be inappropriate'),
33
+ };
34
+
35
+ export const TILE_OVERLAY_ICONS: Record<TileOverlayType, TemplateResult> = {
36
+ 'login-required': loginRequiredIcon,
37
+ 'content-warning': restrictedIcon,
38
+ };
39
+
40
+ /**
41
+ * What type of request produced a given set of hits:
42
+ * - `search_query`: Hits produced by an explicit user query and/or applied filters on any page
43
+ * - `collection_members`: Hits produced for a collection page without any query or filters
44
+ * - `profile_tab`: Hits produced for a tab of the profile page without any query or filters
45
+ * - `unknown`: Hits produced via any other means
46
+ */
47
+ export type HitRequestSource =
48
+ | 'search_query'
49
+ | 'collection_members'
50
+ | 'profile_tab'
51
+ | 'unknown';
52
+
53
+ /**
54
+ * Class for converting & storing raw search results in the correct format for UI tiles.
55
+ */
56
+ export class TileModel {
57
+ /** For TV hits. List of identifiers for any commercials contained. */
58
+ adIds?: string[];
59
+
60
+ averageRating?: number;
61
+
62
+ /** For Web Archive hits on profile pages. List of capture dates for the current URL. */
63
+ captureDates?: Date[];
64
+
65
+ /** Whether this tile is currently checked for item management functions */
66
+ checked: boolean;
67
+
68
+ collectionIdentifier?: string;
69
+
70
+ collectionName?: string;
71
+
72
+ collectionFilesCount: number;
73
+
74
+ collections: string[];
75
+
76
+ collectionSize: number;
77
+
78
+ commentCount: number;
79
+
80
+ creator?: string;
81
+
82
+ creators: string[];
83
+
84
+ /** A string representation of the publication date, used strictly for passing preformatted dates to the parent */
85
+ dateStr?: string;
86
+
87
+ /** Date added to public search (software-defined) [from MD field: addeddate] */
88
+ dateAdded?: Date;
89
+
90
+ /** Date archived (software-defined) item created on archive.org [from MD field: publicdate] */
91
+ dateArchived?: Date;
92
+
93
+ /** Date work published in the world (user-defined) [from MD field: date] */
94
+ datePublished?: Date;
95
+
96
+ /** Date reviewed (user-created) most recent review [from MD field: reviewdate] */
97
+ dateReviewed?: Date;
98
+
99
+ description?: string;
100
+
101
+ /** For TV hits. List of URLs for any fact-checks of the contained clips. */
102
+ factChecks?: string[];
103
+
104
+ favCount: number;
105
+
106
+ hitRequestSource: HitRequestSource;
107
+
108
+ hitType?: HitType;
109
+
110
+ href?: string;
111
+
112
+ identifier?: string;
113
+
114
+ /** Whether this model represents a TV clip */
115
+ isClip?: boolean;
116
+
117
+ issue?: string;
118
+
119
+ itemCount: number;
120
+
121
+ mediatype: MediaType;
122
+
123
+ review?: SearchReview;
124
+
125
+ source?: string;
126
+
127
+ snippets?: string[];
128
+
129
+ subjects: string[];
130
+
131
+ thumbnailUrl?: string;
132
+
133
+ title: string;
134
+
135
+ tvClipCount?: number;
136
+
137
+ viewCount?: number;
138
+
139
+ volume?: string;
140
+
141
+ weeklyViewCount?: number;
142
+
143
+ loginRequired: boolean;
144
+
145
+ contentWarning: boolean;
146
+
147
+ constructor(
148
+ result: SearchResult,
149
+ hitRequestSource: HitRequestSource = 'unknown',
150
+ ) {
151
+ const flags = this.getFlags(result);
152
+
153
+ this.adIds = result.ad_id?.values;
154
+ this.averageRating = result.avg_rating?.value;
155
+ this.captureDates = result.capture_dates?.values;
156
+ this.checked = false;
157
+ this.collections = result.collection?.values ?? [];
158
+ this.collectionFilesCount = result.collection_files_count?.value ?? 0;
159
+ this.collectionSize = result.collection_size?.value ?? 0;
160
+ this.commentCount = result.num_reviews?.value ?? 0;
161
+ this.creator = result.creator?.value;
162
+ this.creators = result.creator?.values ?? [];
163
+ this.dateAdded = result.addeddate?.value;
164
+ this.dateArchived = result.publicdate?.value;
165
+ this.datePublished = result.date?.value;
166
+ this.dateReviewed = result.reviewdate?.value;
167
+ this.description = result.description?.values.join('\n');
168
+ this.factChecks = result.factcheck?.values;
169
+ this.favCount = result.num_favorites?.value ?? 0;
170
+ this.hitRequestSource = hitRequestSource;
171
+ this.hitType = result.rawMetadata?.hit_type;
172
+ this.href = collapseRepeatedQuotes(
173
+ result.review?.__href__ ?? result.__href__?.value,
174
+ );
175
+ this.identifier = TileModel.cleanIdentifier(result.identifier);
176
+ this.isClip = result.is_clip?.value;
177
+ this.issue = result.issue?.value;
178
+ this.itemCount = result.item_count?.value ?? 0;
179
+ this.mediatype = resolveMediatype(result);
180
+ this.review = result.review;
181
+ this.snippets = result.highlight?.values ?? [];
182
+ this.source = result.source?.value;
183
+ this.subjects = result.subject?.values ?? [];
184
+ this.thumbnailUrl = result.__img__?.value;
185
+ this.title = result.title?.value ?? '';
186
+ this.tvClipCount = result.num_clips?.value ?? 0;
187
+ this.volume = result.volume?.value;
188
+ this.viewCount = result.downloads?.value;
189
+ this.weeklyViewCount = result.week?.value;
190
+ this.loginRequired = flags.loginRequired;
191
+ this.contentWarning = flags.contentWarning;
192
+ }
193
+
194
+ /**
195
+ * Copies the contents of this TileModel onto a new instance
196
+ */
197
+ clone(): TileModel {
198
+ const cloned = new TileModel({});
199
+ cloned.adIds = this.adIds;
200
+ cloned.averageRating = this.averageRating;
201
+ cloned.captureDates = this.captureDates;
202
+ cloned.checked = this.checked;
203
+ cloned.collections = this.collections;
204
+ cloned.collectionFilesCount = this.collectionFilesCount;
205
+ cloned.collectionSize = this.collectionSize;
206
+ cloned.commentCount = this.commentCount;
207
+ cloned.creator = this.creator;
208
+ cloned.creators = this.creators;
209
+ cloned.dateStr = this.dateStr;
210
+ cloned.dateAdded = this.dateAdded;
211
+ cloned.dateArchived = this.dateArchived;
212
+ cloned.datePublished = this.datePublished;
213
+ cloned.dateReviewed = this.dateReviewed;
214
+ cloned.description = this.description;
215
+ cloned.factChecks = this.factChecks;
216
+ cloned.favCount = this.favCount;
217
+ cloned.hitRequestSource = this.hitRequestSource;
218
+ cloned.hitType = this.hitType;
219
+ cloned.href = this.href;
220
+ cloned.identifier = this.identifier;
221
+ cloned.isClip = this.isClip;
222
+ cloned.issue = this.issue;
223
+ cloned.itemCount = this.itemCount;
224
+ cloned.mediatype = this.mediatype;
225
+ cloned.snippets = this.snippets;
226
+ cloned.source = this.source;
227
+ cloned.subjects = this.subjects;
228
+ cloned.thumbnailUrl = this.thumbnailUrl;
229
+ cloned.title = this.title;
230
+ cloned.tvClipCount = this.tvClipCount;
231
+ cloned.volume = this.volume;
232
+ cloned.viewCount = this.viewCount;
233
+ cloned.weeklyViewCount = this.weeklyViewCount;
234
+ cloned.loginRequired = this.loginRequired;
235
+ cloned.contentWarning = this.contentWarning;
236
+ return cloned;
237
+ }
238
+
239
+ /**
240
+ * Whether this model represents the result of a TV search query.
241
+ */
242
+ get isTvSearchResult(): boolean {
243
+ return (
244
+ this.hitType === 'tv_clip' && this.hitRequestSource === 'search_query'
245
+ );
246
+ }
247
+
248
+ /**
249
+ * Determines the appropriate tile flags for the given search result
250
+ * (login required and/or content warning)
251
+ */
252
+ private getFlags(result: SearchResult): TileFlags {
253
+ const flags: TileFlags = {
254
+ loginRequired: false,
255
+ contentWarning: false,
256
+ };
257
+
258
+ // Check if item and item in "modifying" collection, setting above flags
259
+ if (
260
+ result.collection?.values.length &&
261
+ result.mediatype?.value !== 'collection'
262
+ ) {
263
+ for (const collection of result.collection?.values ?? []) {
264
+ if (collection === 'loggedin') {
265
+ flags.loginRequired = true;
266
+ if (flags.contentWarning) break;
267
+ }
268
+ if (collection === 'no-preview') {
269
+ flags.contentWarning = true;
270
+ if (flags.loginRequired) break;
271
+ }
272
+ }
273
+ }
274
+
275
+ return flags;
276
+ }
277
+
278
+ private static cleanIdentifier(
279
+ identifier: string | undefined,
280
+ ): string | undefined {
281
+ // Some identifiers (e.g., from Whisper) represent documents rather than items, and
282
+ // are suffixed with values that need to be stripped. Those values are separated
283
+ // from the item identifier itself with '|'.
284
+ const barIndex = identifier?.indexOf('|') ?? -1;
285
+ const cleaned = barIndex > 0 ? identifier?.slice(0, barIndex) : identifier;
286
+ return cleaned;
287
+ }
288
+ }
289
+
290
+ export type RequestKind = 'full' | 'hits' | 'aggregations';
291
+
292
+ export type CollectionDisplayMode = 'grid' | 'list-compact' | 'list-detail';
293
+
294
+ export type TileDisplayMode =
295
+ | 'grid'
296
+ | 'list-compact'
297
+ | 'list-detail'
298
+ | 'list-header';
299
+
300
+ /**
301
+ * This is mainly used to set the cookies for the collection display mode.
302
+ *
303
+ * It allows the user to set different modes for different contexts (collection page, search page, profile page etc).
304
+ */
305
+ export type CollectionBrowserContext = 'collection' | 'search' | 'profile';
306
+
307
+ /**
308
+ * The sort fields shown in the sort filter bar
309
+ */
310
+ export enum SortField {
311
+ 'default' = 'default',
312
+ 'unrecognized' = 'unrecognized',
313
+ 'relevance' = 'relevance',
314
+ 'alltimeview' = 'alltimeview',
315
+ 'weeklyview' = 'weeklyview',
316
+ 'title' = 'title',
317
+ 'date' = 'date',
318
+ 'datearchived' = 'datearchived',
319
+ 'datereviewed' = 'datereviewed',
320
+ 'dateadded' = 'dateadded',
321
+ 'datefavorited' = 'datefavorited',
322
+ 'creator' = 'creator',
323
+ }
324
+
325
+ /**
326
+ * Views-related sort fields
327
+ */
328
+ export const ALL_VIEWS_SORT_FIELDS = [
329
+ SortField.weeklyview,
330
+ SortField.alltimeview,
331
+ ] as const;
332
+ export type ViewsSortField = (typeof ALL_VIEWS_SORT_FIELDS)[number];
333
+
334
+ /**
335
+ * Date-related sort fields
336
+ */
337
+ export const ALL_DATE_SORT_FIELDS = [
338
+ SortField.datefavorited,
339
+ SortField.date,
340
+ SortField.datearchived,
341
+ SortField.datereviewed,
342
+ SortField.dateadded,
343
+ ] as const;
344
+ export type DateSortField = (typeof ALL_DATE_SORT_FIELDS)[number];
345
+
346
+ export interface SortOption {
347
+ /**
348
+ * The SortField enum member corresponding to this option.
349
+ */
350
+ field: SortField;
351
+
352
+ /**
353
+ * The default sort direction to apply when this sort option is first selected.
354
+ */
355
+ defaultSortDirection: SortDirection | null;
356
+
357
+ /**
358
+ * Whether this sort option allows its sort direction to be changed from the default.
359
+ */
360
+ canSetDirection: boolean;
361
+
362
+ /**
363
+ * Whether this sort option may appear in the sort bar.
364
+ */
365
+ shownInSortBar: boolean;
366
+
367
+ /**
368
+ * Whether this sort option should be saved to the URL.
369
+ * If false, then no `sort` param will be added to the URL when this sort option
370
+ * is selected.
371
+ */
372
+ shownInURL: boolean;
373
+
374
+ /**
375
+ * Whether this sort option is passed to the search service.
376
+ * If false, then no sort param will be passed to the search service at all when
377
+ * this sort option is selected.
378
+ */
379
+ handledBySearchService: boolean;
380
+
381
+ /**
382
+ * The string identifying this sort field to the search service & backend API.
383
+ */
384
+ searchServiceKey?: string;
385
+
386
+ /**
387
+ * The human-readable name to use for this option in the sort bar (if applicable).
388
+ */
389
+ displayName: string;
390
+
391
+ /**
392
+ * A list of URL param keys that should be mapped to this sort option.
393
+ * E.g., both `title` and `titleSorter` in the URL map to the `SortField.title` option.
394
+ */
395
+ urlNames: (string | null | undefined)[];
396
+ }
397
+
398
+ export const SORT_OPTIONS: Record<SortField, SortOption> = {
399
+ // Default sort is the case where the user has not specified a sort option via the sort bar or URL.
400
+ // In these cases, we defer to whatever sort the backend chooses.
401
+ // For the search page, the default is always relevance sort.
402
+ // For collection pages _without a query_, the default is usually weekly views, but this can be
403
+ // overridden by the collection's `sort-by` metadata entry. If a query _is_ specified, then the
404
+ // default is again relevance sort.
405
+ // For fav-* collections only, the default is instead sorting by date favorited.
406
+ [SortField.default]: {
407
+ field: SortField.default,
408
+ defaultSortDirection: null,
409
+ canSetDirection: false,
410
+ shownInSortBar: false,
411
+ shownInURL: false,
412
+ handledBySearchService: false, // We rely on the PPS default sort handling in these cases
413
+ displayName: '',
414
+ urlNames: ['', null, undefined], // Empty or nullish sort params result in default sorting
415
+ },
416
+ // Unrecognized sort is the case where the user has specified a sort in the URL, but it is not
417
+ // one of the options listed in this map. We still want these unrecognized sorts to be applied
418
+ // when searching, but they are not displayed in the sort bar and we do not actively manage
419
+ // their URL param beyond flipping the direction as needed.
420
+ [SortField.unrecognized]: {
421
+ field: SortField.unrecognized,
422
+ defaultSortDirection: null,
423
+ canSetDirection: true,
424
+ shownInSortBar: false,
425
+ shownInURL: false,
426
+ handledBySearchService: true, // The unrecognized sort param is passed along as-is
427
+ displayName: '',
428
+ urlNames: [],
429
+ },
430
+ // Relevance sort is unique in that it does not produce a URL param when it is set.
431
+ // It is only available when there is a user-specified query that relevancy can be scored against.
432
+ // Therefore, it does not appear as a sort bar option when browsing a collection with no query set.
433
+ [SortField.relevance]: {
434
+ field: SortField.relevance,
435
+ defaultSortDirection: null,
436
+ canSetDirection: false,
437
+ shownInSortBar: true,
438
+ shownInURL: false,
439
+ handledBySearchService: false,
440
+ displayName: 'Relevance',
441
+ urlNames: ['_score'],
442
+ },
443
+ [SortField.alltimeview]: {
444
+ field: SortField.alltimeview,
445
+ defaultSortDirection: 'desc',
446
+ canSetDirection: true,
447
+ shownInSortBar: true,
448
+ shownInURL: true,
449
+ handledBySearchService: true,
450
+ searchServiceKey: 'downloads',
451
+ displayName: 'All-time views',
452
+ urlNames: ['downloads'],
453
+ },
454
+ [SortField.weeklyview]: {
455
+ field: SortField.weeklyview,
456
+ defaultSortDirection: 'desc',
457
+ canSetDirection: true,
458
+ shownInSortBar: true,
459
+ shownInURL: true,
460
+ handledBySearchService: true,
461
+ searchServiceKey: 'week',
462
+ displayName: 'Weekly views',
463
+ urlNames: ['week'],
464
+ },
465
+ [SortField.title]: {
466
+ field: SortField.title,
467
+ defaultSortDirection: 'asc',
468
+ canSetDirection: true,
469
+ shownInSortBar: true,
470
+ shownInURL: true,
471
+ handledBySearchService: true,
472
+ searchServiceKey: 'titleSorter',
473
+ displayName: 'Title',
474
+ urlNames: ['title', 'titleSorter'],
475
+ },
476
+ [SortField.date]: {
477
+ field: SortField.date,
478
+ defaultSortDirection: 'desc',
479
+ canSetDirection: true,
480
+ shownInSortBar: true,
481
+ shownInURL: true,
482
+ handledBySearchService: true,
483
+ searchServiceKey: 'date',
484
+ displayName: 'Date published',
485
+ urlNames: ['date'],
486
+ },
487
+ [SortField.datearchived]: {
488
+ field: SortField.datearchived,
489
+ defaultSortDirection: 'desc',
490
+ canSetDirection: true,
491
+ shownInSortBar: true,
492
+ shownInURL: true,
493
+ handledBySearchService: true,
494
+ searchServiceKey: 'publicdate',
495
+ displayName: 'Date archived',
496
+ urlNames: ['publicdate'],
497
+ },
498
+ [SortField.datereviewed]: {
499
+ field: SortField.datereviewed,
500
+ defaultSortDirection: 'desc',
501
+ canSetDirection: true,
502
+ shownInSortBar: true,
503
+ shownInURL: true,
504
+ handledBySearchService: true,
505
+ searchServiceKey: 'reviewdate',
506
+ displayName: 'Date reviewed',
507
+ urlNames: ['reviewdate'],
508
+ },
509
+ [SortField.dateadded]: {
510
+ field: SortField.dateadded,
511
+ defaultSortDirection: 'desc',
512
+ canSetDirection: true,
513
+ shownInSortBar: true,
514
+ shownInURL: true,
515
+ handledBySearchService: true,
516
+ searchServiceKey: 'addeddate',
517
+ displayName: 'Date added',
518
+ urlNames: ['addeddate'],
519
+ },
520
+ [SortField.datefavorited]: {
521
+ field: SortField.datefavorited,
522
+ defaultSortDirection: 'desc',
523
+ canSetDirection: false,
524
+ shownInSortBar: true, // But only when viewing fav-* collections
525
+ shownInURL: false,
526
+ handledBySearchService: false,
527
+ searchServiceKey: 'favoritedate',
528
+ displayName: 'Date favorited',
529
+ urlNames: ['favoritedate'],
530
+ },
531
+ [SortField.creator]: {
532
+ field: SortField.creator,
533
+ defaultSortDirection: 'asc',
534
+ canSetDirection: true,
535
+ shownInSortBar: true,
536
+ shownInURL: true,
537
+ handledBySearchService: true,
538
+ searchServiceKey: 'creatorSorter',
539
+ displayName: 'Creator',
540
+ urlNames: ['creator', 'creatorSorter'],
541
+ },
542
+ };
543
+
544
+ /**
545
+ * Returns the SortOption corresponding to the given API sort name, or
546
+ * the "unrecognized" SortOption if none matches.
547
+ */
548
+ export function sortOptionFromAPIString(sortName?: string | null): SortOption {
549
+ return (
550
+ Object.values(SORT_OPTIONS).find(opt =>
551
+ opt.urlNames.some(name => sortName === name),
552
+ ) ?? SORT_OPTIONS[SortField.unrecognized]
553
+ );
554
+ }
555
+
556
+ export const defaultSortAvailability: Record<SortField, boolean> = {
557
+ [SortField.relevance]: true,
558
+ [SortField.weeklyview]: true,
559
+ [SortField.alltimeview]: true,
560
+ [SortField.title]: true,
561
+ [SortField.datefavorited]: false,
562
+ [SortField.date]: true,
563
+ [SortField.datearchived]: true,
564
+ [SortField.datereviewed]: true,
565
+ [SortField.dateadded]: true,
566
+ [SortField.creator]: true,
567
+ [SortField.default]: false,
568
+ [SortField.unrecognized]: false,
569
+ };
570
+
571
+ export const favoritesSortAvailability: Record<SortField, boolean> = {
572
+ ...defaultSortAvailability,
573
+ [SortField.datefavorited]: true,
574
+ };
575
+
576
+ export const tvSortAvailability: Record<SortField, boolean> = {
577
+ ...defaultSortAvailability,
578
+ [SortField.date]: false,
579
+ [SortField.datereviewed]: false,
580
+ [SortField.dateadded]: false,
581
+ };
582
+
583
+ export const defaultProfileElementSorts: Record<
584
+ string,
585
+ Exclude<SortField, SortField.default>
586
+ > = {
587
+ uploads: SortField.datearchived,
588
+ reviews: SortField.datereviewed,
589
+ collections: SortField.datearchived,
590
+ web_archives: SortField.datearchived,
591
+ favorites: SortField.datefavorited,
592
+ };
593
+
594
+ /** A union of the fields that permit prefix filtering (e.g., alphabetical filtering) */
595
+ export type PrefixFilterType = 'title' | 'creator';
596
+
597
+ /** A map from prefixes (e.g., initial letters) to the number of items matching that prefix */
598
+ export type PrefixFilterCounts = Record<string, number>;
599
+
600
+ /**
601
+ * A map from prefix filter types to the corresponding aggregation keys
602
+ * that are needed to fetch the filter counts from the backend.
603
+ */
604
+ export const prefixFilterAggregationKeys: Record<PrefixFilterType, string> = {
605
+ title: 'firstTitle',
606
+ creator: 'firstCreator',
607
+ };
608
+
609
+ /**
610
+ * Different facet loading strategies that can be used with collection browser.
611
+ * - `eager`: Facet data is always loaded as soon as a search is performed
612
+ * - `lazy-mobile`: In the desktop layout, functions exactly as `eager`.
613
+ * In the mobile layout, facet data will only be loaded once the "Filters" accordion is opened.
614
+ * - `opt-in-or-login`: Same as `opt-in` for guest users not logged into an account, but same as `eager` for
615
+ * any logged in user.
616
+ * - `opt-in`: In the desktop layout, facet data will only be loaded after the user presses a "Load Facets" button.
617
+ * In the mobile layout, functions exactly as `lazy-mobile`.
618
+ * - `off`: Facet data will never be loaded, and a message will be displayed in place of facets
619
+ * indicating that they are unavailable.
620
+ */
621
+ export type FacetLoadStrategy =
622
+ | 'eager'
623
+ | 'lazy-mobile'
624
+ | 'opt-in-or-login'
625
+ | 'opt-in'
626
+ | 'off';
627
+
628
+ /**
629
+ * Union of the facet types that are available in the sidebar.
630
+ */
631
+ export type FacetOption =
632
+ | 'subject'
633
+ | 'lending'
634
+ | 'mediatype'
635
+ | 'language'
636
+ | 'creator'
637
+ | 'collection'
638
+ | 'year'
639
+ // TV-specific facet options:
640
+ | 'clip_type'
641
+ | 'program'
642
+ | 'person'
643
+ | 'sponsor';
644
+
645
+ export type SelectedFacetState = 'selected' | 'hidden';
646
+
647
+ export type FacetState = SelectedFacetState | 'none';
648
+
649
+ export interface FacetBucket {
650
+ key: string;
651
+ count: number;
652
+ state: FacetState;
653
+ // for some facets, we augment the key with a display value
654
+ displayText?: string;
655
+ // for TV channel facets, we add a parenthesized secondary name
656
+ extraNote?: string;
657
+ }
658
+
659
+ export interface FacetGroup {
660
+ title: string;
661
+ key: FacetOption;
662
+ buckets: FacetBucket[];
663
+ }
664
+
665
+ /**
666
+ * Information about a user interaction event on a facet.
667
+ */
668
+ export type FacetEventDetails = {
669
+ /**
670
+ * The type of facet that was interacted with (e.g., 'mediatype', 'language', ...).
671
+ */
672
+ facetType: FacetOption;
673
+ /**
674
+ * The bucket corresponding to the facet that was interacted with, including the
675
+ * updated state of the facet after the interaction.
676
+ */
677
+ bucket: FacetBucket;
678
+ /**
679
+ * Whether the interaction occurred on a negative facet.
680
+ */
681
+ negative: boolean;
682
+ };
683
+
684
+ export type FacetValue = string;
685
+
686
+ export type SelectedFacets = Partial<
687
+ Record<FacetOption, Record<FacetValue, FacetBucket>>
688
+ >;
689
+
690
+ export const getDefaultSelectedFacets = (): Required<SelectedFacets> => ({
691
+ subject: {},
692
+ lending: {},
693
+ mediatype: {},
694
+ language: {},
695
+ creator: {},
696
+ collection: {},
697
+ year: {},
698
+ clip_type: {},
699
+ program: {},
700
+ person: {},
701
+ sponsor: {},
702
+ });
703
+
704
+ /**
705
+ * For TV search results, what types of TV clips to restrict the results to.
706
+ */
707
+ export type TvClipFilterType = 'commercial' | 'fact check' | 'quote';
708
+
709
+ /**
710
+ * Map from allowed TV filtering parameters in the URL to their corresponding filter type
711
+ */
712
+ export const tvClipURLParamsToFilters: Record<string, TvClipFilterType> = {
713
+ only_commercials: 'commercial',
714
+ only_factchecks: 'fact check',
715
+ only_quotes: 'quote',
716
+ };
717
+
718
+ /**
719
+ * Facet display order when presenting results for all search types *except* TV (see below).
720
+ */
721
+ export const defaultFacetDisplayOrder: FacetOption[] = [
722
+ 'mediatype',
723
+ // 'lending', Commenting this out removes the lending facet from the sidebar for now
724
+ 'year',
725
+ 'subject',
726
+ 'collection',
727
+ 'creator',
728
+ 'language',
729
+ ];
730
+
731
+ /**
732
+ * Specialized facet ordering when displaying TV search results
733
+ */
734
+ export const tvFacetDisplayOrder: FacetOption[] = [
735
+ 'program',
736
+ 'creator',
737
+ 'year',
738
+ 'subject',
739
+ // 'person', Omitting the Person facet group for now, though it may be re-added later with new semantics
740
+ 'language',
741
+ 'clip_type',
742
+ ];
743
+
744
+ /**
745
+ * Human-readable titles for each facet group.
746
+ */
747
+ export const facetTitles: Record<FacetOption, string> = {
748
+ subject: 'Subject',
749
+ lending: 'Availability',
750
+ mediatype: 'Media Type',
751
+ language: 'Language',
752
+ creator: 'Creator',
753
+ collection: 'Collection',
754
+ year: 'Year',
755
+ clip_type: 'Clip Type',
756
+ program: 'Program',
757
+ person: 'Person',
758
+ sponsor: 'Sponsor',
759
+ };
760
+
761
+ /**
762
+ * The default sort type to use for each facet type
763
+ */
764
+ export const defaultFacetSort: Record<FacetOption, AggregationSortType> = {
765
+ subject: AggregationSortType.COUNT,
766
+ lending: AggregationSortType.COUNT,
767
+ mediatype: AggregationSortType.COUNT,
768
+ language: AggregationSortType.COUNT,
769
+ creator: AggregationSortType.COUNT,
770
+ collection: AggregationSortType.COUNT,
771
+ year: AggregationSortType.NUMERIC, // Year facets are ordered by their numeric value by default
772
+ clip_type: AggregationSortType.COUNT,
773
+ program: AggregationSortType.COUNT,
774
+ person: AggregationSortType.COUNT,
775
+ sponsor: AggregationSortType.COUNT,
776
+ };
777
+
778
+ /**
779
+ * The default sort type to use for each facet type in TV search More... dialogs only
780
+ */
781
+ export const tvMoreFacetSort: Record<FacetOption, AggregationSortType> = {
782
+ ...defaultFacetSort,
783
+ creator: AggregationSortType.ALPHABETICAL,
784
+ program: AggregationSortType.ALPHABETICAL,
785
+ };
786
+
787
+ /**
788
+ * The sort type corresponding to facet bucket values, for each facet type
789
+ * (i.e., the opposite of "sort by count" for that type).
790
+ */
791
+ export const valueFacetSort: Record<FacetOption, AggregationSortType> = {
792
+ subject: AggregationSortType.ALPHABETICAL,
793
+ lending: AggregationSortType.ALPHABETICAL,
794
+ mediatype: AggregationSortType.ALPHABETICAL,
795
+ language: AggregationSortType.ALPHABETICAL,
796
+ creator: AggregationSortType.ALPHABETICAL,
797
+ collection: AggregationSortType.ALPHABETICAL,
798
+ year: AggregationSortType.NUMERIC, // Year facets' values should be compared numerically, not lexicographically (year 2001 > year 3)
799
+ clip_type: AggregationSortType.ALPHABETICAL,
800
+ program: AggregationSortType.ALPHABETICAL,
801
+ person: AggregationSortType.ALPHABETICAL,
802
+ sponsor: AggregationSortType.ALPHABETICAL,
803
+ };
804
+
805
+ export type LendingFacetKey =
806
+ | 'is_lendable'
807
+ | 'is_borrowable'
808
+ | 'available_to_borrow'
809
+ | 'is_browsable'
810
+ | 'available_to_browse'
811
+ | 'is_readable'
812
+ | 'available_to_waitlist';
813
+
814
+ /**
815
+ * Maps valid lending keys to whether they should be visible in the facet sidebar
816
+ */
817
+ export const lendingFacetKeysVisibility: Record<LendingFacetKey, boolean> = {
818
+ is_lendable: true,
819
+ is_borrowable: false,
820
+ available_to_borrow: true,
821
+ is_browsable: false,
822
+ available_to_browse: false,
823
+ is_readable: true,
824
+ available_to_waitlist: false,
825
+ };
826
+
827
+ /**
828
+ * Most facet options allow any string as keys, while some others can only take on a
829
+ * limited set of strings. This type just defines those restrictions for the specific
830
+ * facet types where they apply.
831
+ */
832
+ export type AllowedFacetKey<T extends FacetOption> = T extends 'lending'
833
+ ? LendingFacetKey
834
+ : T extends 'clip_type'
835
+ ? TvClipFilterType
836
+ : string;
837
+
838
+ /**
839
+ * A type mapping FacetOptions to objects that define custom display names for some
840
+ * or all of their valid keys.
841
+ */
842
+ export type FacetDisplayNameMap = {
843
+ [K in FacetOption]?: Partial<Record<AllowedFacetKey<K>, string>>;
844
+ };
845
+
846
+ export const customFacetDisplayNames: FacetDisplayNameMap = {
847
+ lending: {
848
+ is_lendable: 'Lending Library',
849
+ available_to_borrow: 'Borrow 14 Days',
850
+ is_readable: 'Always Available',
851
+ },
852
+ clip_type: {
853
+ quote: 'Quote',
854
+ commercial: 'Political Ad',
855
+ 'fact check': 'Fact Check',
856
+ },
857
+ };
858
+
859
+ /**
860
+ * A record of which admin-only collections should be suppressed from being displayed
861
+ * as facets or in an item's list of collections.
862
+ */
863
+ export const suppressedCollections: Record<string, boolean> = {
864
+ deemphasize: true,
865
+ community: true,
866
+ stream_only: true,
867
+ samples_only: true,
868
+ test_collection: true,
869
+ printdisabled: true,
870
+ 'openlibrary-ol': true,
871
+ nationalemergencylibrary: true,
872
+ china: true,
873
+ americana: true,
874
+ toronto: true,
875
+ };
876
+
877
+ /**
878
+ * A record of manageable item
879
+ */
880
+ export interface ManageableItem {
881
+ identifier: string;
882
+ title?: string;
883
+ dateStr?: string;
884
+ date?: string;
885
+ }
886
+
887
+ /**
888
+ * Possible states for whether & how the user has overridden their user preference
889
+ * for blurring behavior on tiles with sensitive content.
890
+ * - `no-override`: The user has not overridden their user preference, so simply
891
+ * respect the preference as given.
892
+ * - `on`: The user has overridden their preference and wants tile blurring enabled.
893
+ * - `off`: The user has overridden their preference and wants tile blurring disabled.
894
+ */
895
+ export type TileBlurOverrideState = 'no-override' | 'on' | 'off';