@internetarchive/collection-browser 4.1.0 → 4.2.0-alpha-webdev8164.1

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