@internetarchive/collection-browser 3.5.2-alpha-webdev8160.0 → 3.5.2-alpha-webdev8093.0

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 (58) hide show
  1. package/dist/src/app-root.js +606 -606
  2. package/dist/src/app-root.js.map +1 -1
  3. package/dist/src/collection-browser.js +764 -764
  4. package/dist/src/collection-browser.js.map +1 -1
  5. package/dist/src/collection-facets/facet-row.js +140 -140
  6. package/dist/src/collection-facets/facet-row.js.map +1 -1
  7. package/dist/src/collection-facets/facets-template.js +23 -23
  8. package/dist/src/collection-facets/facets-template.js.map +1 -1
  9. package/dist/src/collection-facets/more-facets-content.d.ts +1 -0
  10. package/dist/src/collection-facets/more-facets-content.js +126 -127
  11. package/dist/src/collection-facets/more-facets-content.js.map +1 -1
  12. package/dist/src/collection-facets.js +267 -267
  13. package/dist/src/collection-facets.js.map +1 -1
  14. package/dist/src/data-source/collection-browser-data-source-interface.js.map +1 -1
  15. package/dist/src/data-source/collection-browser-data-source.js.map +1 -1
  16. package/dist/src/data-source/collection-browser-query-state.js.map +1 -1
  17. package/dist/src/data-source/models.js.map +1 -1
  18. package/dist/src/models.d.ts +0 -4
  19. package/dist/src/models.js +0 -8
  20. package/dist/src/models.js.map +1 -1
  21. package/dist/src/restoration-state-handler.js.map +1 -1
  22. package/dist/src/tiles/grid/collection-tile.js +77 -77
  23. package/dist/src/tiles/grid/collection-tile.js.map +1 -1
  24. package/dist/src/tiles/grid/item-tile.js +137 -137
  25. package/dist/src/tiles/grid/item-tile.js.map +1 -1
  26. package/dist/src/tiles/hover/hover-pane-controller.d.ts +8 -0
  27. package/dist/src/tiles/hover/hover-pane-controller.js +13 -1
  28. package/dist/src/tiles/hover/hover-pane-controller.js.map +1 -1
  29. package/dist/src/tiles/models.js.map +1 -1
  30. package/dist/src/tiles/tile-dispatcher.js +215 -215
  31. package/dist/src/tiles/tile-dispatcher.js.map +1 -1
  32. package/dist/test/collection-browser.test.js +189 -189
  33. package/dist/test/collection-browser.test.js.map +1 -1
  34. package/dist/test/collection-facets/more-facets-content.test.js +28 -28
  35. package/dist/test/collection-facets/more-facets-content.test.js.map +1 -1
  36. package/dist/test/restoration-state-handler.test.js.map +1 -1
  37. package/package.json +1 -1
  38. package/src/app-root.ts +1140 -1140
  39. package/src/collection-browser.ts +3075 -3075
  40. package/src/collection-facets/facet-row.ts +299 -299
  41. package/src/collection-facets/facets-template.ts +83 -83
  42. package/src/collection-facets/more-facets-content.ts +642 -644
  43. package/src/collection-facets.ts +1010 -1010
  44. package/src/data-source/collection-browser-data-source-interface.ts +345 -345
  45. package/src/data-source/collection-browser-data-source.ts +1441 -1441
  46. package/src/data-source/collection-browser-query-state.ts +59 -59
  47. package/src/data-source/models.ts +56 -56
  48. package/src/models.ts +0 -9
  49. package/src/restoration-state-handler.ts +546 -546
  50. package/src/tiles/grid/collection-tile.ts +163 -163
  51. package/src/tiles/grid/item-tile.ts +340 -340
  52. package/src/tiles/hover/hover-pane-controller.ts +15 -1
  53. package/src/tiles/models.ts +1 -1
  54. package/src/tiles/tile-dispatcher.ts +517 -517
  55. package/test/collection-browser.test.ts +2413 -2413
  56. package/test/collection-facets/more-facets-content.test.ts +231 -231
  57. package/test/restoration-state-handler.test.ts +480 -480
  58. package/vite.config.ts +29 -29
