@internetarchive/collection-browser 4.5.1-alpha-webdev8221.0 → 4.5.2

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