@@ -1,1441 +1,1441 @@
1
- import type { ReactiveControllerHost } from 'lit';
2
- import {
3
- AccountExtraInfo,
4
- Aggregation,
5
- Bucket,
6
- CollectionExtraInfo,
7
- FilterConstraint,
8
- FilterMap,
9
- FilterMapBuilder,
10
- PageElementMap,
11
- SearchParams,
12
- SearchResponseSessionContext,
13
- SearchResult,
14
- SearchType,
15
- } from '@internetarchive/search-service';
16
- import {
17
- prefixFilterAggregationKeys,
18
- type FacetBucket,
19
- type PrefixFilterType,
20
- TileModel,
21
- PrefixFilterCounts,
22
- RequestKind,
23
- SortField,
24
- SORT_OPTIONS,
25
- HitRequestSource,
26
- } from '../models';
27
- import {
28
- FACETLESS_PAGE_ELEMENTS,
29
- TVChannelMaps,
30
- type PageSpecifierParams,
31
- } from './models';
32
- import type { CollectionBrowserDataSourceInterface } from './collection-browser-data-source-interface';
33
- import type { CollectionBrowserSearchInterface } from './collection-browser-query-state';
34
- import { sha1 } from '../utils/sha1';
35
- import { log } from '../utils/log';
36
- import { mergeSelectedFacets } from '../utils/facet-utils';
37
-
38
- export class CollectionBrowserDataSource
39
- implements CollectionBrowserDataSourceInterface
40
- {
41
- /**
42
- * All pages of tile models that have been fetched so far, indexed by their page
43
- * number (with the first being page 1).
44
- */
45
- private pages: Record<string, TileModel[]> = {};
46
-
47
- /**
48
- * Tile offset to apply when looking up tiles in the pages, in order to maintain
49
- * page alignment after tiles are removed.
50
- */
51
- private offset = 0;
52
-
53
- /**
54
- * Total number of tile models stored in this data source's pages
55
- */
56
- private numTileModels = 0;
57
-
58
- /**
59
- * How many consecutive pages should be batched together on the initial page fetch.
60
- * Defaults to 2 pages.
61
- */
62
- private numInitialPages = 2;
63
-
64
- /**
65
- * A set of fetch IDs that are valid for the current query state
66
- */
67
- private fetchesInProgress = new Set<string>();
68
-
69
- /**
70
- * A record of the query key used for the last search.
71
- * If this changes, we need to load new results.
72
- */
73
- private previousQueryKey: string = '';
74
-
75
- /**
76
- * Whether the initial page of search results for the current query state
77
- * is presently being fetched.
78
- */
79
- private searchResultsLoading = false;
80
-
81
- /**
82
- * Whether the facets (aggregations) for the current query state are
83
- * presently being fetched.
84
- */
85
- private facetsLoading = false;
86
-
87
- /**
88
- * Whether the facets are actually visible -- if not, then we can delay any facet
89
- * fetches until they become visible.
90
- */
91
- private facetsReadyToLoad = false;
92
-
93
- /**
94
- * Whether further query changes should be ignored and not trigger fetches
95
- */
96
- private suppressFetches = false;
97
-
98
- /**
99
- * @inheritdoc
100
- */
101
- totalResults = 0;
102
-
103
- /**
104
- * @inheritdoc
105
- */
106
- endOfDataReached = false;
107
-
108
- /**
109
- * @inheritdoc
110
- */
111
- queryInitialized = false;
112
-
113
- /**
114
- * @inheritdoc
115
- */
116
- aggregations?: Record<string, Aggregation>;
117
-
118
- /**
119
- * @inheritdoc
120
- */
121
- histogramAggregation?: Aggregation;
122
-
123
- /**
124
- * @inheritdoc
125
- */
126
- collectionTitles = new Map<string, string>();
127
-
128
- /**
129
- * @inheritdoc
130
- */
131
- tvChannelMaps: TVChannelMaps = {};
132
-
133
- /**
134
- * @inheritdoc
135
- */
136
- tvChannelAliases = new Map<string, string>();
137
-
138
- /**
139
- * @inheritdoc
140
- */
141
- collectionExtraInfo?: CollectionExtraInfo;
142
-
143
- /**
144
- * @inheritdoc
145
- */
146
- accountExtraInfo?: AccountExtraInfo;
147
-
148
- /**
149
- * @inheritdoc
150
- */
151
- sessionContext?: SearchResponseSessionContext;
152
-
153
- /**
154
- * @inheritdoc
155
- */
156
- pageElements?: PageElementMap;
157
-
158
- /**
159
- * @inheritdoc
160
- */
161
- parentCollections?: string[] = [];
162
-
163
- /**
164
- * @inheritdoc
165
- */
166
- prefixFilterCountMap: Partial<Record<PrefixFilterType, PrefixFilterCounts>> =
167
- {};
168
-
169
- /**
170
- * @inheritdoc
171
- */
172
- queryErrorMessage?: string;
173
-
174
- /**
175
- * Internal property to store the promise for any current TV channel maps fetch.
176
- */
177
- private _tvMapsPromise?: Promise<TVChannelMaps>;
178
-
179
- /**
180
- * Internal property to store the private value backing the `initialSearchComplete` getter.
181
- */
182
- private _initialSearchCompletePromise: Promise<boolean> =
183
- Promise.resolve(true);
184
-
185
- /**
186
- * @inheritdoc
187
- */
188
- get initialSearchComplete(): Promise<boolean> {
189
- return this._initialSearchCompletePromise;
190
- }
191
-
192
- constructor(
193
- /** The host element to which this controller should attach listeners */
194
- private readonly host: ReactiveControllerHost &
195
- CollectionBrowserSearchInterface,
196
- /** Default size of result pages */
197
- private pageSize: number = 50,
198
- ) {
199
- // Just setting some property values
200
- }
201
-
202
- hostConnected(): void {
203
- this.setSearchResultsLoading(this.searchResultsLoading);
204
- this.setFacetsLoading(this.facetsLoading);
205
- }
206
-
207
- hostUpdate(): void {
208
- // This reactive controller hook is run whenever the host component (collection-browser) performs an update.
209
- // We check whether the host's state has changed in a way which should trigger a reset & new results fetch.
210
-
211
- // Only the currently-installed data source should react to the update
212
- if (!this.activeOnHost) return;
213
-
214
- // Copy loading states onto the host
215
- this.setSearchResultsLoading(this.searchResultsLoading);
216
- this.setFacetsLoading(this.facetsLoading);
217
-
218
- // Can't perform searches without a search service
219
- if (!this.host.searchService) return;
220
-
221
- // We should only reset if part of the full query state has changed
222
- const queryKeyChanged = this.pageFetchQueryKey !== this.previousQueryKey;
223
- if (!queryKeyChanged) return;
224
-
225
- // We should only reset if either:
226
- // (a) our state permits a valid search, or
227
- // (b) we have a blank query that we're showing a placeholder/message for
228
- const queryIsEmpty = !this.host.baseQuery;
229
- if (!(this.canPerformSearch || queryIsEmpty)) return;
230
-
231
- if (this.activeOnHost) this.host.emitQueryStateChanged();
232
- this.handleQueryChange();
233
- }
234
-
235
- /**
236
- * Returns whether this data source is the one currently installed on the host component.
237
- */
238
- private get activeOnHost(): boolean {
239
- return this.host.dataSource === this;
240
- }
241
-
242
- /**
243
- * @inheritdoc
244
- */
245
- get size(): number {
246
- return this.numTileModels;
247
- }
248
-
249
- /**
250
- * @inheritdoc
251
- */
252
- reset(): void {
253
- log('Resetting CB data source');
254
- this.pages = {};
255
- this.aggregations = {};
256
- this.histogramAggregation = undefined;
257
- this.pageElements = undefined;
258
- this.parentCollections = [];
259
- this.previousQueryKey = '';
260
- this.queryErrorMessage = undefined;
261
-
262
- this.offset = 0;
263
- this.numTileModels = 0;
264
- this.endOfDataReached = false;
265
- this.queryInitialized = false;
266
- this.facetsLoading = false;
267
-
268
- // Invalidate any fetches in progress
269
- this.fetchesInProgress.clear();
270
-
271
- this.setTotalResultCount(0);
272
- this.requestHostUpdate();
273
- }
274
-
275
- /**
276
- * @inheritdoc
277
- */
278
- resetPages(): void {
279
- if (Object.keys(this.pages).length < this.host.maxPagesToManage) {
280
- this.pages = {};
281
-
282
- // Invalidate any page fetches in progress (keep facet fetches)
283
- this.fetchesInProgress.forEach(key => {
284
- if (!key.startsWith('facets-')) this.fetchesInProgress.delete(key);
285
- });
286
- this.requestHostUpdate();
287
- }
288
- }
289
-
290
- /**
291
- * @inheritdoc
292
- */
293
- addPage(pageNum: number, pageTiles: TileModel[]): void {
294
- this.pages[pageNum] = pageTiles;
295
- this.numTileModels += pageTiles.length;
296
- this.requestHostUpdate();
297
- }
298
-
299
- /**
300
- * @inheritdoc
301
- */
302
- addMultiplePages(firstPageNum: number, tiles: TileModel[]): void {
303
- const numPages = Math.ceil(tiles.length / this.pageSize);
304
- for (let i = 0; i < numPages; i += 1) {
305
- const pageStartIndex = this.pageSize * i;
306
- this.addPage(
307
- firstPageNum + i,
308
- tiles.slice(pageStartIndex, pageStartIndex + this.pageSize),
309
- );
310
- }
311
-
312
- const visiblePages = this.host.currentVisiblePageNumbers;
313
- const needsReload = visiblePages.some(
314
- page => page >= firstPageNum && page <= firstPageNum + numPages,
315
- );
316
- if (needsReload) {
317
- this.refreshVisibleResults();
318
- }
319
- }
320
-
321
- /**
322
- * @inheritdoc
323
- */
324
- getPage(pageNum: number): TileModel[] {
325
- return this.pages[pageNum];
326
- }
327
-
328
- /**
329
- * @inheritdoc
330
- */
331
- getAllPages(): Record<string, TileModel[]> {
332
- return this.pages;
333
- }
334
-
335
- /**
336
- * @inheritdoc
337
- */
338
- hasPage(pageNum: number): boolean {
339
- return !!this.pages[pageNum];
340
- }
341
-
342
- /**
343
- * @inheritdoc
344
- */
345
- getTileModelAt(index: number): TileModel | undefined {
346
- const offsetIndex = index + this.offset;
347
- const expectedPageNum = Math.floor(offsetIndex / this.pageSize) + 1;
348
- const expectedIndexOnPage = offsetIndex % this.pageSize;
349
-
350
- let page = 1;
351
- let tilesSeen = 0;
352
- while (tilesSeen <= offsetIndex) {
353
- if (!this.pages[page]) {
354
- // If we encounter a missing page, either we're past all the results or the page data is sparse.
355
- // So just return the tile at the expected position if it exists.
356
- return this.pages[expectedPageNum]?.[expectedIndexOnPage];
357
- }
358
-
359
- if (tilesSeen + this.pages[page].length > offsetIndex) {
360
- return this.pages[page][offsetIndex - tilesSeen];
361
- }
362
-
363
- tilesSeen += this.pages[page].length;
364
- page += 1;
365
- }
366
-
367
- return this.pages[expectedPageNum]?.[expectedIndexOnPage];
368
- }
369
-
370
- /**
371
- * @inheritdoc
372
- */
373
- indexOf(tile: TileModel): number {
374
- return Object.values(this.pages).flat().indexOf(tile) - this.offset;
375
- }
376
-
377
- /**
378
- * @inheritdoc
379
- */
380
- getPageSize(): number {
381
- return this.pageSize;
382
- }
383
-
384
- /**
385
- * @inheritdoc
386
- */
387
- setPageSize(pageSize: number): void {
388
- this.reset();
389
- this.pageSize = pageSize;
390
- }
391
-
392
- /**
393
- * @inheritdoc
394
- */
395
- setNumInitialPages(numPages: number): void {
396
- this.numInitialPages = numPages;
397
- }
398
-
399
- /**
400
- * @inheritdoc
401
- */
402
- setTotalResultCount(count: number): void {
403
- this.totalResults = count;
404
- if (this.activeOnHost) {
405
- this.host.setTotalResultCount(count);
406
- }
407
- }
408
-
409
- /**
410
- * @inheritdoc
411
- */
412
- setFetchesSuppressed(suppressed: boolean): void {
413
- this.suppressFetches = suppressed;
414
- }
415
-
416
- /**
417
- * @inheritdoc
418
- */
419
- setEndOfDataReached(reached: boolean): void {
420
- this.endOfDataReached = reached;
421
- }
422
-
423
- /**
424
- * @inheritdoc
425
- */
426
- async handleQueryChange(): Promise<void> {
427
- // Don't react to the change if fetches are suppressed for this data source
428
- if (this.suppressFetches) return;
429
-
430
- this.reset();
431
-
432
- // Reset the `initialSearchComplete` promise with a new value for the imminent search
433
- let initialSearchCompleteResolver: (value: boolean) => void;
434
- this._initialSearchCompletePromise = new Promise(res => {
435
- initialSearchCompleteResolver = res;
436
- });
437
-
438
- // Fire the initial page & facet requests
439
- this.queryInitialized = true;
440
- await Promise.all([
441
- this.doInitialPageFetch(),
442
- this.canFetchFacets ? this.fetchFacets() : null,
443
- ]);
444
-
445
- // Resolve the `initialSearchComplete` promise for this search
446
- initialSearchCompleteResolver!(true);
447
- }
448
-
449
- /**
450
- * @inheritdoc
451
- */
452
- async handleFacetReadinessChange(ready: boolean): Promise<void> {
453
- const facetsBecameReady = !this.facetsReadyToLoad && ready;
454
- this.facetsReadyToLoad = ready;
455
-
456
- const needsFetch = facetsBecameReady && this.canFetchFacets;
457
- if (needsFetch) {
458
- this.fetchFacets();
459
- }
460
- }
461
-
462
- /**
463
- * Whether the data source & its host are in a state where a facet request should be fired.
464
- * (i.e., they aren't suppressed or already loading, etc.)
465
- */
466
- private get canFetchFacets(): boolean {
467
- // Don't fetch facets if they are suppressed entirely or not required for the current profile page element
468
- if (this.host.facetLoadStrategy === 'off') return false;
469
- if (FACETLESS_PAGE_ELEMENTS.includes(this.host.profileElement!))
470
- return false;
471
-
472
- // If facets are to be lazy-loaded, don't fetch them if they are not going to be visible anyway
473
- // (wait until they become visible instead)
474
- if (this.host.facetLoadStrategy !== 'eager' && !this.facetsReadyToLoad)
475
- return false;
476
-
477
- // Don't fetch facets again if they are already fetched or pending
478
- const facetsAlreadyFetched =
479
- Object.keys(this.aggregations ?? {}).length > 0;
480
- if (this.facetsLoading || facetsAlreadyFetched) return false;
481
-
482
- return true;
483
- }
484
-
485
- /**
486
- * @inheritdoc
487
- */
488
- map(
489
- callback: (
490
- model: TileModel,
491
- index: number,
492
- array: TileModel[],
493
- ) => TileModel,
494
- ): void {
495
- if (!Object.keys(this.pages).length) return;
496
- this.pages = Object.fromEntries(
497
- Object.entries(this.pages).map(([page, tileModels]) => [
498
- page,
499
- tileModels.map((model, index, array) =>
500
- model ? callback(model, index, array) : model,
501
- ),
502
- ]),
503
- );
504
- this.requestHostUpdate();
505
- this.refreshVisibleResults();
506
- }
507
-
508
- /**
509
- * @inheritdoc
510
- */
511
- checkAllTiles = (): void => {
512
- this.map(model => {
513
- const cloned = model.clone();
514
- cloned.checked = true;
515
- return cloned;
516
- });
517
- };
518
-
519
- /**
520
- * @inheritdoc
521
- */
522
- uncheckAllTiles = (): void => {
523
- this.map(model => {
524
- const cloned = model.clone();
525
- cloned.checked = false;
526
- return cloned;
527
- });
528
- };
529
-
530
- /**
531
- * @inheritdoc
532
- */
533
- removeCheckedTiles = (): void => {
534
- // To make sure our data source remains page-aligned, we will offset our data source by
535
- // the number of removed tiles, so that we can just add the offset when the infinite
536
- // scroller queries for cell contents.
537
- // This only matters while we're still viewing the same set of results. If the user changes
538
- // their query/filters/sort, then the data source is overwritten and the offset cleared.
539
- const { checkedTileModels, uncheckedTileModels } = this;
540
- const numChecked = checkedTileModels.length;
541
- if (numChecked === 0) return;
542
- this.offset += numChecked;
543
- const newPages: typeof this.pages = {};
544
-
545
- // Which page the remaining tile models start on, post-offset
546
- let offsetPageNumber = Math.floor(this.offset / this.pageSize) + 1;
547
- let indexOnPage = this.offset % this.pageSize;
548
-
549
- // Fill the pages up to that point with empty models
550
- for (let page = 1; page <= offsetPageNumber; page += 1) {
551
- const remainingHidden = this.offset - this.pageSize * (page - 1);
552
- const offsetCellsOnPage = Math.min(this.pageSize, remainingHidden);
553
- newPages[page] = Array(offsetCellsOnPage).fill(undefined);
554
- }
555
-
556
- // Shift all the remaining tiles into their new positions in the data source
557
- for (const model of uncheckedTileModels) {
558
- if (!newPages[offsetPageNumber]) newPages[offsetPageNumber] = [];
559
- newPages[offsetPageNumber].push(model);
560
- indexOnPage += 1;
561
- if (indexOnPage >= this.pageSize) {
562
- offsetPageNumber += 1;
563
- indexOnPage = 0;
564
- }
565
- }
566
-
567
- // Swap in the new pages
568
- this.pages = newPages;
569
- this.numTileModels -= numChecked;
570
- this.totalResults -= numChecked;
571
- this.host.setTileCount(this.size);
572
- this.host.setTotalResultCount(this.totalResults);
573
- this.requestHostUpdate();
574
- this.refreshVisibleResults();
575
- };
576
-
577
- /**
578
- * @inheritdoc
579
- */
580
- get checkedTileModels(): TileModel[] {
581
- return this.getFilteredTileModels(model => model.checked);
582
- }
583
-
584
- /**
585
- * @inheritdoc
586
- */
587
- get uncheckedTileModels(): TileModel[] {
588
- return this.getFilteredTileModels(model => !model.checked);
589
- }
590
-
591
- /**
592
- * Returns a flattened, filtered array of all the tile models in the data source
593
- * for which the given predicate returns a truthy value.
594
- *
595
- * @param predicate A callback function to apply on each tile model, as with Array.filter
596
- * @returns A filtered array of tile models satisfying the predicate
597
- */
598
- private getFilteredTileModels(
599
- predicate: (model: TileModel, index: number, array: TileModel[]) => unknown,
600
- ): TileModel[] {
601
- return Object.values(this.pages)
602
- .flat()
603
- .filter((model, index, array) =>
604
- model ? predicate(model, index, array) : false,
605
- );
606
- }
607
-
608
- /**
609
- * @inheritdoc
610
- */
611
- get canPerformSearch(): boolean {
612
- if (!this.host.searchService) return false;
613
-
614
- const trimmedQuery = this.host.baseQuery?.trim();
615
- const hasNonEmptyQuery = !!trimmedQuery;
616
- const hasIdentifiers = !!this.host.identifiers?.length;
617
- const isCollectionSearch = !!this.host.withinCollection;
618
- const isProfileSearch = !!this.host.withinProfile;
619
- const hasProfileElement = !!this.host.profileElement;
620
- const isDefaultedSearch = this.host.searchType === SearchType.DEFAULT;
621
- const isMetadataSearch = this.host.searchType === SearchType.METADATA;
622
- const isTvSearch = this.host.searchType === SearchType.TV;
623
-
624
- // Metadata/tv searches within a collection are allowed to have no query.
625
- const isValidForCollectionSearch =
626
- isDefaultedSearch || isMetadataSearch || isTvSearch;
627
-
628
- // Searches within a profile page may also be performed without a query, provided the profile element is set.
629
- const isValidForProfileSearch =
630
- hasProfileElement && (isDefaultedSearch || isMetadataSearch);
631
-
632
- // Otherwise, a non-empty query must be set.
633
- return (
634
- hasNonEmptyQuery ||
635
- hasIdentifiers ||
636
- (isCollectionSearch && isValidForCollectionSearch) ||
637
- (isProfileSearch && isValidForProfileSearch)
638
- );
639
- }
640
-
641
- /**
642
- * Sets the state for whether the initial set of search results for the
643
- * current query is loading
644
- */
645
- private setSearchResultsLoading(loading: boolean): void {
646
- this.searchResultsLoading = loading;
647
- if (this.activeOnHost) {
648
- this.host.setSearchResultsLoading(loading);
649
- }
650
- }
651
-
652
- /**
653
- * Sets the state for whether the facets for a query is loading
654
- */
655
- private setFacetsLoading(loading: boolean): void {
656
- this.facetsLoading = loading;
657
- if (this.activeOnHost) {
658
- this.host.setFacetsLoading(loading);
659
- }
660
- }
661
-
662
- /**
663
- * Requests that the host perform an update, provided this data
664
- * source is actively installed on it.
665
- */
666
- private requestHostUpdate(): void {
667
- if (this.activeOnHost) {
668
- this.host.requestUpdate();
669
- }
670
- }
671
-
672
- /**
673
- * Requests that the host refresh its visible tiles, provided this
674
- * data source is actively installed on it.
675
- */
676
- private refreshVisibleResults(): void {
677
- if (this.activeOnHost) {
678
- this.host.refreshVisibleResults();
679
- }
680
- }
681
-
682
- /**
683
- * The query key is a string that uniquely identifies the current search.
684
- * It consists of:
685
- * - The current base query
686
- * - The current collection/profile target & page element
687
- * - The current search type
688
- * - Any currently-applied facets
689
- * - Any currently-applied date range
690
- * - Any currently-applied prefix filters
691
- * - The current sort options
692
- *
693
- * This lets us internally keep track of queries so we don't persist data that's
694
- * no longer relevant. Not meant to be human-readable.
695
- */
696
- get pageFetchQueryKey(): string {
697
- const profileKey = `pf;${this.host.withinProfile}--pe;${this.host.profileElement}`;
698
- const pageTarget = this.host.withinCollection ?? profileKey;
699
- const sortField = this.host.selectedSort ?? 'none';
700
- const sortDirection = this.host.sortDirection ?? 'none';
701
- return `fq:${this.fullQuery}-pt:${pageTarget}-st:${this.host.searchType}-sf:${sortField}-sd:${sortDirection}`;
702
- }
703
-
704
- /**
705
- * Similar to `pageFetchQueryKey` above, but excludes sort fields since they
706
- * are not relevant in determining aggregation queries.
707
- */
708
- get facetFetchQueryKey(): string {
709
- const profileKey = `pf;${this.host.withinProfile}--pe;${this.host.profileElement}`;
710
- const pageTarget = this.host.withinCollection ?? profileKey;
711
- return `facets-fq:${this.fullQuery}-pt:${pageTarget}-st:${this.host.searchType}`;
712
- }
713
-
714
- /**
715
- * Constructs a search service FilterMap object from the combination of
716
- * all the currently-applied filters. This includes any facets, letter
717
- * filters, and date range.
718
- */
719
- get filterMap(): FilterMap {
720
- const builder = new FilterMapBuilder();
721
-
722
- const {
723
- minSelectedDate,
724
- maxSelectedDate,
725
- selectedFacets,
726
- internalFilters,
727
- selectedTitleFilter,
728
- selectedCreatorFilter,
729
- } = this.host;
730
-
731
- const dateField = this.host.searchType === SearchType.TV ? 'date' : 'year';
732
-
733
- if (minSelectedDate) {
734
- builder.addFilter(
735
- dateField,
736
- minSelectedDate,
737
- FilterConstraint.GREATER_OR_EQUAL,
738
- );
739
- }
740
- if (maxSelectedDate) {
741
- builder.addFilter(
742
- dateField,
743
- maxSelectedDate,
744
- FilterConstraint.LESS_OR_EQUAL,
745
- );
746
- }
747
-
748
- // Add any selected facets and internal filters
749
- const combinedFilters = mergeSelectedFacets(
750
- internalFilters,
751
- selectedFacets,
752
- );
753
- if (combinedFilters) {
754
- for (const [facetName, facetValues] of Object.entries(combinedFilters)) {
755
- const { name, values } = this.prepareFacetForFetch(
756
- facetName,
757
- facetValues,
758
- );
759
- for (const [value, bucket] of Object.entries(values)) {
760
- let constraint;
761
- if (bucket.state === 'selected') {
762
- constraint = FilterConstraint.INCLUDE;
763
- } else if (bucket.state === 'hidden') {
764
- constraint = FilterConstraint.EXCLUDE;
765
- }
766
-
767
- if (constraint) {
768
- builder.addFilter(name, value, constraint);
769
- }
770
- }
771
- }
772
- }
773
-
774
- // Add any letter filters
775
- if (selectedTitleFilter) {
776
- builder.addFilter(
777
- 'firstTitle',
778
- selectedTitleFilter,
779
- FilterConstraint.INCLUDE,
780
- );
781
- }
782
- if (selectedCreatorFilter) {
783
- builder.addFilter(
784
- 'firstCreator',
785
- selectedCreatorFilter,
786
- FilterConstraint.INCLUDE,
787
- );
788
- }
789
-
790
- const filterMap = builder.build();
791
- return filterMap;
792
- }
793
-
794
- /**
795
- * Produces a compact unique ID for a search request that can help with debugging
796
- * on the backend by making related requests easier to trace through different services.
797
- * (e.g., tying the hits/aggregations requests for the same page back to a single hash).
798
- *
799
- * @param params The search service parameters for the request
800
- * @param kind The kind of request (hits-only, aggregations-only, or both)
801
- * @returns A Promise resolving to the uid to apply to the request
802
- */
803
- async requestUID(params: SearchParams, kind: RequestKind): Promise<string> {
804
- const paramsToHash = JSON.stringify({
805
- pageType: params.pageType,
806
- pageTarget: params.pageTarget,
807
- query: params.query,
808
- fields: params.fields,
809
- filters: params.filters,
810
- sort: params.sort,
811
- searchType: this.host.searchType,
812
- });
813
-
814
- const fullQueryHash = (await sha1(paramsToHash)).slice(0, 20); // First 80 bits of SHA-1 are plenty for this
815
- const sessionId = (await this.host.getSessionId()).slice(0, 20); // Likewise
816
- const page = params.page ?? 0;
817
- const kindPrefix = kind.charAt(0); // f = full, h = hits, a = aggregations
818
- const currentTime = Date.now();
819
-
820
- return `R:${fullQueryHash}-S:${sessionId}-P:${page}-K:${kindPrefix}-T:${currentTime}`;
821
- }
822
-
823
- /**
824
- * @inheritdoc
825
- */
826
- get pageSpecifierParams(): PageSpecifierParams | null {
827
- if (this.host.identifiers?.length) {
828
- return {
829
- pageType: 'client_document_fetch',
830
- };
831
- }
832
- if (this.host.withinCollection) {
833
- return {
834
- pageType: 'collection_details',
835
- pageTarget: this.host.withinCollection,
836
- };
837
- }
838
- if (this.host.withinProfile) {
839
- return {
840
- pageType: 'account_details',
841
- pageTarget: this.host.withinProfile,
842
- pageElements: this.host.profileElement
843
- ? [this.host.profileElement]
844
- : [],
845
- };
846
- }
847
- return null;
848
- }
849
-
850
- /**
851
- * The full query, including year facets and date range clauses
852
- */
853
- private get fullQuery(): string | undefined {
854
- const parts = [];
855
- const trimmedQuery = this.host.baseQuery?.trim();
856
- if (trimmedQuery) parts.push(trimmedQuery);
857
-
858
- if (this.host.identifiers) {
859
- parts.push(`identifier:(${this.host.identifiers.join(' OR ')})`);
860
- }
861
-
862
- const { facetQuery, dateRangeQueryClause, sortFilterQueries } = this;
863
- if (facetQuery) parts.push(facetQuery);
864
- if (dateRangeQueryClause) parts.push(dateRangeQueryClause);
865
- if (sortFilterQueries) parts.push(sortFilterQueries);
866
-
867
- return parts.join(' AND ').trim();
868
- }
869
-
870
- /**
871
- * Generates a query string representing the current set of applied facets
872
- *
873
- * Example: `mediatype:("collection" OR "audio" OR -"etree") AND year:("2000" OR "2001")`
874
- */
875
- private get facetQuery(): string | undefined {
876
- if (!this.host.selectedFacets) return undefined;
877
- const facetClauses = [];
878
- for (const [facetName, facetValues] of Object.entries(
879
- this.host.selectedFacets,
880
- )) {
881
- facetClauses.push(this.buildFacetClause(facetName, facetValues));
882
- }
883
- return this.joinFacetClauses(facetClauses)?.trim();
884
- }
885
-
886
- private get dateRangeQueryClause(): string | undefined {
887
- if (!this.host.minSelectedDate || !this.host.maxSelectedDate) {
888
- return undefined;
889
- }
890
-
891
- return `year:[${this.host.minSelectedDate} TO ${this.host.maxSelectedDate}]`;
892
- }
893
-
894
- private get sortFilterQueries(): string {
895
- const queries = [this.titleQuery, this.creatorQuery];
896
- return queries.filter(q => q).join(' AND ');
897
- }
898
-
899
- /**
900
- * Returns a query clause identifying the currently selected title filter,
901
- * e.g., `firstTitle:X`.
902
- */
903
- private get titleQuery(): string | undefined {
904
- return this.host.selectedTitleFilter
905
- ? `firstTitle:${this.host.selectedTitleFilter}`
906
- : undefined;
907
- }
908
-
909
- /**
910
- * Returns a query clause identifying the currently selected creator filter,
911
- * e.g., `firstCreator:X`.
912
- */
913
- private get creatorQuery(): string | undefined {
914
- return this.host.selectedCreatorFilter
915
- ? `firstCreator:${this.host.selectedCreatorFilter}`
916
- : undefined;
917
- }
918
-
919
- /**
920
- * Builds an OR-joined facet clause for the given facet name and values.
921
- *
922
- * E.g., for name `subject` and values
923
- * `{ foo: { state: 'selected' }, bar: { state: 'hidden' } }`
924
- * this will produce the clause
925
- * `subject:("foo" OR -"bar")`.
926
- *
927
- * @param facetName The facet type (e.g., 'collection')
928
- * @param facetValues The facet buckets, mapped by their keys
929
- */
930
- private buildFacetClause(
931
- facetName: string,
932
- facetValues: Record<string, FacetBucket>,
933
- ): string {
934
- const { name: facetQueryName, values } = this.prepareFacetForFetch(
935
- facetName,
936
- facetValues,
937
- );
938
- const facetEntries = Object.entries(values);
939
- if (facetEntries.length === 0) return '';
940
-
941
- const facetValuesArray: string[] = [];
942
- for (const [key, facetData] of facetEntries) {
943
- const plusMinusPrefix = facetData.state === 'hidden' ? '-' : '';
944
- facetValuesArray.push(`${plusMinusPrefix}"${key}"`);
945
- }
946
-
947
- const valueQuery = facetValuesArray.join(` OR `);
948
- return `${facetQueryName}:(${valueQuery})`;
949
- }
950
-
951
- /**
952
- * Handles some special pre-request normalization steps for certain facet types
953
- * that require them.
954
- *
955
- * @param facetName The name of the facet type (e.g., 'language')
956
- * @param facetValues An array of values for that facet type
957
- */
958
- private prepareFacetForFetch(
959
- facetName: string,
960
- facetValues: Record<string, FacetBucket>,
961
- ): { name: string; values: Record<string, FacetBucket> } {
962
- // eslint-disable-next-line prefer-const
963
- let [normalizedName, normalizedValues] = [facetName, facetValues];
964
-
965
- // The full "search engine" name of the lending field is "lending___status"
966
- if (facetName === 'lending') {
967
- normalizedName = 'lending___status';
968
- }
969
-
970
- return {
971
- name: normalizedName,
972
- values: normalizedValues,
973
- };
974
- }
975
-
976
- /**
977
- * Takes an array of facet clauses, and combines them into a
978
- * full AND-joined facet query string. Empty clauses are ignored.
979
- */
980
- private joinFacetClauses(facetClauses: string[]): string | undefined {
981
- const nonEmptyFacetClauses = facetClauses.filter(
982
- clause => clause.length > 0,
983
- );
984
- return nonEmptyFacetClauses.length > 0
985
- ? `(${nonEmptyFacetClauses.join(' AND ')})`
986
- : undefined;
987
- }
988
-
989
- /**
990
- * Fires a backend request to fetch a set of aggregations (representing UI facets) for
991
- * the current search state.
992
- */
993
- private async fetchFacets(): Promise<void> {
994
- const trimmedQuery = this.host.baseQuery?.trim();
995
- if (!this.canPerformSearch) return;
996
-
997
- const { facetFetchQueryKey } = this;
998
- if (this.fetchesInProgress.has(facetFetchQueryKey)) return;
999
- this.fetchesInProgress.add(facetFetchQueryKey);
1000
-
1001
- this.setFacetsLoading(true);
1002
-
1003
- const sortParams = this.host.sortParam ? [this.host.sortParam] : [];
1004
- const params: SearchParams = {
1005
- ...this.pageSpecifierParams,
1006
- query: trimmedQuery || '',
1007
- identifiers: this.host.identifiers,
1008
- rows: 0,
1009
- filters: this.filterMap,
1010
- // Fetch a few extra buckets beyond the 6 we show, in case some get suppressed
1011
- aggregationsSize: 10,
1012
- // Note: we don't need an aggregations param to fetch the default aggregations from the PPS.
1013
- // The default aggregations for the search_results page type should be what we need here.
1014
- };
1015
- params.uid = await this.requestUID(
1016
- { ...params, sort: sortParams },
1017
- 'aggregations',
1018
- );
1019
-
1020
- const searchResponse = await this.host.searchService?.search(
1021
- params,
1022
- this.host.searchType,
1023
- );
1024
- const success = searchResponse?.success;
1025
-
1026
- // This is checking to see if the query has changed since the data was fetched.
1027
- // If so, we just want to discard this set of aggregations because they are
1028
- // likely no longer valid for the newer query.
1029
- const queryChangedSinceFetch =
1030
- !this.fetchesInProgress.has(facetFetchQueryKey);
1031
- this.fetchesInProgress.delete(facetFetchQueryKey);
1032
- if (queryChangedSinceFetch) return;
1033
-
1034
- if (!success) {
1035
- const errorMsg = searchResponse?.error?.message;
1036
- const detailMsg = searchResponse?.error?.details?.message;
1037
-
1038
- if (!errorMsg && !detailMsg) {
1039
- // @ts-expect-error: Property 'Sentry' does not exist on type 'Window & typeof globalThis'
1040
- window?.Sentry?.captureMessage?.(
1041
- 'Missing or malformed facet response from backend',
1042
- 'error',
1043
- );
1044
- }
1045
-
1046
- this.setFacetsLoading(false);
1047
- return;
1048
- }
1049
-
1050
- const { aggregations, collectionTitles, tvChannelAliases } =
1051
- success.response;
1052
- this.aggregations = aggregations;
1053
-
1054
- this.histogramAggregation =
1055
- this.host.searchType === SearchType.TV
1056
- ? aggregations?.date_histogram
1057
- : aggregations?.year_histogram;
1058
-
1059
- if (collectionTitles) {
1060
- for (const [id, title] of Object.entries(collectionTitles)) {
1061
- this.collectionTitles.set(id, title);
1062
- }
1063
- }
1064
- if (tvChannelAliases) {
1065
- for (const [channel, network] of Object.entries(tvChannelAliases)) {
1066
- this.tvChannelAliases.set(channel, network);
1067
- }
1068
- }
1069
-
1070
- this.setFacetsLoading(false);
1071
- this.requestHostUpdate();
1072
- }
1073
-
1074
- /**
1075
- * Performs the initial page fetch(es) for the current search state.
1076
- */
1077
- private async doInitialPageFetch(): Promise<void> {
1078
- this.setSearchResultsLoading(true);
1079
- // Try to batch 2 initial page requests when possible
1080
- await this.fetchPage(this.host.initialPageNumber, this.numInitialPages);
1081
- }
1082
-
1083
- /**
1084
- * Fetches one or more pages of results and updates the data source.
1085
- *
1086
- * @param pageNumber The page number to fetch
1087
- * @param numInitialPages If this is an initial page fetch (`pageNumber = 1`),
1088
- * specifies how many pages to batch together in one request. Ignored
1089
- * if `pageNumber != 1`, defaulting to a single page.
1090
- */
1091
- async fetchPage(pageNumber: number, numInitialPages = 1): Promise<void> {
1092
- const trimmedQuery = this.host.baseQuery?.trim();
1093
- // reset loading status
1094
- if (!this.canPerformSearch) {
1095
- this.setSearchResultsLoading(false);
1096
- return;
1097
- }
1098
-
1099
- // if we already have data, don't fetch again
1100
- if (this.hasPage(pageNumber)) return;
1101
-
1102
- if (this.endOfDataReached) return;
1103
-
1104
- // Batch multiple initial page requests together if needed (e.g., can request
1105
- // pages 1 and 2 together in a single request).
1106
- let numPages = pageNumber === 1 ? numInitialPages : 1;
1107
- const numRows = this.pageSize * numPages;
1108
-
1109
- // if a fetch is already in progress for this query and page, don't fetch again
1110
- const { pageFetchQueryKey } = this;
1111
- const currentPageKey = `${pageFetchQueryKey}-p:${pageNumber}`;
1112
- if (this.fetchesInProgress.has(currentPageKey)) return;
1113
-
1114
- for (let i = 0; i < numPages; i += 1) {
1115
- this.fetchesInProgress.add(`${pageFetchQueryKey}-p:${pageNumber + i}`);
1116
- }
1117
- this.previousQueryKey = pageFetchQueryKey;
1118
-
1119
- const { withinCollection, withinProfile } = this.host;
1120
-
1121
- let sortParams = this.host.sortParam ? [this.host.sortParam] : [];
1122
- // TODO eventually the PPS should handle these defaults natively
1123
- const isDefaultProfileSort =
1124
- withinProfile && this.host.selectedSort === SortField.default;
1125
- if (isDefaultProfileSort && this.host.defaultSortField) {
1126
- const sortOption = SORT_OPTIONS[this.host.defaultSortField];
1127
- if (sortOption.searchServiceKey) {
1128
- sortParams = [
1129
- {
1130
- field: sortOption.searchServiceKey,
1131
- direction: 'desc',
1132
- },
1133
- ];
1134
- }
1135
- }
1136
-
1137
- const params: SearchParams = {
1138
- ...this.pageSpecifierParams,
1139
- query: trimmedQuery || '',
1140
- identifiers: this.host.identifiers,
1141
- page: pageNumber,
1142
- rows: numRows,
1143
- sort: sortParams,
1144
- filters: this.filterMap,
1145
- aggregations: { omit: true },
1146
- };
1147
- params.uid = await this.requestUID(params, 'hits');
1148
-
1149
- // log('=== FIRING PAGE REQUEST ===', params);
1150
- const searchResponse = await this.host.searchService?.search(
1151
- params,
1152
- this.host.searchType,
1153
- );
1154
- // log('=== RECEIVED PAGE RESPONSE IN CB ===', searchResponse);
1155
- const success = searchResponse?.success;
1156
-
1157
- // This is checking to see if the fetch has been invalidated since it was fired off.
1158
- // If so, we just want to discard the response since it is for an obsolete query state.
1159
- if (!this.fetchesInProgress.has(currentPageKey)) return;
1160
- for (let i = 0; i < numPages; i += 1) {
1161
- this.fetchesInProgress.delete(`${pageFetchQueryKey}-p:${pageNumber + i}`);
1162
- }
1163
-
1164
- if (!success) {
1165
- const errorMsg = searchResponse?.error?.message;
1166
- const detailMsg = searchResponse?.error?.details?.message;
1167
-
1168
- this.queryErrorMessage = `${errorMsg ?? ''}${
1169
- detailMsg ? `; ${detailMsg}` : ''
1170
- }`;
1171
-
1172
- if (!this.queryErrorMessage) {
1173
- this.queryErrorMessage = 'Missing or malformed response from backend';
1174
- // @ts-expect-error: Property 'Sentry' does not exist on type 'Window & typeof globalThis'
1175
- window?.Sentry?.captureMessage?.(this.queryErrorMessage, 'error');
1176
- }
1177
-
1178
- this.setSearchResultsLoading(false);
1179
- this.requestHostUpdate();
1180
- this.host.emitSearchError();
1181
- return;
1182
- }
1183
-
1184
- this.setTotalResultCount(success.response.totalResults - this.offset);
1185
- if (this.activeOnHost && this.totalResults === 0) {
1186
- // display event to offshoot when result count is zero.
1187
- this.host.emitEmptyResults();
1188
- }
1189
-
1190
- this.sessionContext = success.sessionContext;
1191
- if (withinCollection) {
1192
- this.collectionExtraInfo = success.response.collectionExtraInfo;
1193
-
1194
- // For collections, we want the UI to respect the default sort option
1195
- // which can be specified in metadata, or otherwise assumed to be week:desc
1196
- if (this.activeOnHost) {
1197
- this.host.applyDefaultCollectionSort(this.collectionExtraInfo);
1198
- }
1199
-
1200
- if (this.collectionExtraInfo) {
1201
- this.parentCollections = [].concat(
1202
- this.collectionExtraInfo.public_metadata?.collection ?? [],
1203
- );
1204
-
1205
- // Update the TV collection status now that we know the parent collections
1206
- this.host.isTVCollection =
1207
- this.host.withinCollection?.startsWith('TV-') ||
1208
- this.host.withinCollection === 'tvnews' ||
1209
- this.host.withinCollection === 'tvarchive' ||
1210
- this.parentCollections.includes('tvnews') ||
1211
- this.parentCollections.includes('tvarchive');
1212
- }
1213
- } else if (withinProfile) {
1214
- this.accountExtraInfo = success.response.accountExtraInfo;
1215
- this.pageElements = success.response.pageElements;
1216
- }
1217
-
1218
- const { results, collectionTitles, tvChannelAliases } = success.response;
1219
- if (results && results.length > 0) {
1220
- // Load any collection titles present on the response into the cache,
1221
- // or queue up preload fetches for them if none were present.
1222
- if (collectionTitles) {
1223
- for (const [id, title] of Object.entries(collectionTitles)) {
1224
- this.collectionTitles.set(id, title);
1225
- }
1226
-
1227
- // Also add the target collection's title if available
1228
- const targetTitle = this.collectionExtraInfo?.public_metadata?.title;
1229
- if (withinCollection && targetTitle) {
1230
- this.collectionTitles.set(withinCollection, targetTitle);
1231
- }
1232
- }
1233
-
1234
- if (tvChannelAliases) {
1235
- for (const [channel, network] of Object.entries(tvChannelAliases)) {
1236
- this.tvChannelAliases.set(channel, network);
1237
- }
1238
- }
1239
-
1240
- // Update the data source for each returned page.
1241
- // For loans and web archives, we must account for receiving more pages than we asked for.
1242
- const isUnpagedElement = ['lending', 'web_archives'].includes(
1243
- this.host.profileElement!,
1244
- );
1245
- if (isUnpagedElement) {
1246
- numPages = Math.ceil(results.length / this.pageSize);
1247
- this.endOfDataReached = true;
1248
- if (this.activeOnHost) this.host.setTileCount(this.totalResults);
1249
- }
1250
-
1251
- for (let i = 0; i < numPages; i += 1) {
1252
- const pageStartIndex = this.pageSize * i;
1253
- this.addFetchedResultsToDataSource(
1254
- pageNumber + i,
1255
- results.slice(pageStartIndex, pageStartIndex + this.pageSize),
1256
- !isUnpagedElement || i === numPages - 1,
1257
- );
1258
- }
1259
- }
1260
-
1261
- // When we reach the end of the data, we can set the infinite scroller's
1262
- // item count to the real total number of results (rather than the
1263
- // temporary estimates based on pages rendered so far).
1264
- if (this.size >= this.totalResults || results.length === 0) {
1265
- this.endOfDataReached = true;
1266
- if (this.activeOnHost) this.host.setTileCount(this.size);
1267
- }
1268
-
1269
- this.setSearchResultsLoading(false);
1270
- this.requestHostUpdate();
1271
- }
1272
-
1273
- /**
1274
- * Returns the type of request that produced the current set of hits,
1275
- * based on the presence of a search query or profile/collection target
1276
- * on the host.
1277
- */
1278
- private get hitRequestSource(): HitRequestSource {
1279
- const { host } = this;
1280
- if (host.baseQuery) return 'search_query';
1281
- if (host.withinProfile) return 'profile_tab';
1282
- if (host.withinCollection) return 'collection_members';
1283
- return 'unknown';
1284
- }
1285
-
1286
- /**
1287
- * Update the datasource from the fetch response
1288
- *
1289
- * @param pageNumber
1290
- * @param results
1291
- */
1292
- private addFetchedResultsToDataSource(
1293
- pageNumber: number,
1294
- results: SearchResult[],
1295
- needsReload = true,
1296
- ): void {
1297
- const tiles: TileModel[] = [];
1298
- const requestSource = this.hitRequestSource;
1299
- results?.forEach(result => {
1300
- if (!result.identifier) return;
1301
- tiles.push(new TileModel(result, requestSource));
1302
- });
1303
-
1304
- this.addPage(pageNumber, tiles);
1305
-
1306
- if (needsReload) {
1307
- this.refreshVisibleResults();
1308
- }
1309
- }
1310
-
1311
- /**
1312
- * Fetches the aggregation buckets for the given prefix filter type.
1313
- */
1314
- private async fetchPrefixFilterBuckets(
1315
- filterType: PrefixFilterType,
1316
- ): Promise<Bucket[]> {
1317
- const trimmedQuery = this.host.baseQuery?.trim();
1318
- if (!this.canPerformSearch) return [];
1319
-
1320
- const filterAggregationKey = prefixFilterAggregationKeys[filterType];
1321
- const sortParams = this.host.sortParam ? [this.host.sortParam] : [];
1322
-
1323
- const params: SearchParams = {
1324
- ...this.pageSpecifierParams,
1325
- query: trimmedQuery || '',
1326
- identifiers: this.host.identifiers,
1327
- rows: 0,
1328
- filters: this.filterMap,
1329
- // Only fetch the firstTitle or firstCreator aggregation
1330
- aggregations: { simpleParams: [filterAggregationKey] },
1331
- // Fetch all 26 letter buckets
1332
- aggregationsSize: 26,
1333
- };
1334
- params.uid = await this.requestUID(
1335
- { ...params, sort: sortParams },
1336
- 'aggregations',
1337
- );
1338
-
1339
- const searchResponse = await this.host.searchService?.search(
1340
- params,
1341
- this.host.searchType,
1342
- );
1343
-
1344
- return (searchResponse?.success?.response?.aggregations?.[
1345
- filterAggregationKey
1346
- ]?.buckets ?? []) as Bucket[];
1347
- }
1348
-
1349
- /**
1350
- * Fetches and caches the prefix filter counts for the given filter type.
1351
- */
1352
- async updatePrefixFilterCounts(filterType: PrefixFilterType): Promise<void> {
1353
- const { facetFetchQueryKey } = this;
1354
- const buckets = await this.fetchPrefixFilterBuckets(filterType);
1355
-
1356
- // Don't update the filter counts for an outdated query (if it has been changed
1357
- // since we sent the request)
1358
- const queryChangedSinceFetch =
1359
- facetFetchQueryKey !== this.facetFetchQueryKey;
1360
- if (queryChangedSinceFetch) return;
1361
-
1362
- // Unpack the aggregation buckets into a simple map like { 'A': 50, 'B': 25, ... }
1363
- this.prefixFilterCountMap = { ...this.prefixFilterCountMap }; // Clone the object to trigger an update
1364
- this.prefixFilterCountMap[filterType] = buckets.reduce(
1365
- (acc: Record<string, number>, bucket: Bucket) => {
1366
- acc[(bucket.key as string).toUpperCase()] = bucket.doc_count;
1367
- return acc;
1368
- },
1369
- {},
1370
- );
1371
-
1372
- this.requestHostUpdate();
1373
- }
1374
-
1375
- /**
1376
- * @inheritdoc
1377
- */
1378
- async updatePrefixFiltersForCurrentSort(): Promise<void> {
1379
- if (['title', 'creator'].includes(this.host.selectedSort as SortField)) {
1380
- const filterType = this.host.selectedSort as PrefixFilterType;
1381
- if (!this.prefixFilterCountMap[filterType]) {
1382
- this.updatePrefixFilterCounts(filterType);
1383
- }
1384
- }
1385
- }
1386
-
1387
- /**
1388
- * @inheritdoc
1389
- */
1390
- refreshLetterCounts(): void {
1391
- if (Object.keys(this.prefixFilterCountMap).length > 0) {
1392
- this.prefixFilterCountMap = {};
1393
- }
1394
- this.updatePrefixFiltersForCurrentSort();
1395
- this.requestHostUpdate();
1396
- }
1397
-
1398
- /**
1399
- * @inheritdoc
1400
- */
1401
- populateTVChannelMaps(): Promise<TVChannelMaps> {
1402
- // To ensure that we only make these requests once, cache the Promise returned by the
1403
- // first call, and return the same Promise on repeated calls.
1404
- // Resolves once both maps have been retrieved and saved in the data source.
1405
- if (!this._tvMapsPromise) {
1406
- this._tvMapsPromise = this._fetchTVChannelMaps();
1407
- }
1408
-
1409
- return this._tvMapsPromise;
1410
- }
1411
-
1412
- /**
1413
- * Internal function implementing the actual fetches for TV channel mappings.
1414
- * This should only called by the public populateTVChannelMaps method, which is guarded so
1415
- * that we do not make extra requests for these rather large mappings.
1416
- */
1417
- private async _fetchTVChannelMaps(): Promise<TVChannelMaps> {
1418
- const baseURL = 'https://av.archive.org/etc';
1419
- const dateStr = new Date().toISOString().slice(0, 10); // YYYY-MM-DD
1420
- const chan2networkPromise = fetch(
1421
- `${baseURL}/chan2network.json?date=${dateStr}`,
1422
- );
1423
- const program2chansPromise = fetch(
1424
- `${baseURL}/program2chans.json?date=${dateStr}`,
1425
- );
1426
-
1427
- const [chan2networkResponse, program2chansResponse] = await Promise.all([
1428
- chan2networkPromise,
1429
- program2chansPromise,
1430
- ]);
1431
- this.tvChannelMaps.channelToNetwork = new Map(
1432
- Object.entries(await chan2networkResponse.json()),
1433
- );
1434
- this.tvChannelMaps.programToChannels = new Map(
1435
- Object.entries(await program2chansResponse.json()),
1436
- );
1437
-
1438
- this.requestHostUpdate();
1439
- return this.tvChannelMaps;
1440
- }
1441
- }
1
+ import type { ReactiveControllerHost } from 'lit';
2
+ import {
3
+ AccountExtraInfo,
4
+ Aggregation,
5
+ Bucket,
6
+ CollectionExtraInfo,
7
+ FilterConstraint,
8
+ FilterMap,
9
+ FilterMapBuilder,
10
+ PageElementMap,
11
+ SearchParams,
12
+ SearchResponseSessionContext,
13
+ SearchResult,
14
+ SearchType,
15
+ } from '@internetarchive/search-service';
16
+ import {
17
+ prefixFilterAggregationKeys,
18
+ type FacetBucket,
19
+ type PrefixFilterType,
20
+ TileModel,
21
+ PrefixFilterCounts,
22
+ RequestKind,
23
+ SortField,
24
+ SORT_OPTIONS,
25
+ HitRequestSource,
26
+ } from '../models';
27
+ import {
28
+ FACETLESS_PAGE_ELEMENTS,
29
+ TVChannelMaps,
30
+ type PageSpecifierParams,
31
+ } from './models';
32
+ import type { CollectionBrowserDataSourceInterface } from './collection-browser-data-source-interface';
33
+ import type { CollectionBrowserSearchInterface } from './collection-browser-query-state';
34
+ import { sha1 } from '../utils/sha1';
35
+ import { log } from '../utils/log';
36
+ import { mergeSelectedFacets } from '../utils/facet-utils';
37
+
38
+ export class CollectionBrowserDataSource
39
+ implements CollectionBrowserDataSourceInterface
40
+ {
41
+ /**
42
+ * All pages of tile models that have been fetched so far, indexed by their page
43
+ * number (with the first being page 1).
44
+ */
45
+ private pages: Record<string, TileModel[]> = {};
46
+
47
+ /**
48
+ * Tile offset to apply when looking up tiles in the pages, in order to maintain
49
+ * page alignment after tiles are removed.
50
+ */
51
+ private offset = 0;
52
+
53
+ /**
54
+ * Total number of tile models stored in this data source's pages
55
+ */
56
+ private numTileModels = 0;
57
+
58
+ /**
59
+ * How many consecutive pages should be batched together on the initial page fetch.
60
+ * Defaults to 2 pages.
61
+ */
62
+ private numInitialPages = 2;
63
+
64
+ /**
65
+ * A set of fetch IDs that are valid for the current query state
66
+ */
67
+ private fetchesInProgress = new Set<string>();
68
+
69
+ /**
70
+ * A record of the query key used for the last search.
71
+ * If this changes, we need to load new results.
72
+ */
73
+ private previousQueryKey: string = '';
74
+
75
+ /**
76
+ * Whether the initial page of search results for the current query state
77
+ * is presently being fetched.
78
+ */
79
+ private searchResultsLoading = false;
80
+
81
+ /**
82
+ * Whether the facets (aggregations) for the current query state are
83
+ * presently being fetched.
84
+ */
85
+ private facetsLoading = false;
86
+
87
+ /**
88
+ * Whether the facets are actually visible -- if not, then we can delay any facet
89
+ * fetches until they become visible.
90
+ */
91
+ private facetsReadyToLoad = false;
92
+
93
+ /**
94
+ * Whether further query changes should be ignored and not trigger fetches
95
+ */
96
+ private suppressFetches = false;
97
+
98
+ /**
99
+ * @inheritdoc
100
+ */
101
+ totalResults = 0;
102
+
103
+ /**
104
+ * @inheritdoc
105
+ */
106
+ endOfDataReached = false;
107
+
108
+ /**
109
+ * @inheritdoc
110
+ */
111
+ queryInitialized = false;
112
+
113
+ /**
114
+ * @inheritdoc
115
+ */
116
+ aggregations?: Record<string, Aggregation>;
117
+
118
+ /**
119
+ * @inheritdoc
120
+ */
121
+ histogramAggregation?: Aggregation;
122
+
123
+ /**
124
+ * @inheritdoc
125
+ */
126
+ collectionTitles = new Map<string, string>();
127
+
128
+ /**
129
+ * @inheritdoc
130
+ */
131
+ tvChannelMaps: TVChannelMaps = {};
132
+
133
+ /**
134
+ * @inheritdoc
135
+ */
136
+ tvChannelAliases = new Map<string, string>();
137
+
138
+ /**
139
+ * @inheritdoc
140
+ */
141
+ collectionExtraInfo?: CollectionExtraInfo;
142
+
143
+ /**
144
+ * @inheritdoc
145
+ */
146
+ accountExtraInfo?: AccountExtraInfo;
147
+
148
+ /**
149
+ * @inheritdoc
150
+ */
151
+ sessionContext?: SearchResponseSessionContext;
152
+
153
+ /**
154
+ * @inheritdoc
155
+ */
156
+ pageElements?: PageElementMap;
157
+
158
+ /**
159
+ * @inheritdoc
160
+ */
161
+ parentCollections?: string[] = [];
162
+
163
+ /**
164
+ * @inheritdoc
165
+ */
166
+ prefixFilterCountMap: Partial<Record<PrefixFilterType, PrefixFilterCounts>> =
167
+ {};
168
+
169
+ /**
170
+ * @inheritdoc
171
+ */
172
+ queryErrorMessage?: string;
173
+
174
+ /**
175
+ * Internal property to store the promise for any current TV channel maps fetch.
176
+ */
177
+ private _tvMapsPromise?: Promise<TVChannelMaps>;
178
+
179
+ /**
180
+ * Internal property to store the private value backing the `initialSearchComplete` getter.
181
+ */
182
+ private _initialSearchCompletePromise: Promise<boolean> =
183
+ Promise.resolve(true);
184
+
185
+ /**
186
+ * @inheritdoc
187
+ */
188
+ get initialSearchComplete(): Promise<boolean> {
189
+ return this._initialSearchCompletePromise;
190
+ }
191
+
192
+ constructor(
193
+ /** The host element to which this controller should attach listeners */
194
+ private readonly host: ReactiveControllerHost &
195
+ CollectionBrowserSearchInterface,
196
+ /** Default size of result pages */
197
+ private pageSize: number = 50,
198
+ ) {
199
+ // Just setting some property values
200
+ }
201
+
202
+ hostConnected(): void {
203
+ this.setSearchResultsLoading(this.searchResultsLoading);
204
+ this.setFacetsLoading(this.facetsLoading);
205
+ }
206
+
207
+ hostUpdate(): void {
208
+ // This reactive controller hook is run whenever the host component (collection-browser) performs an update.
209
+ // We check whether the host's state has changed in a way which should trigger a reset & new results fetch.
210
+
211
+ // Only the currently-installed data source should react to the update
212
+ if (!this.activeOnHost) return;
213
+
214
+ // Copy loading states onto the host
215
+ this.setSearchResultsLoading(this.searchResultsLoading);
216
+ this.setFacetsLoading(this.facetsLoading);
217
+
218
+ // Can't perform searches without a search service
219
+ if (!this.host.searchService) return;
220
+
221
+ // We should only reset if part of the full query state has changed
222
+ const queryKeyChanged = this.pageFetchQueryKey !== this.previousQueryKey;
223
+ if (!queryKeyChanged) return;
224
+
225
+ // We should only reset if either:
226
+ // (a) our state permits a valid search, or
227
+ // (b) we have a blank query that we're showing a placeholder/message for
228
+ const queryIsEmpty = !this.host.baseQuery;
229
+ if (!(this.canPerformSearch || queryIsEmpty)) return;
230
+
231
+ if (this.activeOnHost) this.host.emitQueryStateChanged();
232
+ this.handleQueryChange();
233
+ }
234
+
235
+ /**
236
+ * Returns whether this data source is the one currently installed on the host component.
237
+ */
238
+ private get activeOnHost(): boolean {
239
+ return this.host.dataSource === this;
240
+ }
241
+
242
+ /**
243
+ * @inheritdoc
244
+ */
245
+ get size(): number {
246
+ return this.numTileModels;
247
+ }
248
+
249
+ /**
250
+ * @inheritdoc
251
+ */
252
+ reset(): void {
253
+ log('Resetting CB data source');
254
+ this.pages = {};
255
+ this.aggregations = {};
256
+ this.histogramAggregation = undefined;
257
+ this.pageElements = undefined;
258
+ this.parentCollections = [];
259
+ this.previousQueryKey = '';
260
+ this.queryErrorMessage = undefined;
261
+
262
+ this.offset = 0;
263
+ this.numTileModels = 0;
264
+ this.endOfDataReached = false;
265
+ this.queryInitialized = false;
266
+ this.facetsLoading = false;
267
+
268
+ // Invalidate any fetches in progress
269
+ this.fetchesInProgress.clear();
270
+
271
+ this.setTotalResultCount(0);
272
+ this.requestHostUpdate();
273
+ }
274
+
275
+ /**
276
+ * @inheritdoc
277
+ */
278
+ resetPages(): void {
279
+ if (Object.keys(this.pages).length < this.host.maxPagesToManage) {
280
+ this.pages = {};
281
+
282
+ // Invalidate any page fetches in progress (keep facet fetches)
283
+ this.fetchesInProgress.forEach(key => {
284
+ if (!key.startsWith('facets-')) this.fetchesInProgress.delete(key);
285
+ });
286
+ this.requestHostUpdate();
287
+ }
288
+ }
289
+
290
+ /**
291
+ * @inheritdoc
292
+ */
293
+ addPage(pageNum: number, pageTiles: TileModel[]): void {
294
+ this.pages[pageNum] = pageTiles;
295
+ this.numTileModels += pageTiles.length;
296
+ this.requestHostUpdate();
297
+ }
298
+
299
+ /**
300
+ * @inheritdoc
301
+ */
302
+ addMultiplePages(firstPageNum: number, tiles: TileModel[]): void {
303
+ const numPages = Math.ceil(tiles.length / this.pageSize);
304
+ for (let i = 0; i < numPages; i += 1) {
305
+ const pageStartIndex = this.pageSize * i;
306
+ this.addPage(
307
+ firstPageNum + i,
308
+ tiles.slice(pageStartIndex, pageStartIndex + this.pageSize),
309
+ );
310
+ }
311
+
312
+ const visiblePages = this.host.currentVisiblePageNumbers;
313
+ const needsReload = visiblePages.some(
314
+ page => page >= firstPageNum && page <= firstPageNum + numPages,
315
+ );
316
+ if (needsReload) {
317
+ this.refreshVisibleResults();
318
+ }
319
+ }
320
+
321
+ /**
322
+ * @inheritdoc
323
+ */
324
+ getPage(pageNum: number): TileModel[] {
325
+ return this.pages[pageNum];
326
+ }
327
+
328
+ /**
329
+ * @inheritdoc
330
+ */
331
+ getAllPages(): Record<string, TileModel[]> {
332
+ return this.pages;
333
+ }
334
+
335
+ /**
336
+ * @inheritdoc
337
+ */
338
+ hasPage(pageNum: number): boolean {
339
+ return !!this.pages[pageNum];
340
+ }
341
+
342
+ /**
343
+ * @inheritdoc
344
+ */
345
+ getTileModelAt(index: number): TileModel | undefined {
346
+ const offsetIndex = index + this.offset;
347
+ const expectedPageNum = Math.floor(offsetIndex / this.pageSize) + 1;
348
+ const expectedIndexOnPage = offsetIndex % this.pageSize;
349
+
350
+ let page = 1;
351
+ let tilesSeen = 0;
352
+ while (tilesSeen <= offsetIndex) {
353
+ if (!this.pages[page]) {
354
+ // If we encounter a missing page, either we're past all the results or the page data is sparse.
355
+ // So just return the tile at the expected position if it exists.
356
+ return this.pages[expectedPageNum]?.[expectedIndexOnPage];
357
+ }
358
+
359
+ if (tilesSeen + this.pages[page].length > offsetIndex) {
360
+ return this.pages[page][offsetIndex - tilesSeen];
361
+ }
362
+
363
+ tilesSeen += this.pages[page].length;
364
+ page += 1;
365
+ }
366
+
367
+ return this.pages[expectedPageNum]?.[expectedIndexOnPage];
368
+ }
369
+
370
+ /**
371
+ * @inheritdoc
372
+ */
373
+ indexOf(tile: TileModel): number {
374
+ return Object.values(this.pages).flat().indexOf(tile) - this.offset;
375
+ }
376
+
377
+ /**
378
+ * @inheritdoc
379
+ */
380
+ getPageSize(): number {
381
+ return this.pageSize;
382
+ }
383
+
384
+ /**
385
+ * @inheritdoc
386
+ */
387
+ setPageSize(pageSize: number): void {
388
+ this.reset();
389
+ this.pageSize = pageSize;
390
+ }
391
+
392
+ /**
393
+ * @inheritdoc
394
+ */
395
+ setNumInitialPages(numPages: number): void {
396
+ this.numInitialPages = numPages;
397
+ }
398
+
399
+ /**
400
+ * @inheritdoc
401
+ */
402
+ setTotalResultCount(count: number): void {
403
+ this.totalResults = count;
404
+ if (this.activeOnHost) {
405
+ this.host.setTotalResultCount(count);
406
+ }
407
+ }
408
+
409
+ /**
410
+ * @inheritdoc
411
+ */
412
+ setFetchesSuppressed(suppressed: boolean): void {
413
+ this.suppressFetches = suppressed;
414
+ }
415
+
416
+ /**
417
+ * @inheritdoc
418
+ */
419
+ setEndOfDataReached(reached: boolean): void {
420
+ this.endOfDataReached = reached;
421
+ }
422
+
423
+ /**
424
+ * @inheritdoc
425
+ */
426
+ async handleQueryChange(): Promise<void> {
427
+ // Don't react to the change if fetches are suppressed for this data source
428
+ if (this.suppressFetches) return;
429
+
430
+ this.reset();
431
+
432
+ // Reset the `initialSearchComplete` promise with a new value for the imminent search
433
+ let initialSearchCompleteResolver: (value: boolean) => void;
434
+ this._initialSearchCompletePromise = new Promise(res => {
435
+ initialSearchCompleteResolver = res;
436
+ });
437
+
438
+ // Fire the initial page & facet requests
439
+ this.queryInitialized = true;
440
+ await Promise.all([
441
+ this.doInitialPageFetch(),
442
+ this.canFetchFacets ? this.fetchFacets() : null,
443
+ ]);
444
+
445
+ // Resolve the `initialSearchComplete` promise for this search
446
+ initialSearchCompleteResolver!(true);
447
+ }
448
+
449
+ /**
450
+ * @inheritdoc
451
+ */
452
+ async handleFacetReadinessChange(ready: boolean): Promise<void> {
453
+ const facetsBecameReady = !this.facetsReadyToLoad && ready;
454
+ this.facetsReadyToLoad = ready;
455
+
456
+ const needsFetch = facetsBecameReady && this.canFetchFacets;
457
+ if (needsFetch) {
458
+ this.fetchFacets();
459
+ }
460
+ }
461
+
462
+ /**
463
+ * Whether the data source & its host are in a state where a facet request should be fired.
464
+ * (i.e., they aren't suppressed or already loading, etc.)
465
+ */
466
+ private get canFetchFacets(): boolean {
467
+ // Don't fetch facets if they are suppressed entirely or not required for the current profile page element
468
+ if (this.host.facetLoadStrategy === 'off') return false;
469
+ if (FACETLESS_PAGE_ELEMENTS.includes(this.host.profileElement!))
470
+ return false;
471
+
472
+ // If facets are to be lazy-loaded, don't fetch them if they are not going to be visible anyway
473
+ // (wait until they become visible instead)
474
+ if (this.host.facetLoadStrategy !== 'eager' && !this.facetsReadyToLoad)
475
+ return false;
476
+
477
+ // Don't fetch facets again if they are already fetched or pending
478
+ const facetsAlreadyFetched =
479
+ Object.keys(this.aggregations ?? {}).length > 0;
480
+ if (this.facetsLoading || facetsAlreadyFetched) return false;
481
+
482
+ return true;
483
+ }
484
+
485
+ /**
486
+ * @inheritdoc
487
+ */
488
+ map(
489
+ callback: (
490
+ model: TileModel,
491
+ index: number,
492
+ array: TileModel[],
493
+ ) => TileModel,
494
+ ): void {
495
+ if (!Object.keys(this.pages).length) return;
496
+ this.pages = Object.fromEntries(
497
+ Object.entries(this.pages).map(([page, tileModels]) => [
498
+ page,
499
+ tileModels.map((model, index, array) =>
500
+ model ? callback(model, index, array) : model,
501
+ ),
502
+ ]),
503
+ );
504
+ this.requestHostUpdate();
505
+ this.refreshVisibleResults();
506
+ }
507
+
508
+ /**
509
+ * @inheritdoc
510
+ */
511
+ checkAllTiles = (): void => {
512
+ this.map(model => {
513
+ const cloned = model.clone();
514
+ cloned.checked = true;
515
+ return cloned;
516
+ });
517
+ };
518
+
519
+ /**
520
+ * @inheritdoc
521
+ */
522
+ uncheckAllTiles = (): void => {
523
+ this.map(model => {
524
+ const cloned = model.clone();
525
+ cloned.checked = false;
526
+ return cloned;
527
+ });
528
+ };
529
+
530
+ /**
531
+ * @inheritdoc
532
+ */
533
+ removeCheckedTiles = (): void => {
534
+ // To make sure our data source remains page-aligned, we will offset our data source by
535
+ // the number of removed tiles, so that we can just add the offset when the infinite
536
+ // scroller queries for cell contents.
537
+ // This only matters while we're still viewing the same set of results. If the user changes
538
+ // their query/filters/sort, then the data source is overwritten and the offset cleared.
539
+ const { checkedTileModels, uncheckedTileModels } = this;
540
+ const numChecked = checkedTileModels.length;
541
+ if (numChecked === 0) return;
542
+ this.offset += numChecked;
543
+ const newPages: typeof this.pages = {};
544
+
545
+ // Which page the remaining tile models start on, post-offset
546
+ let offsetPageNumber = Math.floor(this.offset / this.pageSize) + 1;
547
+ let indexOnPage = this.offset % this.pageSize;
548
+
549
+ // Fill the pages up to that point with empty models
550
+ for (let page = 1; page <= offsetPageNumber; page += 1) {
551
+ const remainingHidden = this.offset - this.pageSize * (page - 1);
552
+ const offsetCellsOnPage = Math.min(this.pageSize, remainingHidden);
553
+ newPages[page] = Array(offsetCellsOnPage).fill(undefined);
554
+ }
555
+
556
+ // Shift all the remaining tiles into their new positions in the data source
557
+ for (const model of uncheckedTileModels) {
558
+ if (!newPages[offsetPageNumber]) newPages[offsetPageNumber] = [];
559
+ newPages[offsetPageNumber].push(model);
560
+ indexOnPage += 1;
561
+ if (indexOnPage >= this.pageSize) {
562
+ offsetPageNumber += 1;
563
+ indexOnPage = 0;
564
+ }
565
+ }
566
+
567
+ // Swap in the new pages
568
+ this.pages = newPages;
569
+ this.numTileModels -= numChecked;
570
+ this.totalResults -= numChecked;
571
+ this.host.setTileCount(this.size);
572
+ this.host.setTotalResultCount(this.totalResults);
573
+ this.requestHostUpdate();
574
+ this.refreshVisibleResults();
575
+ };
576
+
577
+ /**
578
+ * @inheritdoc
579
+ */
580
+ get checkedTileModels(): TileModel[] {
581
+ return this.getFilteredTileModels(model => model.checked);
582
+ }
583
+
584
+ /**
585
+ * @inheritdoc
586
+ */
587
+ get uncheckedTileModels(): TileModel[] {
588
+ return this.getFilteredTileModels(model => !model.checked);
589
+ }
590
+
591
+ /**
592
+ * Returns a flattened, filtered array of all the tile models in the data source
593
+ * for which the given predicate returns a truthy value.
594
+ *
595
+ * @param predicate A callback function to apply on each tile model, as with Array.filter
596
+ * @returns A filtered array of tile models satisfying the predicate
597
+ */
598
+ private getFilteredTileModels(
599
+ predicate: (model: TileModel, index: number, array: TileModel[]) => unknown,
600
+ ): TileModel[] {
601
+ return Object.values(this.pages)
602
+ .flat()
603
+ .filter((model, index, array) =>
604
+ model ? predicate(model, index, array) : false,
605
+ );
606
+ }
607
+
608
+ /**
609
+ * @inheritdoc
610
+ */
611
+ get canPerformSearch(): boolean {
612
+ if (!this.host.searchService) return false;
613
+
614
+ const trimmedQuery = this.host.baseQuery?.trim();
615
+ const hasNonEmptyQuery = !!trimmedQuery;
616
+ const hasIdentifiers = !!this.host.identifiers?.length;
617
+ const isCollectionSearch = !!this.host.withinCollection;
618
+ const isProfileSearch = !!this.host.withinProfile;
619
+ const hasProfileElement = !!this.host.profileElement;
620
+ const isDefaultedSearch = this.host.searchType === SearchType.DEFAULT;
621
+ const isMetadataSearch = this.host.searchType === SearchType.METADATA;
622
+ const isTvSearch = this.host.searchType === SearchType.TV;
623
+
624
+ // Metadata/tv searches within a collection are allowed to have no query.
625
+ const isValidForCollectionSearch =
626
+ isDefaultedSearch || isMetadataSearch || isTvSearch;
627
+
628
+ // Searches within a profile page may also be performed without a query, provided the profile element is set.
629
+ const isValidForProfileSearch =
630
+ hasProfileElement && (isDefaultedSearch || isMetadataSearch);
631
+
632
+ // Otherwise, a non-empty query must be set.
633
+ return (
634
+ hasNonEmptyQuery ||
635
+ hasIdentifiers ||
636
+ (isCollectionSearch && isValidForCollectionSearch) ||
637
+ (isProfileSearch && isValidForProfileSearch)
638
+ );
639
+ }
640
+
641
+ /**
642
+ * Sets the state for whether the initial set of search results for the
643
+ * current query is loading
644
+ */
645
+ private setSearchResultsLoading(loading: boolean): void {
646
+ this.searchResultsLoading = loading;
647
+ if (this.activeOnHost) {
648
+ this.host.setSearchResultsLoading(loading);
649
+ }
650
+ }
651
+
652
+ /**
653
+ * Sets the state for whether the facets for a query is loading
654
+ */
655
+ private setFacetsLoading(loading: boolean): void {
656
+ this.facetsLoading = loading;
657
+ if (this.activeOnHost) {
658
+ this.host.setFacetsLoading(loading);
659
+ }
660
+ }
661
+
662
+ /**
663
+ * Requests that the host perform an update, provided this data
664
+ * source is actively installed on it.
665
+ */
666
+ private requestHostUpdate(): void {
667
+ if (this.activeOnHost) {
668
+ this.host.requestUpdate();
669
+ }
670
+ }
671
+
672
+ /**
673
+ * Requests that the host refresh its visible tiles, provided this
674
+ * data source is actively installed on it.
675
+ */
676
+ private refreshVisibleResults(): void {
677
+ if (this.activeOnHost) {
678
+ this.host.refreshVisibleResults();
679
+ }
680
+ }
681
+
682
+ /**
683
+ * The query key is a string that uniquely identifies the current search.
684
+ * It consists of:
685
+ * - The current base query
686
+ * - The current collection/profile target & page element
687
+ * - The current search type
688
+ * - Any currently-applied facets
689
+ * - Any currently-applied date range
690
+ * - Any currently-applied prefix filters
691
+ * - The current sort options
692
+ *
693
+ * This lets us internally keep track of queries so we don't persist data that's
694
+ * no longer relevant. Not meant to be human-readable.
695
+ */
696
+ get pageFetchQueryKey(): string {
697
+ const profileKey = `pf;${this.host.withinProfile}--pe;${this.host.profileElement}`;
698
+ const pageTarget = this.host.withinCollection ?? profileKey;
699
+ const sortField = this.host.selectedSort ?? 'none';
700
+ const sortDirection = this.host.sortDirection ?? 'none';
701
+ return `fq:${this.fullQuery}-pt:${pageTarget}-st:${this.host.searchType}-sf:${sortField}-sd:${sortDirection}`;
702
+ }
703
+
704
+ /**
705
+ * Similar to `pageFetchQueryKey` above, but excludes sort fields since they
706
+ * are not relevant in determining aggregation queries.
707
+ */
708
+ get facetFetchQueryKey(): string {
709
+ const profileKey = `pf;${this.host.withinProfile}--pe;${this.host.profileElement}`;
710
+ const pageTarget = this.host.withinCollection ?? profileKey;
711
+ return `facets-fq:${this.fullQuery}-pt:${pageTarget}-st:${this.host.searchType}`;
712
+ }
713
+
714
+ /**
715
+ * Constructs a search service FilterMap object from the combination of
716
+ * all the currently-applied filters. This includes any facets, letter
717
+ * filters, and date range.
718
+ */
719
+ get filterMap(): FilterMap {
720
+ const builder = new FilterMapBuilder();
721
+
722
+ const {
723
+ minSelectedDate,
724
+ maxSelectedDate,
725
+ selectedFacets,
726
+ internalFilters,
727
+ selectedTitleFilter,
728
+ selectedCreatorFilter,
729
+ } = this.host;
730
+
731
+ const dateField = this.host.searchType === SearchType.TV ? 'date' : 'year';
732
+
733
+ if (minSelectedDate) {
734
+ builder.addFilter(
735
+ dateField,
736
+ minSelectedDate,
737
+ FilterConstraint.GREATER_OR_EQUAL,
738
+ );
739
+ }
740
+ if (maxSelectedDate) {
741
+ builder.addFilter(
742
+ dateField,
743
+ maxSelectedDate,
744
+ FilterConstraint.LESS_OR_EQUAL,
745
+ );
746
+ }
747
+
748
+ // Add any selected facets and internal filters
749
+ const combinedFilters = mergeSelectedFacets(
750
+ internalFilters,
751
+ selectedFacets,
752
+ );
753
+ if (combinedFilters) {
754
+ for (const [facetName, facetValues] of Object.entries(combinedFilters)) {
755
+ const { name, values } = this.prepareFacetForFetch(
756
+ facetName,
757
+ facetValues,
758
+ );
759
+ for (const [value, bucket] of Object.entries(values)) {
760
+ let constraint;
761
+ if (bucket.state === 'selected') {
762
+ constraint = FilterConstraint.INCLUDE;
763
+ } else if (bucket.state === 'hidden') {
764
+ constraint = FilterConstraint.EXCLUDE;
765
+ }
766
+
767
+ if (constraint) {
768
+ builder.addFilter(name, value, constraint);
769
+ }
770
+ }
771
+ }
772
+ }
773
+
774
+ // Add any letter filters
775
+ if (selectedTitleFilter) {
776
+ builder.addFilter(
777
+ 'firstTitle',
778
+ selectedTitleFilter,
779
+ FilterConstraint.INCLUDE,
780
+ );
781
+ }
782
+ if (selectedCreatorFilter) {
783
+ builder.addFilter(
784
+ 'firstCreator',
785
+ selectedCreatorFilter,
786
+ FilterConstraint.INCLUDE,
787
+ );
788
+ }
789
+
790
+ const filterMap = builder.build();
791
+ return filterMap;
792
+ }
793
+
794
+ /**
795
+ * Produces a compact unique ID for a search request that can help with debugging
796
+ * on the backend by making related requests easier to trace through different services.
797
+ * (e.g., tying the hits/aggregations requests for the same page back to a single hash).
798
+ *
799
+ * @param params The search service parameters for the request
800
+ * @param kind The kind of request (hits-only, aggregations-only, or both)
801
+ * @returns A Promise resolving to the uid to apply to the request
802
+ */
803
+ async requestUID(params: SearchParams, kind: RequestKind): Promise<string> {
804
+ const paramsToHash = JSON.stringify({
805
+ pageType: params.pageType,
806
+ pageTarget: params.pageTarget,
807
+ query: params.query,
808
+ fields: params.fields,
809
+ filters: params.filters,
810
+ sort: params.sort,
811
+ searchType: this.host.searchType,
812
+ });
813
+
814
+ const fullQueryHash = (await sha1(paramsToHash)).slice(0, 20); // First 80 bits of SHA-1 are plenty for this
815
+ const sessionId = (await this.host.getSessionId()).slice(0, 20); // Likewise
816
+ const page = params.page ?? 0;
817
+ const kindPrefix = kind.charAt(0); // f = full, h = hits, a = aggregations
818
+ const currentTime = Date.now();
819
+
820
+ return `R:${fullQueryHash}-S:${sessionId}-P:${page}-K:${kindPrefix}-T:${currentTime}`;
821
+ }
822
+
823
+ /**
824
+ * @inheritdoc
825
+ */
826
+ get pageSpecifierParams(): PageSpecifierParams | null {
827
+ if (this.host.identifiers?.length) {
828
+ return {
829
+ pageType: 'client_document_fetch',
830
+ };
831
+ }
832
+ if (this.host.withinCollection) {
833
+ return {
834
+ pageType: 'collection_details',
835
+ pageTarget: this.host.withinCollection,
836
+ };
837
+ }
838
+ if (this.host.withinProfile) {
839
+ return {
840
+ pageType: 'account_details',
841
+ pageTarget: this.host.withinProfile,
842
+ pageElements: this.host.profileElement
843
+ ? [this.host.profileElement]
844
+ : [],
845
+ };
846
+ }
847
+ return null;
848
+ }
849
+
850
+ /**
851
+ * The full query, including year facets and date range clauses
852
+ */
853
+ private get fullQuery(): string | undefined {
854
+ const parts = [];
855
+ const trimmedQuery = this.host.baseQuery?.trim();
856
+ if (trimmedQuery) parts.push(trimmedQuery);
857
+
858
+ if (this.host.identifiers) {
859
+ parts.push(`identifier:(${this.host.identifiers.join(' OR ')})`);
860
+ }
861
+
862
+ const { facetQuery, dateRangeQueryClause, sortFilterQueries } = this;
863
+ if (facetQuery) parts.push(facetQuery);
864
+ if (dateRangeQueryClause) parts.push(dateRangeQueryClause);
865
+ if (sortFilterQueries) parts.push(sortFilterQueries);
866
+
867
+ return parts.join(' AND ').trim();
868
+ }
869
+
870
+ /**
871
+ * Generates a query string representing the current set of applied facets
872
+ *
873
+ * Example: `mediatype:("collection" OR "audio" OR -"etree") AND year:("2000" OR "2001")`
874
+ */
875
+ private get facetQuery(): string | undefined {
876
+ if (!this.host.selectedFacets) return undefined;
877
+ const facetClauses = [];
878
+ for (const [facetName, facetValues] of Object.entries(
879
+ this.host.selectedFacets,
880
+ )) {
881
+ facetClauses.push(this.buildFacetClause(facetName, facetValues));
882
+ }
883
+ return this.joinFacetClauses(facetClauses)?.trim();
884
+ }
885
+
886
+ private get dateRangeQueryClause(): string | undefined {
887
+ if (!this.host.minSelectedDate || !this.host.maxSelectedDate) {
888
+ return undefined;
889
+ }
890
+
891
+ return `year:[${this.host.minSelectedDate} TO ${this.host.maxSelectedDate}]`;
892
+ }
893
+
894
+ private get sortFilterQueries(): string {
895
+ const queries = [this.titleQuery, this.creatorQuery];
896
+ return queries.filter(q => q).join(' AND ');
897
+ }
898
+
899
+ /**
900
+ * Returns a query clause identifying the currently selected title filter,
901
+ * e.g., `firstTitle:X`.
902
+ */
903
+ private get titleQuery(): string | undefined {
904
+ return this.host.selectedTitleFilter
905
+ ? `firstTitle:${this.host.selectedTitleFilter}`
906
+ : undefined;
907
+ }
908
+
909
+ /**
910
+ * Returns a query clause identifying the currently selected creator filter,
911
+ * e.g., `firstCreator:X`.
912
+ */
913
+ private get creatorQuery(): string | undefined {
914
+ return this.host.selectedCreatorFilter
915
+ ? `firstCreator:${this.host.selectedCreatorFilter}`
916
+ : undefined;
917
+ }
918
+
919
+ /**
920
+ * Builds an OR-joined facet clause for the given facet name and values.
921
+ *
922
+ * E.g., for name `subject` and values
923
+ * `{ foo: { state: 'selected' }, bar: { state: 'hidden' } }`
924
+ * this will produce the clause
925
+ * `subject:("foo" OR -"bar")`.
926
+ *
927
+ * @param facetName The facet type (e.g., 'collection')
928
+ * @param facetValues The facet buckets, mapped by their keys
929
+ */
930
+ private buildFacetClause(
931
+ facetName: string,
932
+ facetValues: Record<string, FacetBucket>,
933
+ ): string {
934
+ const { name: facetQueryName, values } = this.prepareFacetForFetch(
935
+ facetName,
936
+ facetValues,
937
+ );
938
+ const facetEntries = Object.entries(values);
939
+ if (facetEntries.length === 0) return '';
940
+
941
+ const facetValuesArray: string[] = [];
942
+ for (const [key, facetData] of facetEntries) {
943
+ const plusMinusPrefix = facetData.state === 'hidden' ? '-' : '';
944
+ facetValuesArray.push(`${plusMinusPrefix}"${key}"`);
945
+ }
946
+
947
+ const valueQuery = facetValuesArray.join(` OR `);
948
+ return `${facetQueryName}:(${valueQuery})`;
949
+ }
950
+
951
+ /**
952
+ * Handles some special pre-request normalization steps for certain facet types
953
+ * that require them.
954
+ *
955
+ * @param facetName The name of the facet type (e.g., 'language')
956
+ * @param facetValues An array of values for that facet type
957
+ */
958
+ private prepareFacetForFetch(
959
+ facetName: string,
960
+ facetValues: Record<string, FacetBucket>,
961
+ ): { name: string; values: Record<string, FacetBucket> } {
962
+ // eslint-disable-next-line prefer-const
963
+ let [normalizedName, normalizedValues] = [facetName, facetValues];
964
+
965
+ // The full "search engine" name of the lending field is "lending___status"
966
+ if (facetName === 'lending') {
967
+ normalizedName = 'lending___status';
968
+ }
969
+
970
+ return {
971
+ name: normalizedName,
972
+ values: normalizedValues,
973
+ };
974
+ }
975
+
976
+ /**
977
+ * Takes an array of facet clauses, and combines them into a
978
+ * full AND-joined facet query string. Empty clauses are ignored.
979
+ */
980
+ private joinFacetClauses(facetClauses: string[]): string | undefined {
981
+ const nonEmptyFacetClauses = facetClauses.filter(
982
+ clause => clause.length > 0,
983
+ );
984
+ return nonEmptyFacetClauses.length > 0
985
+ ? `(${nonEmptyFacetClauses.join(' AND ')})`
986
+ : undefined;
987
+ }
988
+
989
+ /**
990
+ * Fires a backend request to fetch a set of aggregations (representing UI facets) for
991
+ * the current search state.
992
+ */
993
+ private async fetchFacets(): Promise<void> {
994
+ const trimmedQuery = this.host.baseQuery?.trim();
995
+ if (!this.canPerformSearch) return;
996
+
997
+ const { facetFetchQueryKey } = this;
998
+ if (this.fetchesInProgress.has(facetFetchQueryKey)) return;
999
+ this.fetchesInProgress.add(facetFetchQueryKey);
1000
+
1001
+ this.setFacetsLoading(true);
1002
+
1003
+ const sortParams = this.host.sortParam ? [this.host.sortParam] : [];
1004
+ const params: SearchParams = {
1005
+ ...this.pageSpecifierParams,
1006
+ query: trimmedQuery || '',
1007
+ identifiers: this.host.identifiers,
1008
+ rows: 0,
1009
+ filters: this.filterMap,
1010
+ // Fetch a few extra buckets beyond the 6 we show, in case some get suppressed
1011
+ aggregationsSize: 10,
1012
+ // Note: we don't need an aggregations param to fetch the default aggregations from the PPS.
1013
+ // The default aggregations for the search_results page type should be what we need here.
1014
+ };
1015
+ params.uid = await this.requestUID(
1016
+ { ...params, sort: sortParams },
1017
+ 'aggregations',
1018
+ );
1019
+
1020
+ const searchResponse = await this.host.searchService?.search(
1021
+ params,
1022
+ this.host.searchType,
1023
+ );
1024
+ const success = searchResponse?.success;
1025
+
1026
+ // This is checking to see if the query has changed since the data was fetched.
1027
+ // If so, we just want to discard this set of aggregations because they are
1028
+ // likely no longer valid for the newer query.
1029
+ const queryChangedSinceFetch =
1030
+ !this.fetchesInProgress.has(facetFetchQueryKey);
1031
+ this.fetchesInProgress.delete(facetFetchQueryKey);
1032
+ if (queryChangedSinceFetch) return;
1033
+
1034
+ if (!success) {
1035
+ const errorMsg = searchResponse?.error?.message;
1036
+ const detailMsg = searchResponse?.error?.details?.message;
1037
+
1038
+ if (!errorMsg && !detailMsg) {
1039
+ // @ts-expect-error: Property 'Sentry' does not exist on type 'Window & typeof globalThis'
1040
+ window?.Sentry?.captureMessage?.(
1041
+ 'Missing or malformed facet response from backend',
1042
+ 'error',
1043
+ );
1044
+ }
1045
+
1046
+ this.setFacetsLoading(false);
1047
+ return;
1048
+ }
1049
+
1050
+ const { aggregations, collectionTitles, tvChannelAliases } =
1051
+ success.response;
1052
+ this.aggregations = aggregations;
1053
+
1054
+ this.histogramAggregation =
1055
+ this.host.searchType === SearchType.TV
1056
+ ? aggregations?.date_histogram
1057
+ : aggregations?.year_histogram;
1058
+
1059
+ if (collectionTitles) {
1060
+ for (const [id, title] of Object.entries(collectionTitles)) {
1061
+ this.collectionTitles.set(id, title);
1062
+ }
1063
+ }
1064
+ if (tvChannelAliases) {
1065
+ for (const [channel, network] of Object.entries(tvChannelAliases)) {
1066
+ this.tvChannelAliases.set(channel, network);
1067
+ }
1068
+ }
1069
+
1070
+ this.setFacetsLoading(false);
1071
+ this.requestHostUpdate();
1072
+ }
1073
+
1074
+ /**
1075
+ * Performs the initial page fetch(es) for the current search state.
1076
+ */
1077
+ private async doInitialPageFetch(): Promise<void> {
1078
+ this.setSearchResultsLoading(true);
1079
+ // Try to batch 2 initial page requests when possible
1080
+ await this.fetchPage(this.host.initialPageNumber, this.numInitialPages);
1081
+ }
1082
+
1083
+ /**
1084
+ * Fetches one or more pages of results and updates the data source.
1085
+ *
1086
+ * @param pageNumber The page number to fetch
1087
+ * @param numInitialPages If this is an initial page fetch (`pageNumber = 1`),
1088
+ * specifies how many pages to batch together in one request. Ignored
1089
+ * if `pageNumber != 1`, defaulting to a single page.
1090
+ */
1091
+ async fetchPage(pageNumber: number, numInitialPages = 1): Promise<void> {
1092
+ const trimmedQuery = this.host.baseQuery?.trim();
1093
+ // reset loading status
1094
+ if (!this.canPerformSearch) {
1095
+ this.setSearchResultsLoading(false);
1096
+ return;
1097
+ }
1098
+
1099
+ // if we already have data, don't fetch again
1100
+ if (this.hasPage(pageNumber)) return;
1101
+
1102
+ if (this.endOfDataReached) return;
1103
+
1104
+ // Batch multiple initial page requests together if needed (e.g., can request
1105
+ // pages 1 and 2 together in a single request).
1106
+ let numPages = pageNumber === 1 ? numInitialPages : 1;
1107
+ const numRows = this.pageSize * numPages;
1108
+
1109
+ // if a fetch is already in progress for this query and page, don't fetch again
1110
+ const { pageFetchQueryKey } = this;
1111
+ const currentPageKey = `${pageFetchQueryKey}-p:${pageNumber}`;
1112
+ if (this.fetchesInProgress.has(currentPageKey)) return;
1113
+
1114
+ for (let i = 0; i < numPages; i += 1) {
1115
+ this.fetchesInProgress.add(`${pageFetchQueryKey}-p:${pageNumber + i}`);
1116
+ }
1117
+ this.previousQueryKey = pageFetchQueryKey;
1118
+
1119
+ const { withinCollection, withinProfile } = this.host;
1120
+
1121
+ let sortParams = this.host.sortParam ? [this.host.sortParam] : [];
1122
+ // TODO eventually the PPS should handle these defaults natively
1123
+ const isDefaultProfileSort =
1124
+ withinProfile && this.host.selectedSort === SortField.default;
1125
+ if (isDefaultProfileSort && this.host.defaultSortField) {
1126
+ const sortOption = SORT_OPTIONS[this.host.defaultSortField];
1127
+ if (sortOption.searchServiceKey) {
1128
+ sortParams = [
1129
+ {
1130
+ field: sortOption.searchServiceKey,
1131
+ direction: 'desc',
1132
+ },
1133
+ ];
1134
+ }
1135
+ }
1136
+
1137
+ const params: SearchParams = {
1138
+ ...this.pageSpecifierParams,
1139
+ query: trimmedQuery || '',
1140
+ identifiers: this.host.identifiers,
1141
+ page: pageNumber,
1142
+ rows: numRows,
1143
+ sort: sortParams,
1144
+ filters: this.filterMap,
1145
+ aggregations: { omit: true },
1146
+ };
1147
+ params.uid = await this.requestUID(params, 'hits');
1148
+
1149
+ // log('=== FIRING PAGE REQUEST ===', params);
1150
+ const searchResponse = await this.host.searchService?.search(
1151
+ params,
1152
+ this.host.searchType,
1153
+ );
1154
+ // log('=== RECEIVED PAGE RESPONSE IN CB ===', searchResponse);
1155
+ const success = searchResponse?.success;
1156
+
1157
+ // This is checking to see if the fetch has been invalidated since it was fired off.
1158
+ // If so, we just want to discard the response since it is for an obsolete query state.
1159
+ if (!this.fetchesInProgress.has(currentPageKey)) return;
1160
+ for (let i = 0; i < numPages; i += 1) {
1161
+ this.fetchesInProgress.delete(`${pageFetchQueryKey}-p:${pageNumber + i}`);
1162
+ }
1163
+
1164
+ if (!success) {
1165
+ const errorMsg = searchResponse?.error?.message;
1166
+ const detailMsg = searchResponse?.error?.details?.message;
1167
+
1168
+ this.queryErrorMessage = `${errorMsg ?? ''}${
1169
+ detailMsg ? `; ${detailMsg}` : ''
1170
+ }`;
1171
+
1172
+ if (!this.queryErrorMessage) {
1173
+ this.queryErrorMessage = 'Missing or malformed response from backend';
1174
+ // @ts-expect-error: Property 'Sentry' does not exist on type 'Window & typeof globalThis'
1175
+ window?.Sentry?.captureMessage?.(this.queryErrorMessage, 'error');
1176
+ }
1177
+
1178
+ this.setSearchResultsLoading(false);
1179
+ this.requestHostUpdate();
1180
+ this.host.emitSearchError();
1181
+ return;
1182
+ }
1183
+
1184
+ this.setTotalResultCount(success.response.totalResults - this.offset);
1185
+ if (this.activeOnHost && this.totalResults === 0) {
1186
+ // display event to offshoot when result count is zero.
1187
+ this.host.emitEmptyResults();
1188
+ }
1189
+
1190
+ this.sessionContext = success.sessionContext;
1191
+ if (withinCollection) {
1192
+ this.collectionExtraInfo = success.response.collectionExtraInfo;
1193
+
1194
+ // For collections, we want the UI to respect the default sort option
1195
+ // which can be specified in metadata, or otherwise assumed to be week:desc
1196
+ if (this.activeOnHost) {
1197
+ this.host.applyDefaultCollectionSort(this.collectionExtraInfo);
1198
+ }
1199
+
1200
+ if (this.collectionExtraInfo) {
1201
+ this.parentCollections = [].concat(
1202
+ this.collectionExtraInfo.public_metadata?.collection ?? [],
1203
+ );
1204
+
1205
+ // Update the TV collection status now that we know the parent collections
1206
+ this.host.isTVCollection =
1207
+ this.host.withinCollection?.startsWith('TV-') ||
1208
+ this.host.withinCollection === 'tvnews' ||
1209
+ this.host.withinCollection === 'tvarchive' ||
1210
+ this.parentCollections.includes('tvnews') ||
1211
+ this.parentCollections.includes('tvarchive');
1212
+ }
1213
+ } else if (withinProfile) {
1214
+ this.accountExtraInfo = success.response.accountExtraInfo;
1215
+ this.pageElements = success.response.pageElements;
1216
+ }
1217
+
1218
+ const { results, collectionTitles, tvChannelAliases } = success.response;
1219
+ if (results && results.length > 0) {
1220
+ // Load any collection titles present on the response into the cache,
1221
+ // or queue up preload fetches for them if none were present.
1222
+ if (collectionTitles) {
1223
+ for (const [id, title] of Object.entries(collectionTitles)) {
1224
+ this.collectionTitles.set(id, title);
1225
+ }
1226
+
1227
+ // Also add the target collection's title if available
1228
+ const targetTitle = this.collectionExtraInfo?.public_metadata?.title;
1229
+ if (withinCollection && targetTitle) {
1230
+ this.collectionTitles.set(withinCollection, targetTitle);
1231
+ }
1232
+ }
1233
+
1234
+ if (tvChannelAliases) {
1235
+ for (const [channel, network] of Object.entries(tvChannelAliases)) {
1236
+ this.tvChannelAliases.set(channel, network);
1237
+ }
1238
+ }
1239
+
1240
+ // Update the data source for each returned page.
1241
+ // For loans and web archives, we must account for receiving more pages than we asked for.
1242
+ const isUnpagedElement = ['lending', 'web_archives'].includes(
1243
+ this.host.profileElement!,
1244
+ );
1245
+ if (isUnpagedElement) {
1246
+ numPages = Math.ceil(results.length / this.pageSize);
1247
+ this.endOfDataReached = true;
1248
+ if (this.activeOnHost) this.host.setTileCount(this.totalResults);
1249
+ }
1250
+
1251
+ for (let i = 0; i < numPages; i += 1) {
1252
+ const pageStartIndex = this.pageSize * i;
1253
+ this.addFetchedResultsToDataSource(
1254
+ pageNumber + i,
1255
+ results.slice(pageStartIndex, pageStartIndex + this.pageSize),
1256
+ !isUnpagedElement || i === numPages - 1,
1257
+ );
1258
+ }
1259
+ }
1260
+
1261
+ // When we reach the end of the data, we can set the infinite scroller's
1262
+ // item count to the real total number of results (rather than the
1263
+ // temporary estimates based on pages rendered so far).
1264
+ if (this.size >= this.totalResults || results.length === 0) {
1265
+ this.endOfDataReached = true;
1266
+ if (this.activeOnHost) this.host.setTileCount(this.size);
1267
+ }
1268
+
1269
+ this.setSearchResultsLoading(false);
1270
+ this.requestHostUpdate();
1271
+ }
1272
+
1273
+ /**
1274
+ * Returns the type of request that produced the current set of hits,
1275
+ * based on the presence of a search query or profile/collection target
1276
+ * on the host.
1277
+ */
1278
+ private get hitRequestSource(): HitRequestSource {
1279
+ const { host } = this;
1280
+ if (host.baseQuery) return 'search_query';
1281
+ if (host.withinProfile) return 'profile_tab';
1282
+ if (host.withinCollection) return 'collection_members';
1283
+ return 'unknown';
1284
+ }
1285
+
1286
+ /**
1287
+ * Update the datasource from the fetch response
1288
+ *
1289
+ * @param pageNumber
1290
+ * @param results
1291
+ */
1292
+ private addFetchedResultsToDataSource(
1293
+ pageNumber: number,
1294
+ results: SearchResult[],
1295
+ needsReload = true,
1296
+ ): void {
1297
+ const tiles: TileModel[] = [];
1298
+ const requestSource = this.hitRequestSource;
1299
+ results?.forEach(result => {
1300
+ if (!result.identifier) return;
1301
+ tiles.push(new TileModel(result, requestSource));
1302
+ });
1303
+
1304
+ this.addPage(pageNumber, tiles);
1305
+
1306
+ if (needsReload) {
1307
+ this.refreshVisibleResults();
1308
+ }
1309
+ }
1310
+
1311
+ /**
1312
+ * Fetches the aggregation buckets for the given prefix filter type.
1313
+ */
1314
+ private async fetchPrefixFilterBuckets(
1315
+ filterType: PrefixFilterType,
1316
+ ): Promise<Bucket[]> {
1317
+ const trimmedQuery = this.host.baseQuery?.trim();
1318
+ if (!this.canPerformSearch) return [];
1319
+
1320
+ const filterAggregationKey = prefixFilterAggregationKeys[filterType];
1321
+ const sortParams = this.host.sortParam ? [this.host.sortParam] : [];
1322
+
1323
+ const params: SearchParams = {
1324
+ ...this.pageSpecifierParams,
1325
+ query: trimmedQuery || '',
1326
+ identifiers: this.host.identifiers,
1327
+ rows: 0,
1328
+ filters: this.filterMap,
1329
+ // Only fetch the firstTitle or firstCreator aggregation
1330
+ aggregations: { simpleParams: [filterAggregationKey] },
1331
+ // Fetch all 26 letter buckets
1332
+ aggregationsSize: 26,
1333
+ };
1334
+ params.uid = await this.requestUID(
1335
+ { ...params, sort: sortParams },
1336
+ 'aggregations',
1337
+ );
1338
+
1339
+ const searchResponse = await this.host.searchService?.search(
1340
+ params,
1341
+ this.host.searchType,
1342
+ );
1343
+
1344
+ return (searchResponse?.success?.response?.aggregations?.[
1345
+ filterAggregationKey
1346
+ ]?.buckets ?? []) as Bucket[];
1347
+ }
1348
+
1349
+ /**
1350
+ * Fetches and caches the prefix filter counts for the given filter type.
1351
+ */
1352
+ async updatePrefixFilterCounts(filterType: PrefixFilterType): Promise<void> {
1353
+ const { facetFetchQueryKey } = this;
1354
+ const buckets = await this.fetchPrefixFilterBuckets(filterType);
1355
+
1356
+ // Don't update the filter counts for an outdated query (if it has been changed
1357
+ // since we sent the request)
1358
+ const queryChangedSinceFetch =
1359
+ facetFetchQueryKey !== this.facetFetchQueryKey;
1360
+ if (queryChangedSinceFetch) return;
1361
+
1362
+ // Unpack the aggregation buckets into a simple map like { 'A': 50, 'B': 25, ... }
1363
+ this.prefixFilterCountMap = { ...this.prefixFilterCountMap }; // Clone the object to trigger an update
1364
+ this.prefixFilterCountMap[filterType] = buckets.reduce(
1365
+ (acc: Record<string, number>, bucket: Bucket) => {
1366
+ acc[(bucket.key as string).toUpperCase()] = bucket.doc_count;
1367
+ return acc;
1368
+ },
1369
+ {},
1370
+ );
1371
+
1372
+ this.requestHostUpdate();
1373
+ }
1374
+
1375
+ /**
1376
+ * @inheritdoc
1377
+ */
1378
+ async updatePrefixFiltersForCurrentSort(): Promise<void> {
1379
+ if (['title', 'creator'].includes(this.host.selectedSort as SortField)) {
1380
+ const filterType = this.host.selectedSort as PrefixFilterType;
1381
+ if (!this.prefixFilterCountMap[filterType]) {
1382
+ this.updatePrefixFilterCounts(filterType);
1383
+ }
1384
+ }
1385
+ }
1386
+
1387
+ /**
1388
+ * @inheritdoc
1389
+ */
1390
+ refreshLetterCounts(): void {
1391
+ if (Object.keys(this.prefixFilterCountMap).length > 0) {
1392
+ this.prefixFilterCountMap = {};
1393
+ }
1394
+ this.updatePrefixFiltersForCurrentSort();
1395
+ this.requestHostUpdate();
1396
+ }
1397
+
1398
+ /**
1399
+ * @inheritdoc
1400
+ */
1401
+ populateTVChannelMaps(): Promise<TVChannelMaps> {
1402
+ // To ensure that we only make these requests once, cache the Promise returned by the
1403
+ // first call, and return the same Promise on repeated calls.
1404
+ // Resolves once both maps have been retrieved and saved in the data source.
1405
+ if (!this._tvMapsPromise) {
1406
+ this._tvMapsPromise = this._fetchTVChannelMaps();
1407
+ }
1408
+
1409
+ return this._tvMapsPromise;
1410
+ }
1411
+
1412
+ /**
1413
+ * Internal function implementing the actual fetches for TV channel mappings.
1414
+ * This should only called by the public populateTVChannelMaps method, which is guarded so
1415
+ * that we do not make extra requests for these rather large mappings.
1416
+ */
1417
+ private async _fetchTVChannelMaps(): Promise<TVChannelMaps> {
1418
+ const baseURL = 'https://av.archive.org/etc';
1419
+ const dateStr = new Date().toISOString().slice(0, 10); // YYYY-MM-DD
1420
+ const chan2networkPromise = fetch(
1421
+ `${baseURL}/chan2network.json?date=${dateStr}`,
1422
+ );
1423
+ const program2chansPromise = fetch(
1424
+ `${baseURL}/program2chans.json?date=${dateStr}`,
1425
+ );
1426
+
1427
+ const [chan2networkResponse, program2chansResponse] = await Promise.all([
1428
+ chan2networkPromise,
1429
+ program2chansPromise,
1430
+ ]);
1431
+ this.tvChannelMaps.channelToNetwork = new Map(
1432
+ Object.entries(await chan2networkResponse.json()),
1433
+ );
1434
+ this.tvChannelMaps.programToChannels = new Map(
1435
+ Object.entries(await program2chansResponse.json()),
1436
+ );
1437
+
1438
+ this.requestHostUpdate();
1439
+ return this.tvChannelMaps;
1440
+ }
1441
+ }