@internetarchive/collection-browser 4.3.0 → 4.3.1-alpha-webdev8257.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/dist/index.d.ts +1 -0
  2. package/dist/index.js.map +1 -1
  3. package/dist/src/app-root.d.ts +11 -0
  4. package/dist/src/app-root.js +107 -0
  5. package/dist/src/app-root.js.map +1 -1
  6. package/dist/src/collection-browser.d.ts +8 -4
  7. package/dist/src/collection-browser.js +19 -20
  8. package/dist/src/collection-browser.js.map +1 -1
  9. package/dist/src/data-source/collection-browser-data-source.js.map +1 -1
  10. package/dist/src/manage/manage-bar.d.ts +7 -2
  11. package/dist/src/manage/manage-bar.js +25 -3
  12. package/dist/src/manage/manage-bar.js.map +1 -1
  13. package/dist/src/styles/tile-action-styles.d.ts +14 -0
  14. package/dist/src/styles/tile-action-styles.js +52 -0
  15. package/dist/src/styles/tile-action-styles.js.map +1 -0
  16. package/dist/src/tiles/base-tile-component.d.ts +17 -1
  17. package/dist/src/tiles/base-tile-component.js +48 -1
  18. package/dist/src/tiles/base-tile-component.js.map +1 -1
  19. package/dist/src/tiles/grid/item-tile.js +1 -0
  20. package/dist/src/tiles/grid/item-tile.js.map +1 -1
  21. package/dist/src/tiles/list/tile-list-compact-header.js +66 -46
  22. package/dist/src/tiles/list/tile-list-compact-header.js.map +1 -1
  23. package/dist/src/tiles/list/tile-list-compact.d.ts +1 -1
  24. package/dist/src/tiles/list/tile-list-compact.js +132 -100
  25. package/dist/src/tiles/list/tile-list-compact.js.map +1 -1
  26. package/dist/src/tiles/list/tile-list.d.ts +1 -1
  27. package/dist/src/tiles/list/tile-list.js +316 -298
  28. package/dist/src/tiles/list/tile-list.js.map +1 -1
  29. package/dist/src/tiles/models.d.ts +14 -0
  30. package/dist/src/tiles/models.js.map +1 -1
  31. package/dist/src/tiles/tile-dispatcher.d.ts +14 -0
  32. package/dist/src/tiles/tile-dispatcher.js +107 -4
  33. package/dist/src/tiles/tile-dispatcher.js.map +1 -1
  34. package/dist/src/tiles/tile-display-value-provider.js.map +1 -1
  35. package/dist/test/data-source/collection-browser-data-source.test.js +2 -2
  36. package/dist/test/data-source/collection-browser-data-source.test.js.map +1 -1
  37. package/dist/test/manage/manage-bar.test.d.ts +1 -0
  38. package/dist/test/manage/manage-bar.test.js +123 -1
  39. package/dist/test/manage/manage-bar.test.js.map +1 -1
  40. package/dist/test/tiles/list/tile-list-compact-header.test.js +12 -12
  41. package/dist/test/tiles/list/tile-list-compact-header.test.js.map +1 -1
  42. package/dist/test/tiles/list/tile-list.test.js +134 -134
  43. package/dist/test/tiles/list/tile-list.test.js.map +1 -1
  44. package/index.ts +1 -0
  45. package/package.json +1 -1
  46. package/src/app-root.ts +115 -0
  47. package/src/collection-browser.ts +16 -30
  48. package/src/data-source/collection-browser-data-source.ts +1465 -1465
  49. package/src/manage/manage-bar.ts +33 -4
  50. package/src/styles/tile-action-styles.ts +52 -0
  51. package/src/tiles/base-tile-component.ts +57 -1
  52. package/src/tiles/grid/item-tile.ts +1 -0
  53. package/src/tiles/list/tile-list-compact-header.ts +106 -86
  54. package/src/tiles/list/tile-list-compact.ts +273 -239
  55. package/src/tiles/list/tile-list.ts +718 -700
  56. package/src/tiles/models.ts +16 -0
  57. package/src/tiles/tile-dispatcher.ts +114 -4
  58. package/src/tiles/tile-display-value-provider.ts +134 -134
  59. package/test/data-source/collection-browser-data-source.test.ts +193 -193
  60. package/test/manage/manage-bar.test.ts +192 -2
  61. package/test/tiles/list/tile-list-compact-header.test.ts +43 -43
  62. package/test/tiles/list/tile-list.test.ts +576 -576
  63. package/.claude/settings.local.json +0 -11
@@ -1,1465 +1,1465 @@
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
- * Returns the search type to actually use for fetches.
608
- * When within a collection with an empty query and FTS selected,
609
- * falls back to metadata search since FTS requires a non-empty query
610
- * and we'd still like to show the unfiltered collection in that case.
611
- */
612
- private get effectiveSearchType(): SearchType {
613
- const trimmedQuery = this.host.baseQuery?.trim();
614
-
615
- const shouldUseMetadataSearch =
616
- !trimmedQuery &&
617
- this.host.withinCollection &&
618
- this.host.searchType === SearchType.FULLTEXT;
619
- if (shouldUseMetadataSearch) return SearchType.METADATA;
620
-
621
- return this.host.searchType;
622
- }
623
-
624
- /**
625
- * @inheritdoc
626
- */
627
- get canPerformSearch(): boolean {
628
- if (!this.host.searchService) return false;
629
-
630
- const trimmedQuery = this.host.baseQuery?.trim();
631
- const hasNonEmptyQuery = !!trimmedQuery;
632
- const hasIdentifiers = !!this.host.identifiers?.length;
633
- const isCollectionSearch = !!this.host.withinCollection;
634
- const isProfileSearch = !!this.host.withinProfile;
635
- const hasProfileElement = !!this.host.profileElement;
636
-
637
- const searchType = this.effectiveSearchType;
638
- const isDefaultedSearch = searchType === SearchType.DEFAULT;
639
- const isMetadataSearch = searchType === SearchType.METADATA;
640
- const isTvSearch = searchType === SearchType.TV;
641
-
642
- // Metadata/tv searches within a collection are allowed to have no query.
643
- const isValidForCollectionSearch =
644
- isDefaultedSearch || isMetadataSearch || isTvSearch;
645
-
646
- // Searches within a profile page may also be performed without a query, provided the profile element is set.
647
- const isValidForProfileSearch =
648
- hasProfileElement && (isDefaultedSearch || isMetadataSearch);
649
-
650
- // Otherwise, a non-empty query must be set.
651
- return (
652
- hasNonEmptyQuery ||
653
- hasIdentifiers ||
654
- (isCollectionSearch && isValidForCollectionSearch) ||
655
- (isProfileSearch && isValidForProfileSearch)
656
- );
657
- }
658
-
659
- /**
660
- * Sets the state for whether the initial set of search results for the
661
- * current query is loading
662
- */
663
- private setSearchResultsLoading(loading: boolean): void {
664
- this.searchResultsLoading = loading;
665
- if (this.activeOnHost) {
666
- this.host.setSearchResultsLoading(loading);
667
- }
668
- }
669
-
670
- /**
671
- * Sets the state for whether the facets for a query is loading
672
- */
673
- private setFacetsLoading(loading: boolean): void {
674
- this.facetsLoading = loading;
675
- if (this.activeOnHost) {
676
- this.host.setFacetsLoading(loading);
677
- }
678
- }
679
-
680
- /**
681
- * Requests that the host perform an update, provided this data
682
- * source is actively installed on it.
683
- */
684
- private requestHostUpdate(): void {
685
- if (this.activeOnHost) {
686
- this.host.requestUpdate();
687
- }
688
- }
689
-
690
- /**
691
- * Requests that the host refresh its visible tiles, provided this
692
- * data source is actively installed on it.
693
- */
694
- private refreshVisibleResults(): void {
695
- if (this.activeOnHost) {
696
- this.host.refreshVisibleResults();
697
- }
698
- }
699
-
700
- /**
701
- * The query key is a string that uniquely identifies the current search.
702
- * It consists of:
703
- * - The current base query
704
- * - The current collection/profile target & page element
705
- * - The current search type
706
- * - Any currently-applied facets
707
- * - Any currently-applied date range
708
- * - Any currently-applied prefix filters
709
- * - The current sort options
710
- *
711
- * This lets us internally keep track of queries so we don't persist data that's
712
- * no longer relevant. Not meant to be human-readable.
713
- */
714
- get pageFetchQueryKey(): string {
715
- const profileKey = `pf;${this.host.withinProfile}--pe;${this.host.profileElement}`;
716
- const pageTarget = this.host.withinCollection ?? profileKey;
717
- const sortField = this.host.selectedSort ?? 'none';
718
- const sortDirection = this.host.sortDirection ?? 'none';
719
- return `fq:${this.fullQuery}-pt:${pageTarget}-st:${this.effectiveSearchType}-sf:${sortField}-sd:${sortDirection}`;
720
- }
721
-
722
- /**
723
- * Similar to `pageFetchQueryKey` above, but excludes sort fields since they
724
- * are not relevant in determining aggregation queries.
725
- */
726
- get facetFetchQueryKey(): string {
727
- const profileKey = `pf;${this.host.withinProfile}--pe;${this.host.profileElement}`;
728
- const pageTarget = this.host.withinCollection ?? profileKey;
729
- return `facets-fq:${this.fullQuery}-pt:${pageTarget}-st:${this.effectiveSearchType}`;
730
- }
731
-
732
- /**
733
- * Constructs a search service FilterMap object from the combination of
734
- * all the currently-applied filters. This includes any facets, letter
735
- * filters, and date range.
736
- */
737
- get filterMap(): FilterMap {
738
- const builder = new FilterMapBuilder();
739
-
740
- const {
741
- minSelectedDate,
742
- maxSelectedDate,
743
- selectedFacets,
744
- internalFilters,
745
- selectedTitleFilter,
746
- selectedCreatorFilter,
747
- } = this.host;
748
-
749
- const dateField = this.host.searchType === SearchType.TV ? 'date' : 'year';
750
-
751
- if (minSelectedDate) {
752
- builder.addFilter(
753
- dateField,
754
- minSelectedDate,
755
- FilterConstraint.GREATER_OR_EQUAL,
756
- );
757
- }
758
- if (maxSelectedDate) {
759
- builder.addFilter(
760
- dateField,
761
- maxSelectedDate,
762
- FilterConstraint.LESS_OR_EQUAL,
763
- );
764
- }
765
-
766
- // Add any selected facets and internal filters
767
- const combinedFilters = mergeSelectedFacets(
768
- internalFilters,
769
- selectedFacets,
770
- );
771
- if (combinedFilters) {
772
- for (const [facetName, facetValues] of Object.entries(combinedFilters)) {
773
- const { name, values } = this.prepareFacetForFetch(
774
- facetName,
775
- facetValues,
776
- );
777
- for (const [value, bucket] of Object.entries(values)) {
778
- let constraint;
779
- if (bucket.state === 'selected') {
780
- constraint = FilterConstraint.INCLUDE;
781
- } else if (bucket.state === 'hidden') {
782
- constraint = FilterConstraint.EXCLUDE;
783
- }
784
-
785
- if (constraint) {
786
- builder.addFilter(name, value, constraint);
787
- }
788
- }
789
- }
790
- }
791
-
792
- // Add any letter filters
793
- if (selectedTitleFilter) {
794
- builder.addFilter(
795
- 'firstTitle',
796
- selectedTitleFilter,
797
- FilterConstraint.INCLUDE,
798
- );
799
- }
800
- if (selectedCreatorFilter) {
801
- builder.addFilter(
802
- 'firstCreator',
803
- selectedCreatorFilter,
804
- FilterConstraint.INCLUDE,
805
- );
806
- }
807
-
808
- const filterMap = builder.build();
809
- return filterMap;
810
- }
811
-
812
- /**
813
- * Produces a compact unique ID for a search request that can help with debugging
814
- * on the backend by making related requests easier to trace through different services.
815
- * (e.g., tying the hits/aggregations requests for the same page back to a single hash).
816
- *
817
- * @param params The search service parameters for the request
818
- * @param kind The kind of request (hits-only, aggregations-only, or both)
819
- * @returns A Promise resolving to the uid to apply to the request
820
- */
821
- async requestUID(params: SearchParams, kind: RequestKind): Promise<string> {
822
- const paramsToHash = JSON.stringify({
823
- pageType: params.pageType,
824
- pageTarget: params.pageTarget,
825
- query: params.query,
826
- fields: params.fields,
827
- filters: params.filters,
828
- sort: params.sort,
829
- searchType: this.host.searchType,
830
- });
831
-
832
- const fullQueryHash = (await sha1(paramsToHash)).slice(0, 20); // First 80 bits of SHA-1 are plenty for this
833
- const sessionId = (await this.host.getSessionId()).slice(0, 20); // Likewise
834
- const page = params.page ?? 0;
835
- const kindPrefix = kind.charAt(0); // f = full, h = hits, a = aggregations
836
- const currentTime = Date.now();
837
-
838
- return `R:${fullQueryHash}-S:${sessionId}-P:${page}-K:${kindPrefix}-T:${currentTime}`;
839
- }
840
-
841
- /**
842
- * @inheritdoc
843
- */
844
- get pageSpecifierParams(): PageSpecifierParams | null {
845
- if (this.host.identifiers?.length) {
846
- return {
847
- pageType: 'client_document_fetch',
848
- };
849
- }
850
- if (this.host.withinCollection) {
851
- return {
852
- pageType: 'collection_details',
853
- pageTarget: this.host.withinCollection,
854
- };
855
- }
856
- if (this.host.withinProfile) {
857
- return {
858
- pageType: 'account_details',
859
- pageTarget: this.host.withinProfile,
860
- pageElements: this.host.profileElement
861
- ? [this.host.profileElement]
862
- : [],
863
- };
864
- }
865
- return null;
866
- }
867
-
868
- /**
869
- * The full query, including year facets and date range clauses
870
- */
871
- private get fullQuery(): string | undefined {
872
- const parts = [];
873
- const trimmedQuery = this.host.baseQuery?.trim();
874
- if (trimmedQuery) parts.push(trimmedQuery);
875
-
876
- if (this.host.identifiers) {
877
- parts.push(`identifier:(${this.host.identifiers.join(' OR ')})`);
878
- }
879
-
880
- const { facetQuery, dateRangeQueryClause, sortFilterQueries } = this;
881
- if (facetQuery) parts.push(facetQuery);
882
- if (dateRangeQueryClause) parts.push(dateRangeQueryClause);
883
- if (sortFilterQueries) parts.push(sortFilterQueries);
884
-
885
- return parts.join(' AND ').trim();
886
- }
887
-
888
- /**
889
- * Generates a query string representing the current set of applied facets
890
- *
891
- * Example: `mediatype:("collection" OR "audio" OR -"etree") AND year:("2000" OR "2001")`
892
- */
893
- private get facetQuery(): string | undefined {
894
- if (!this.host.selectedFacets) return undefined;
895
- const facetClauses = [];
896
- for (const [facetName, facetValues] of Object.entries(
897
- this.host.selectedFacets,
898
- )) {
899
- facetClauses.push(this.buildFacetClause(facetName, facetValues));
900
- }
901
- return this.joinFacetClauses(facetClauses)?.trim();
902
- }
903
-
904
- private get dateRangeQueryClause(): string | undefined {
905
- if (!this.host.minSelectedDate || !this.host.maxSelectedDate) {
906
- return undefined;
907
- }
908
-
909
- return `year:[${this.host.minSelectedDate} TO ${this.host.maxSelectedDate}]`;
910
- }
911
-
912
- private get sortFilterQueries(): string {
913
- const queries = [this.titleQuery, this.creatorQuery];
914
- return queries.filter(q => q).join(' AND ');
915
- }
916
-
917
- /**
918
- * Returns a query clause identifying the currently selected title filter,
919
- * e.g., `firstTitle:X`.
920
- */
921
- private get titleQuery(): string | undefined {
922
- return this.host.selectedTitleFilter
923
- ? `firstTitle:${this.host.selectedTitleFilter}`
924
- : undefined;
925
- }
926
-
927
- /**
928
- * Returns a query clause identifying the currently selected creator filter,
929
- * e.g., `firstCreator:X`.
930
- */
931
- private get creatorQuery(): string | undefined {
932
- return this.host.selectedCreatorFilter
933
- ? `firstCreator:${this.host.selectedCreatorFilter}`
934
- : undefined;
935
- }
936
-
937
- /**
938
- * Builds an OR-joined facet clause for the given facet name and values.
939
- *
940
- * E.g., for name `subject` and values
941
- * `{ foo: { state: 'selected' }, bar: { state: 'hidden' } }`
942
- * this will produce the clause
943
- * `subject:("foo" OR -"bar")`.
944
- *
945
- * @param facetName The facet type (e.g., 'collection')
946
- * @param facetValues The facet buckets, mapped by their keys
947
- */
948
- private buildFacetClause(
949
- facetName: string,
950
- facetValues: Record<string, FacetBucket>,
951
- ): string {
952
- const { name: facetQueryName, values } = this.prepareFacetForFetch(
953
- facetName,
954
- facetValues,
955
- );
956
- const facetEntries = Object.entries(values);
957
- if (facetEntries.length === 0) return '';
958
-
959
- const facetValuesArray: string[] = [];
960
- for (const [key, facetData] of facetEntries) {
961
- const plusMinusPrefix = facetData.state === 'hidden' ? '-' : '';
962
- facetValuesArray.push(`${plusMinusPrefix}"${key}"`);
963
- }
964
-
965
- const valueQuery = facetValuesArray.join(` OR `);
966
- return `${facetQueryName}:(${valueQuery})`;
967
- }
968
-
969
- /**
970
- * Handles some special pre-request normalization steps for certain facet types
971
- * that require them.
972
- *
973
- * @param facetName The name of the facet type (e.g., 'language')
974
- * @param facetValues An array of values for that facet type
975
- */
976
- private prepareFacetForFetch(
977
- facetName: string,
978
- facetValues: Record<string, FacetBucket>,
979
- ): { name: string; values: Record<string, FacetBucket> } {
980
- // eslint-disable-next-line prefer-const
981
- let [normalizedName, normalizedValues] = [facetName, facetValues];
982
-
983
- // The full "search engine" name of the lending field is "lending___status"
984
- if (facetName === 'lending') {
985
- normalizedName = 'lending___status';
986
- }
987
-
988
- return {
989
- name: normalizedName,
990
- values: normalizedValues,
991
- };
992
- }
993
-
994
- /**
995
- * Takes an array of facet clauses, and combines them into a
996
- * full AND-joined facet query string. Empty clauses are ignored.
997
- */
998
- private joinFacetClauses(facetClauses: string[]): string | undefined {
999
- const nonEmptyFacetClauses = facetClauses.filter(
1000
- clause => clause.length > 0,
1001
- );
1002
- return nonEmptyFacetClauses.length > 0
1003
- ? `(${nonEmptyFacetClauses.join(' AND ')})`
1004
- : undefined;
1005
- }
1006
-
1007
- /**
1008
- * Fires a backend request to fetch a set of aggregations (representing UI facets) for
1009
- * the current search state.
1010
- */
1011
- private async fetchFacets(): Promise<void> {
1012
- const trimmedQuery = this.host.baseQuery?.trim();
1013
- if (!this.canPerformSearch) return;
1014
-
1015
- const { facetFetchQueryKey } = this;
1016
- if (this.fetchesInProgress.has(facetFetchQueryKey)) return;
1017
- this.fetchesInProgress.add(facetFetchQueryKey);
1018
-
1019
- this.setFacetsLoading(true);
1020
-
1021
- const sortParams = this.host.sortParam ? [this.host.sortParam] : [];
1022
- const params: SearchParams = {
1023
- ...this.pageSpecifierParams,
1024
- query: trimmedQuery || '',
1025
- identifiers: this.host.identifiers,
1026
- rows: 0,
1027
- filters: this.filterMap,
1028
- // Fetch a few extra buckets beyond the 6 we show, in case some get suppressed
1029
- aggregationsSize: 10,
1030
- // Note: we don't need an aggregations param to fetch the default aggregations from the PPS.
1031
- // The default aggregations for the search_results page type should be what we need here.
1032
- };
1033
- params.uid = await this.requestUID(
1034
- { ...params, sort: sortParams },
1035
- 'aggregations',
1036
- );
1037
-
1038
- const searchResponse = await this.host.searchService?.search(
1039
- params,
1040
- this.effectiveSearchType,
1041
- );
1042
- const success = searchResponse?.success;
1043
-
1044
- // This is checking to see if the query has changed since the data was fetched.
1045
- // If so, we just want to discard this set of aggregations because they are
1046
- // likely no longer valid for the newer query.
1047
- const queryChangedSinceFetch =
1048
- !this.fetchesInProgress.has(facetFetchQueryKey);
1049
- this.fetchesInProgress.delete(facetFetchQueryKey);
1050
- if (queryChangedSinceFetch) return;
1051
-
1052
- if (!success) {
1053
- const errorMsg = searchResponse?.error?.message;
1054
- const detailMsg = searchResponse?.error?.details?.message;
1055
-
1056
- if (!errorMsg && !detailMsg) {
1057
- // @ts-expect-error: Property 'Sentry' does not exist on type 'Window & typeof globalThis'
1058
- window?.Sentry?.captureMessage?.(
1059
- 'Missing or malformed facet response from backend',
1060
- 'error',
1061
- );
1062
- }
1063
-
1064
- this.setFacetsLoading(false);
1065
- return;
1066
- }
1067
-
1068
- const { aggregations, collectionTitles, tvChannelAliases } =
1069
- success.response;
1070
- this.aggregations = aggregations;
1071
-
1072
- this.histogramAggregation =
1073
- this.host.searchType === SearchType.TV
1074
- ? aggregations?.date_histogram
1075
- : aggregations?.year_histogram;
1076
-
1077
- if (collectionTitles) {
1078
- for (const [id, title] of Object.entries(collectionTitles)) {
1079
- this.collectionTitles.set(id, title);
1080
- }
1081
- }
1082
- if (tvChannelAliases) {
1083
- for (const [channel, network] of Object.entries(tvChannelAliases)) {
1084
- this.tvChannelAliases.set(channel, network);
1085
- }
1086
- }
1087
-
1088
- this.setFacetsLoading(false);
1089
- this.requestHostUpdate();
1090
- }
1091
-
1092
- /**
1093
- * Performs the initial page fetch(es) for the current search state.
1094
- */
1095
- private async doInitialPageFetch(): Promise<void> {
1096
- this.setSearchResultsLoading(true);
1097
- // Try to batch 2 initial page requests when possible
1098
- await this.fetchPage(this.host.initialPageNumber, this.numInitialPages);
1099
- }
1100
-
1101
- /**
1102
- * Fetches one or more pages of results and updates the data source.
1103
- *
1104
- * @param pageNumber The page number to fetch
1105
- * @param numInitialPages If this is an initial page fetch (`pageNumber = 1`),
1106
- * specifies how many pages to batch together in one request. Ignored
1107
- * if `pageNumber != 1`, defaulting to a single page.
1108
- */
1109
- async fetchPage(pageNumber: number, numInitialPages = 1): Promise<void> {
1110
- const trimmedQuery = this.host.baseQuery?.trim();
1111
- // reset loading status
1112
- if (!this.canPerformSearch) {
1113
- this.setSearchResultsLoading(false);
1114
- return;
1115
- }
1116
-
1117
- // if we already have data, don't fetch again
1118
- if (this.hasPage(pageNumber)) return;
1119
-
1120
- if (this.endOfDataReached) return;
1121
-
1122
- // Batch multiple initial page requests together if needed (e.g., can request
1123
- // pages 1 and 2 together in a single request).
1124
- let numPages = pageNumber === 1 ? numInitialPages : 1;
1125
- const numRows = this.pageSize * numPages;
1126
-
1127
- // if a fetch is already in progress for this query and page, don't fetch again
1128
- const { pageFetchQueryKey } = this;
1129
- const currentPageKey = `${pageFetchQueryKey}-p:${pageNumber}`;
1130
- if (this.fetchesInProgress.has(currentPageKey)) return;
1131
-
1132
- for (let i = 0; i < numPages; i += 1) {
1133
- this.fetchesInProgress.add(`${pageFetchQueryKey}-p:${pageNumber + i}`);
1134
- }
1135
- this.previousQueryKey = pageFetchQueryKey;
1136
-
1137
- const { withinCollection, withinProfile } = this.host;
1138
-
1139
- let sortParams = this.host.sortParam ? [this.host.sortParam] : [];
1140
- // TODO eventually the PPS should handle these defaults natively
1141
- const isDefaultSort = this.host.selectedSort === SortField.default;
1142
- const isTVSearch = this.host.searchType === SearchType.TV;
1143
- const isDefaultTVSort = isTVSearch && isDefaultSort;
1144
- const isDefaultProfileSort = withinProfile && isDefaultSort;
1145
- if (
1146
- (isDefaultProfileSort || isDefaultTVSort) &&
1147
- this.host.defaultSortField
1148
- ) {
1149
- const sortOption = SORT_OPTIONS[this.host.defaultSortField];
1150
- const { searchServiceKey, handledBySearchService } = sortOption;
1151
- if (searchServiceKey && handledBySearchService) {
1152
- sortParams = [
1153
- {
1154
- field: searchServiceKey,
1155
- direction: this.host.defaultSortDirection ?? 'desc',
1156
- },
1157
- ];
1158
- }
1159
- }
1160
-
1161
- const params: SearchParams = {
1162
- ...this.pageSpecifierParams,
1163
- query: trimmedQuery || '',
1164
- identifiers: this.host.identifiers,
1165
- page: pageNumber,
1166
- rows: numRows,
1167
- sort: sortParams,
1168
- filters: this.filterMap,
1169
- aggregations: { omit: true },
1170
- };
1171
- params.uid = await this.requestUID(params, 'hits');
1172
-
1173
- // log('=== FIRING PAGE REQUEST ===', params);
1174
- const searchResponse = await this.host.searchService?.search(
1175
- params,
1176
- this.effectiveSearchType,
1177
- );
1178
- // log('=== RECEIVED PAGE RESPONSE IN CB ===', searchResponse);
1179
- const success = searchResponse?.success;
1180
-
1181
- // This is checking to see if the fetch has been invalidated since it was fired off.
1182
- // If so, we just want to discard the response since it is for an obsolete query state.
1183
- if (!this.fetchesInProgress.has(currentPageKey)) return;
1184
- for (let i = 0; i < numPages; i += 1) {
1185
- this.fetchesInProgress.delete(`${pageFetchQueryKey}-p:${pageNumber + i}`);
1186
- }
1187
-
1188
- if (!success) {
1189
- const errorMsg = searchResponse?.error?.message;
1190
- const detailMsg = searchResponse?.error?.details?.message;
1191
-
1192
- this.queryErrorMessage = `${errorMsg ?? ''}${
1193
- detailMsg ? `; ${detailMsg}` : ''
1194
- }`;
1195
-
1196
- if (!this.queryErrorMessage) {
1197
- this.queryErrorMessage = 'Missing or malformed response from backend';
1198
- // @ts-expect-error: Property 'Sentry' does not exist on type 'Window & typeof globalThis'
1199
- window?.Sentry?.captureMessage?.(this.queryErrorMessage, 'error');
1200
- }
1201
-
1202
- this.setSearchResultsLoading(false);
1203
- this.requestHostUpdate();
1204
- this.host.emitSearchError();
1205
- return;
1206
- }
1207
-
1208
- this.setTotalResultCount(success.response.totalResults - this.offset);
1209
- if (this.activeOnHost && this.totalResults === 0) {
1210
- // display event to offshoot when result count is zero.
1211
- this.host.emitEmptyResults();
1212
- }
1213
-
1214
- this.sessionContext = success.sessionContext;
1215
- if (withinCollection) {
1216
- this.collectionExtraInfo = success.response.collectionExtraInfo;
1217
-
1218
- // For collections, we want the UI to respect the default sort option
1219
- // which can be specified in metadata, or otherwise assumed to be week:desc
1220
- if (this.activeOnHost) {
1221
- this.host.applyDefaultCollectionSort(this.collectionExtraInfo);
1222
- }
1223
-
1224
- if (this.collectionExtraInfo) {
1225
- this.parentCollections = [].concat(
1226
- this.collectionExtraInfo.public_metadata?.collection ?? [],
1227
- );
1228
-
1229
- // Update the TV collection status now that we know the parent collections
1230
- this.host.isTVCollection =
1231
- this.host.withinCollection?.startsWith('TV-') ||
1232
- this.host.withinCollection === 'tvnews' ||
1233
- this.host.withinCollection === 'tvarchive' ||
1234
- this.parentCollections.includes('tvnews') ||
1235
- this.parentCollections.includes('tvarchive');
1236
- }
1237
- } else if (withinProfile) {
1238
- this.accountExtraInfo = success.response.accountExtraInfo;
1239
- this.pageElements = success.response.pageElements;
1240
- }
1241
-
1242
- const { results, collectionTitles, tvChannelAliases } = success.response;
1243
- if (results && results.length > 0) {
1244
- // Load any collection titles present on the response into the cache,
1245
- // or queue up preload fetches for them if none were present.
1246
- if (collectionTitles) {
1247
- for (const [id, title] of Object.entries(collectionTitles)) {
1248
- this.collectionTitles.set(id, title);
1249
- }
1250
-
1251
- // Also add the target collection's title if available
1252
- const targetTitle = this.collectionExtraInfo?.public_metadata?.title;
1253
- if (withinCollection && targetTitle) {
1254
- this.collectionTitles.set(withinCollection, targetTitle);
1255
- }
1256
- }
1257
-
1258
- if (tvChannelAliases) {
1259
- for (const [channel, network] of Object.entries(tvChannelAliases)) {
1260
- this.tvChannelAliases.set(channel, network);
1261
- }
1262
- }
1263
-
1264
- // Update the data source for each returned page.
1265
- // For loans and web archives, we must account for receiving more pages than we asked for.
1266
- const isUnpagedElement = ['lending', 'web_archives'].includes(
1267
- this.host.profileElement!,
1268
- );
1269
- if (isUnpagedElement) {
1270
- numPages = Math.ceil(results.length / this.pageSize);
1271
- this.endOfDataReached = true;
1272
- if (this.activeOnHost) this.host.setTileCount(this.totalResults);
1273
- }
1274
-
1275
- for (let i = 0; i < numPages; i += 1) {
1276
- const pageStartIndex = this.pageSize * i;
1277
- this.addFetchedResultsToDataSource(
1278
- pageNumber + i,
1279
- results.slice(pageStartIndex, pageStartIndex + this.pageSize),
1280
- !isUnpagedElement || i === numPages - 1,
1281
- );
1282
- }
1283
- }
1284
-
1285
- // When we reach the end of the data, we can set the infinite scroller's
1286
- // item count to the real total number of results (rather than the
1287
- // temporary estimates based on pages rendered so far).
1288
- if (this.size >= this.totalResults || results.length === 0) {
1289
- this.endOfDataReached = true;
1290
- if (this.activeOnHost) this.host.setTileCount(this.size);
1291
- }
1292
-
1293
- this.setSearchResultsLoading(false);
1294
- this.requestHostUpdate();
1295
- }
1296
-
1297
- /**
1298
- * Returns the type of request that produced the current set of hits,
1299
- * based on the presence of a search query or profile/collection target
1300
- * on the host.
1301
- */
1302
- private get hitRequestSource(): HitRequestSource {
1303
- const { host } = this;
1304
- if (host.baseQuery) return 'search_query';
1305
- if (host.withinProfile) return 'profile_tab';
1306
- if (host.withinCollection) return 'collection_members';
1307
- return 'unknown';
1308
- }
1309
-
1310
- /**
1311
- * Update the datasource from the fetch response
1312
- *
1313
- * @param pageNumber
1314
- * @param results
1315
- */
1316
- private addFetchedResultsToDataSource(
1317
- pageNumber: number,
1318
- results: SearchResult[],
1319
- needsReload = true,
1320
- ): void {
1321
- const tiles: TileModel[] = [];
1322
- const requestSource = this.hitRequestSource;
1323
- results?.forEach(result => {
1324
- if (!result.identifier) return;
1325
- tiles.push(new TileModel(result, requestSource));
1326
- });
1327
-
1328
- this.addPage(pageNumber, tiles);
1329
-
1330
- if (needsReload) {
1331
- this.refreshVisibleResults();
1332
- }
1333
- }
1334
-
1335
- /**
1336
- * Fetches the aggregation buckets for the given prefix filter type.
1337
- */
1338
- private async fetchPrefixFilterBuckets(
1339
- filterType: PrefixFilterType,
1340
- ): Promise<Bucket[]> {
1341
- const trimmedQuery = this.host.baseQuery?.trim();
1342
- if (!this.canPerformSearch) return [];
1343
-
1344
- const filterAggregationKey = prefixFilterAggregationKeys[filterType];
1345
- const sortParams = this.host.sortParam ? [this.host.sortParam] : [];
1346
-
1347
- const params: SearchParams = {
1348
- ...this.pageSpecifierParams,
1349
- query: trimmedQuery || '',
1350
- identifiers: this.host.identifiers,
1351
- rows: 0,
1352
- filters: this.filterMap,
1353
- // Only fetch the firstTitle or firstCreator aggregation
1354
- aggregations: { simpleParams: [filterAggregationKey] },
1355
- // Fetch all 26 letter buckets
1356
- aggregationsSize: 26,
1357
- };
1358
- params.uid = await this.requestUID(
1359
- { ...params, sort: sortParams },
1360
- 'aggregations',
1361
- );
1362
-
1363
- const searchResponse = await this.host.searchService?.search(
1364
- params,
1365
- this.effectiveSearchType,
1366
- );
1367
-
1368
- return (searchResponse?.success?.response?.aggregations?.[
1369
- filterAggregationKey
1370
- ]?.buckets ?? []) as Bucket[];
1371
- }
1372
-
1373
- /**
1374
- * Fetches and caches the prefix filter counts for the given filter type.
1375
- */
1376
- async updatePrefixFilterCounts(filterType: PrefixFilterType): Promise<void> {
1377
- const { facetFetchQueryKey } = this;
1378
- const buckets = await this.fetchPrefixFilterBuckets(filterType);
1379
-
1380
- // Don't update the filter counts for an outdated query (if it has been changed
1381
- // since we sent the request)
1382
- const queryChangedSinceFetch =
1383
- facetFetchQueryKey !== this.facetFetchQueryKey;
1384
- if (queryChangedSinceFetch) return;
1385
-
1386
- // Unpack the aggregation buckets into a simple map like { 'A': 50, 'B': 25, ... }
1387
- this.prefixFilterCountMap = { ...this.prefixFilterCountMap }; // Clone the object to trigger an update
1388
- this.prefixFilterCountMap[filterType] = buckets.reduce(
1389
- (acc: Record<string, number>, bucket: Bucket) => {
1390
- acc[(bucket.key as string).toUpperCase()] = bucket.doc_count;
1391
- return acc;
1392
- },
1393
- {},
1394
- );
1395
-
1396
- this.requestHostUpdate();
1397
- }
1398
-
1399
- /**
1400
- * @inheritdoc
1401
- */
1402
- async updatePrefixFiltersForCurrentSort(): Promise<void> {
1403
- if (['title', 'creator'].includes(this.host.selectedSort as SortField)) {
1404
- const filterType = this.host.selectedSort as PrefixFilterType;
1405
- if (!this.prefixFilterCountMap[filterType]) {
1406
- this.updatePrefixFilterCounts(filterType);
1407
- }
1408
- }
1409
- }
1410
-
1411
- /**
1412
- * @inheritdoc
1413
- */
1414
- refreshLetterCounts(): void {
1415
- if (Object.keys(this.prefixFilterCountMap).length > 0) {
1416
- this.prefixFilterCountMap = {};
1417
- }
1418
- this.updatePrefixFiltersForCurrentSort();
1419
- this.requestHostUpdate();
1420
- }
1421
-
1422
- /**
1423
- * @inheritdoc
1424
- */
1425
- populateTVChannelMaps(): Promise<TVChannelMaps> {
1426
- // To ensure that we only make these requests once, cache the Promise returned by the
1427
- // first call, and return the same Promise on repeated calls.
1428
- // Resolves once both maps have been retrieved and saved in the data source.
1429
- if (!this._tvMapsPromise) {
1430
- this._tvMapsPromise = this._fetchTVChannelMaps();
1431
- }
1432
-
1433
- return this._tvMapsPromise;
1434
- }
1435
-
1436
- /**
1437
- * Internal function implementing the actual fetches for TV channel mappings.
1438
- * This should only called by the public populateTVChannelMaps method, which is guarded so
1439
- * that we do not make extra requests for these rather large mappings.
1440
- */
1441
- private async _fetchTVChannelMaps(): Promise<TVChannelMaps> {
1442
- const baseURL = 'https://av.archive.org/etc';
1443
- const dateStr = new Date().toISOString().slice(0, 10); // YYYY-MM-DD
1444
- const chan2networkPromise = fetch(
1445
- `${baseURL}/chan2network.json?date=${dateStr}`,
1446
- );
1447
- const program2chansPromise = fetch(
1448
- `${baseURL}/program2chans.json?date=${dateStr}`,
1449
- );
1450
-
1451
- const [chan2networkResponse, program2chansResponse] = await Promise.all([
1452
- chan2networkPromise,
1453
- program2chansPromise,
1454
- ]);
1455
- this.tvChannelMaps.channelToNetwork = new Map(
1456
- Object.entries(await chan2networkResponse.json()),
1457
- );
1458
- this.tvChannelMaps.programToChannels = new Map(
1459
- Object.entries(await program2chansResponse.json()),
1460
- );
1461
-
1462
- this.requestHostUpdate();
1463
- return this.tvChannelMaps;
1464
- }
1465
- }
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
+ * Returns the search type to actually use for fetches.
608
+ * When within a collection with an empty query and FTS selected,
609
+ * falls back to metadata search since FTS requires a non-empty query
610
+ * and we'd still like to show the unfiltered collection in that case.
611
+ */
612
+ private get effectiveSearchType(): SearchType {
613
+ const trimmedQuery = this.host.baseQuery?.trim();
614
+
615
+ const shouldUseMetadataSearch =
616
+ !trimmedQuery &&
617
+ this.host.withinCollection &&
618
+ this.host.searchType === SearchType.FULLTEXT;
619
+ if (shouldUseMetadataSearch) return SearchType.METADATA;
620
+
621
+ return this.host.searchType;
622
+ }
623
+
624
+ /**
625
+ * @inheritdoc
626
+ */
627
+ get canPerformSearch(): boolean {
628
+ if (!this.host.searchService) return false;
629
+
630
+ const trimmedQuery = this.host.baseQuery?.trim();
631
+ const hasNonEmptyQuery = !!trimmedQuery;
632
+ const hasIdentifiers = !!this.host.identifiers?.length;
633
+ const isCollectionSearch = !!this.host.withinCollection;
634
+ const isProfileSearch = !!this.host.withinProfile;
635
+ const hasProfileElement = !!this.host.profileElement;
636
+
637
+ const searchType = this.effectiveSearchType;
638
+ const isDefaultedSearch = searchType === SearchType.DEFAULT;
639
+ const isMetadataSearch = searchType === SearchType.METADATA;
640
+ const isTvSearch = searchType === SearchType.TV;
641
+
642
+ // Metadata/tv searches within a collection are allowed to have no query.
643
+ const isValidForCollectionSearch =
644
+ isDefaultedSearch || isMetadataSearch || isTvSearch;
645
+
646
+ // Searches within a profile page may also be performed without a query, provided the profile element is set.
647
+ const isValidForProfileSearch =
648
+ hasProfileElement && (isDefaultedSearch || isMetadataSearch);
649
+
650
+ // Otherwise, a non-empty query must be set.
651
+ return (
652
+ hasNonEmptyQuery ||
653
+ hasIdentifiers ||
654
+ (isCollectionSearch && isValidForCollectionSearch) ||
655
+ (isProfileSearch && isValidForProfileSearch)
656
+ );
657
+ }
658
+
659
+ /**
660
+ * Sets the state for whether the initial set of search results for the
661
+ * current query is loading
662
+ */
663
+ private setSearchResultsLoading(loading: boolean): void {
664
+ this.searchResultsLoading = loading;
665
+ if (this.activeOnHost) {
666
+ this.host.setSearchResultsLoading(loading);
667
+ }
668
+ }
669
+
670
+ /**
671
+ * Sets the state for whether the facets for a query is loading
672
+ */
673
+ private setFacetsLoading(loading: boolean): void {
674
+ this.facetsLoading = loading;
675
+ if (this.activeOnHost) {
676
+ this.host.setFacetsLoading(loading);
677
+ }
678
+ }
679
+
680
+ /**
681
+ * Requests that the host perform an update, provided this data
682
+ * source is actively installed on it.
683
+ */
684
+ private requestHostUpdate(): void {
685
+ if (this.activeOnHost) {
686
+ this.host.requestUpdate();
687
+ }
688
+ }
689
+
690
+ /**
691
+ * Requests that the host refresh its visible tiles, provided this
692
+ * data source is actively installed on it.
693
+ */
694
+ private refreshVisibleResults(): void {
695
+ if (this.activeOnHost) {
696
+ this.host.refreshVisibleResults();
697
+ }
698
+ }
699
+
700
+ /**
701
+ * The query key is a string that uniquely identifies the current search.
702
+ * It consists of:
703
+ * - The current base query
704
+ * - The current collection/profile target & page element
705
+ * - The current search type
706
+ * - Any currently-applied facets
707
+ * - Any currently-applied date range
708
+ * - Any currently-applied prefix filters
709
+ * - The current sort options
710
+ *
711
+ * This lets us internally keep track of queries so we don't persist data that's
712
+ * no longer relevant. Not meant to be human-readable.
713
+ */
714
+ get pageFetchQueryKey(): string {
715
+ const profileKey = `pf;${this.host.withinProfile}--pe;${this.host.profileElement}`;
716
+ const pageTarget = this.host.withinCollection ?? profileKey;
717
+ const sortField = this.host.selectedSort ?? 'none';
718
+ const sortDirection = this.host.sortDirection ?? 'none';
719
+ return `fq:${this.fullQuery}-pt:${pageTarget}-st:${this.effectiveSearchType}-sf:${sortField}-sd:${sortDirection}`;
720
+ }
721
+
722
+ /**
723
+ * Similar to `pageFetchQueryKey` above, but excludes sort fields since they
724
+ * are not relevant in determining aggregation queries.
725
+ */
726
+ get facetFetchQueryKey(): string {
727
+ const profileKey = `pf;${this.host.withinProfile}--pe;${this.host.profileElement}`;
728
+ const pageTarget = this.host.withinCollection ?? profileKey;
729
+ return `facets-fq:${this.fullQuery}-pt:${pageTarget}-st:${this.effectiveSearchType}`;
730
+ }
731
+
732
+ /**
733
+ * Constructs a search service FilterMap object from the combination of
734
+ * all the currently-applied filters. This includes any facets, letter
735
+ * filters, and date range.
736
+ */
737
+ get filterMap(): FilterMap {
738
+ const builder = new FilterMapBuilder();
739
+
740
+ const {
741
+ minSelectedDate,
742
+ maxSelectedDate,
743
+ selectedFacets,
744
+ internalFilters,
745
+ selectedTitleFilter,
746
+ selectedCreatorFilter,
747
+ } = this.host;
748
+
749
+ const dateField = this.host.searchType === SearchType.TV ? 'date' : 'year';
750
+
751
+ if (minSelectedDate) {
752
+ builder.addFilter(
753
+ dateField,
754
+ minSelectedDate,
755
+ FilterConstraint.GREATER_OR_EQUAL,
756
+ );
757
+ }
758
+ if (maxSelectedDate) {
759
+ builder.addFilter(
760
+ dateField,
761
+ maxSelectedDate,
762
+ FilterConstraint.LESS_OR_EQUAL,
763
+ );
764
+ }
765
+
766
+ // Add any selected facets and internal filters
767
+ const combinedFilters = mergeSelectedFacets(
768
+ internalFilters,
769
+ selectedFacets,
770
+ );
771
+ if (combinedFilters) {
772
+ for (const [facetName, facetValues] of Object.entries(combinedFilters)) {
773
+ const { name, values } = this.prepareFacetForFetch(
774
+ facetName,
775
+ facetValues,
776
+ );
777
+ for (const [value, bucket] of Object.entries(values)) {
778
+ let constraint;
779
+ if (bucket.state === 'selected') {
780
+ constraint = FilterConstraint.INCLUDE;
781
+ } else if (bucket.state === 'hidden') {
782
+ constraint = FilterConstraint.EXCLUDE;
783
+ }
784
+
785
+ if (constraint) {
786
+ builder.addFilter(name, value, constraint);
787
+ }
788
+ }
789
+ }
790
+ }
791
+
792
+ // Add any letter filters
793
+ if (selectedTitleFilter) {
794
+ builder.addFilter(
795
+ 'firstTitle',
796
+ selectedTitleFilter,
797
+ FilterConstraint.INCLUDE,
798
+ );
799
+ }
800
+ if (selectedCreatorFilter) {
801
+ builder.addFilter(
802
+ 'firstCreator',
803
+ selectedCreatorFilter,
804
+ FilterConstraint.INCLUDE,
805
+ );
806
+ }
807
+
808
+ const filterMap = builder.build();
809
+ return filterMap;
810
+ }
811
+
812
+ /**
813
+ * Produces a compact unique ID for a search request that can help with debugging
814
+ * on the backend by making related requests easier to trace through different services.
815
+ * (e.g., tying the hits/aggregations requests for the same page back to a single hash).
816
+ *
817
+ * @param params The search service parameters for the request
818
+ * @param kind The kind of request (hits-only, aggregations-only, or both)
819
+ * @returns A Promise resolving to the uid to apply to the request
820
+ */
821
+ async requestUID(params: SearchParams, kind: RequestKind): Promise<string> {
822
+ const paramsToHash = JSON.stringify({
823
+ pageType: params.pageType,
824
+ pageTarget: params.pageTarget,
825
+ query: params.query,
826
+ fields: params.fields,
827
+ filters: params.filters,
828
+ sort: params.sort,
829
+ searchType: this.host.searchType,
830
+ });
831
+
832
+ const fullQueryHash = (await sha1(paramsToHash)).slice(0, 20); // First 80 bits of SHA-1 are plenty for this
833
+ const sessionId = (await this.host.getSessionId()).slice(0, 20); // Likewise
834
+ const page = params.page ?? 0;
835
+ const kindPrefix = kind.charAt(0); // f = full, h = hits, a = aggregations
836
+ const currentTime = Date.now();
837
+
838
+ return `R:${fullQueryHash}-S:${sessionId}-P:${page}-K:${kindPrefix}-T:${currentTime}`;
839
+ }
840
+
841
+ /**
842
+ * @inheritdoc
843
+ */
844
+ get pageSpecifierParams(): PageSpecifierParams | null {
845
+ if (this.host.identifiers?.length) {
846
+ return {
847
+ pageType: 'client_document_fetch',
848
+ };
849
+ }
850
+ if (this.host.withinCollection) {
851
+ return {
852
+ pageType: 'collection_details',
853
+ pageTarget: this.host.withinCollection,
854
+ };
855
+ }
856
+ if (this.host.withinProfile) {
857
+ return {
858
+ pageType: 'account_details',
859
+ pageTarget: this.host.withinProfile,
860
+ pageElements: this.host.profileElement
861
+ ? [this.host.profileElement]
862
+ : [],
863
+ };
864
+ }
865
+ return null;
866
+ }
867
+
868
+ /**
869
+ * The full query, including year facets and date range clauses
870
+ */
871
+ private get fullQuery(): string | undefined {
872
+ const parts = [];
873
+ const trimmedQuery = this.host.baseQuery?.trim();
874
+ if (trimmedQuery) parts.push(trimmedQuery);
875
+
876
+ if (this.host.identifiers) {
877
+ parts.push(`identifier:(${this.host.identifiers.join(' OR ')})`);
878
+ }
879
+
880
+ const { facetQuery, dateRangeQueryClause, sortFilterQueries } = this;
881
+ if (facetQuery) parts.push(facetQuery);
882
+ if (dateRangeQueryClause) parts.push(dateRangeQueryClause);
883
+ if (sortFilterQueries) parts.push(sortFilterQueries);
884
+
885
+ return parts.join(' AND ').trim();
886
+ }
887
+
888
+ /**
889
+ * Generates a query string representing the current set of applied facets
890
+ *
891
+ * Example: `mediatype:("collection" OR "audio" OR -"etree") AND year:("2000" OR "2001")`
892
+ */
893
+ private get facetQuery(): string | undefined {
894
+ if (!this.host.selectedFacets) return undefined;
895
+ const facetClauses = [];
896
+ for (const [facetName, facetValues] of Object.entries(
897
+ this.host.selectedFacets,
898
+ )) {
899
+ facetClauses.push(this.buildFacetClause(facetName, facetValues));
900
+ }
901
+ return this.joinFacetClauses(facetClauses)?.trim();
902
+ }
903
+
904
+ private get dateRangeQueryClause(): string | undefined {
905
+ if (!this.host.minSelectedDate || !this.host.maxSelectedDate) {
906
+ return undefined;
907
+ }
908
+
909
+ return `year:[${this.host.minSelectedDate} TO ${this.host.maxSelectedDate}]`;
910
+ }
911
+
912
+ private get sortFilterQueries(): string {
913
+ const queries = [this.titleQuery, this.creatorQuery];
914
+ return queries.filter(q => q).join(' AND ');
915
+ }
916
+
917
+ /**
918
+ * Returns a query clause identifying the currently selected title filter,
919
+ * e.g., `firstTitle:X`.
920
+ */
921
+ private get titleQuery(): string | undefined {
922
+ return this.host.selectedTitleFilter
923
+ ? `firstTitle:${this.host.selectedTitleFilter}`
924
+ : undefined;
925
+ }
926
+
927
+ /**
928
+ * Returns a query clause identifying the currently selected creator filter,
929
+ * e.g., `firstCreator:X`.
930
+ */
931
+ private get creatorQuery(): string | undefined {
932
+ return this.host.selectedCreatorFilter
933
+ ? `firstCreator:${this.host.selectedCreatorFilter}`
934
+ : undefined;
935
+ }
936
+
937
+ /**
938
+ * Builds an OR-joined facet clause for the given facet name and values.
939
+ *
940
+ * E.g., for name `subject` and values
941
+ * `{ foo: { state: 'selected' }, bar: { state: 'hidden' } }`
942
+ * this will produce the clause
943
+ * `subject:("foo" OR -"bar")`.
944
+ *
945
+ * @param facetName The facet type (e.g., 'collection')
946
+ * @param facetValues The facet buckets, mapped by their keys
947
+ */
948
+ private buildFacetClause(
949
+ facetName: string,
950
+ facetValues: Record<string, FacetBucket>,
951
+ ): string {
952
+ const { name: facetQueryName, values } = this.prepareFacetForFetch(
953
+ facetName,
954
+ facetValues,
955
+ );
956
+ const facetEntries = Object.entries(values);
957
+ if (facetEntries.length === 0) return '';
958
+
959
+ const facetValuesArray: string[] = [];
960
+ for (const [key, facetData] of facetEntries) {
961
+ const plusMinusPrefix = facetData.state === 'hidden' ? '-' : '';
962
+ facetValuesArray.push(`${plusMinusPrefix}"${key}"`);
963
+ }
964
+
965
+ const valueQuery = facetValuesArray.join(` OR `);
966
+ return `${facetQueryName}:(${valueQuery})`;
967
+ }
968
+
969
+ /**
970
+ * Handles some special pre-request normalization steps for certain facet types
971
+ * that require them.
972
+ *
973
+ * @param facetName The name of the facet type (e.g., 'language')
974
+ * @param facetValues An array of values for that facet type
975
+ */
976
+ private prepareFacetForFetch(
977
+ facetName: string,
978
+ facetValues: Record<string, FacetBucket>,
979
+ ): { name: string; values: Record<string, FacetBucket> } {
980
+ // eslint-disable-next-line prefer-const
981
+ let [normalizedName, normalizedValues] = [facetName, facetValues];
982
+
983
+ // The full "search engine" name of the lending field is "lending___status"
984
+ if (facetName === 'lending') {
985
+ normalizedName = 'lending___status';
986
+ }
987
+
988
+ return {
989
+ name: normalizedName,
990
+ values: normalizedValues,
991
+ };
992
+ }
993
+
994
+ /**
995
+ * Takes an array of facet clauses, and combines them into a
996
+ * full AND-joined facet query string. Empty clauses are ignored.
997
+ */
998
+ private joinFacetClauses(facetClauses: string[]): string | undefined {
999
+ const nonEmptyFacetClauses = facetClauses.filter(
1000
+ clause => clause.length > 0,
1001
+ );
1002
+ return nonEmptyFacetClauses.length > 0
1003
+ ? `(${nonEmptyFacetClauses.join(' AND ')})`
1004
+ : undefined;
1005
+ }
1006
+
1007
+ /**
1008
+ * Fires a backend request to fetch a set of aggregations (representing UI facets) for
1009
+ * the current search state.
1010
+ */
1011
+ private async fetchFacets(): Promise<void> {
1012
+ const trimmedQuery = this.host.baseQuery?.trim();
1013
+ if (!this.canPerformSearch) return;
1014
+
1015
+ const { facetFetchQueryKey } = this;
1016
+ if (this.fetchesInProgress.has(facetFetchQueryKey)) return;
1017
+ this.fetchesInProgress.add(facetFetchQueryKey);
1018
+
1019
+ this.setFacetsLoading(true);
1020
+
1021
+ const sortParams = this.host.sortParam ? [this.host.sortParam] : [];
1022
+ const params: SearchParams = {
1023
+ ...this.pageSpecifierParams,
1024
+ query: trimmedQuery || '',
1025
+ identifiers: this.host.identifiers,
1026
+ rows: 0,
1027
+ filters: this.filterMap,
1028
+ // Fetch a few extra buckets beyond the 6 we show, in case some get suppressed
1029
+ aggregationsSize: 10,
1030
+ // Note: we don't need an aggregations param to fetch the default aggregations from the PPS.
1031
+ // The default aggregations for the search_results page type should be what we need here.
1032
+ };
1033
+ params.uid = await this.requestUID(
1034
+ { ...params, sort: sortParams },
1035
+ 'aggregations',
1036
+ );
1037
+
1038
+ const searchResponse = await this.host.searchService?.search(
1039
+ params,
1040
+ this.effectiveSearchType,
1041
+ );
1042
+ const success = searchResponse?.success;
1043
+
1044
+ // This is checking to see if the query has changed since the data was fetched.
1045
+ // If so, we just want to discard this set of aggregations because they are
1046
+ // likely no longer valid for the newer query.
1047
+ const queryChangedSinceFetch =
1048
+ !this.fetchesInProgress.has(facetFetchQueryKey);
1049
+ this.fetchesInProgress.delete(facetFetchQueryKey);
1050
+ if (queryChangedSinceFetch) return;
1051
+
1052
+ if (!success) {
1053
+ const errorMsg = searchResponse?.error?.message;
1054
+ const detailMsg = searchResponse?.error?.details?.message;
1055
+
1056
+ if (!errorMsg && !detailMsg) {
1057
+ // @ts-expect-error: Property 'Sentry' does not exist on type 'Window & typeof globalThis'
1058
+ window?.Sentry?.captureMessage?.(
1059
+ 'Missing or malformed facet response from backend',
1060
+ 'error',
1061
+ );
1062
+ }
1063
+
1064
+ this.setFacetsLoading(false);
1065
+ return;
1066
+ }
1067
+
1068
+ const { aggregations, collectionTitles, tvChannelAliases } =
1069
+ success.response;
1070
+ this.aggregations = aggregations;
1071
+
1072
+ this.histogramAggregation =
1073
+ this.host.searchType === SearchType.TV
1074
+ ? aggregations?.date_histogram
1075
+ : aggregations?.year_histogram;
1076
+
1077
+ if (collectionTitles) {
1078
+ for (const [id, title] of Object.entries(collectionTitles)) {
1079
+ this.collectionTitles.set(id, title);
1080
+ }
1081
+ }
1082
+ if (tvChannelAliases) {
1083
+ for (const [channel, network] of Object.entries(tvChannelAliases)) {
1084
+ this.tvChannelAliases.set(channel, network);
1085
+ }
1086
+ }
1087
+
1088
+ this.setFacetsLoading(false);
1089
+ this.requestHostUpdate();
1090
+ }
1091
+
1092
+ /**
1093
+ * Performs the initial page fetch(es) for the current search state.
1094
+ */
1095
+ private async doInitialPageFetch(): Promise<void> {
1096
+ this.setSearchResultsLoading(true);
1097
+ // Try to batch 2 initial page requests when possible
1098
+ await this.fetchPage(this.host.initialPageNumber, this.numInitialPages);
1099
+ }
1100
+
1101
+ /**
1102
+ * Fetches one or more pages of results and updates the data source.
1103
+ *
1104
+ * @param pageNumber The page number to fetch
1105
+ * @param numInitialPages If this is an initial page fetch (`pageNumber = 1`),
1106
+ * specifies how many pages to batch together in one request. Ignored
1107
+ * if `pageNumber != 1`, defaulting to a single page.
1108
+ */
1109
+ async fetchPage(pageNumber: number, numInitialPages = 1): Promise<void> {
1110
+ const trimmedQuery = this.host.baseQuery?.trim();
1111
+ // reset loading status
1112
+ if (!this.canPerformSearch) {
1113
+ this.setSearchResultsLoading(false);
1114
+ return;
1115
+ }
1116
+
1117
+ // if we already have data, don't fetch again
1118
+ if (this.hasPage(pageNumber)) return;
1119
+
1120
+ if (this.endOfDataReached) return;
1121
+
1122
+ // Batch multiple initial page requests together if needed (e.g., can request
1123
+ // pages 1 and 2 together in a single request).
1124
+ let numPages = pageNumber === 1 ? numInitialPages : 1;
1125
+ const numRows = this.pageSize * numPages;
1126
+
1127
+ // if a fetch is already in progress for this query and page, don't fetch again
1128
+ const { pageFetchQueryKey } = this;
1129
+ const currentPageKey = `${pageFetchQueryKey}-p:${pageNumber}`;
1130
+ if (this.fetchesInProgress.has(currentPageKey)) return;
1131
+
1132
+ for (let i = 0; i < numPages; i += 1) {
1133
+ this.fetchesInProgress.add(`${pageFetchQueryKey}-p:${pageNumber + i}`);
1134
+ }
1135
+ this.previousQueryKey = pageFetchQueryKey;
1136
+
1137
+ const { withinCollection, withinProfile } = this.host;
1138
+
1139
+ let sortParams = this.host.sortParam ? [this.host.sortParam] : [];
1140
+ // TODO eventually the PPS should handle these defaults natively
1141
+ const isDefaultSort = this.host.selectedSort === SortField.default;
1142
+ const isTVSearch = this.host.searchType === SearchType.TV;
1143
+ const isDefaultTVSort = isTVSearch && isDefaultSort;
1144
+ const isDefaultProfileSort = withinProfile && isDefaultSort;
1145
+ if (
1146
+ (isDefaultProfileSort || isDefaultTVSort) &&
1147
+ this.host.defaultSortField
1148
+ ) {
1149
+ const sortOption = SORT_OPTIONS[this.host.defaultSortField];
1150
+ const { searchServiceKey, handledBySearchService } = sortOption;
1151
+ if (searchServiceKey && handledBySearchService) {
1152
+ sortParams = [
1153
+ {
1154
+ field: searchServiceKey,
1155
+ direction: this.host.defaultSortDirection ?? 'desc',
1156
+ },
1157
+ ];
1158
+ }
1159
+ }
1160
+
1161
+ const params: SearchParams = {
1162
+ ...this.pageSpecifierParams,
1163
+ query: trimmedQuery || '',
1164
+ identifiers: this.host.identifiers,
1165
+ page: pageNumber,
1166
+ rows: numRows,
1167
+ sort: sortParams,
1168
+ filters: this.filterMap,
1169
+ aggregations: { omit: true },
1170
+ };
1171
+ params.uid = await this.requestUID(params, 'hits');
1172
+
1173
+ // log('=== FIRING PAGE REQUEST ===', params);
1174
+ const searchResponse = await this.host.searchService?.search(
1175
+ params,
1176
+ this.effectiveSearchType,
1177
+ );
1178
+ // log('=== RECEIVED PAGE RESPONSE IN CB ===', searchResponse);
1179
+ const success = searchResponse?.success;
1180
+
1181
+ // This is checking to see if the fetch has been invalidated since it was fired off.
1182
+ // If so, we just want to discard the response since it is for an obsolete query state.
1183
+ if (!this.fetchesInProgress.has(currentPageKey)) return;
1184
+ for (let i = 0; i < numPages; i += 1) {
1185
+ this.fetchesInProgress.delete(`${pageFetchQueryKey}-p:${pageNumber + i}`);
1186
+ }
1187
+
1188
+ if (!success) {
1189
+ const errorMsg = searchResponse?.error?.message;
1190
+ const detailMsg = searchResponse?.error?.details?.message;
1191
+
1192
+ this.queryErrorMessage = `${errorMsg ?? ''}${
1193
+ detailMsg ? `; ${detailMsg}` : ''
1194
+ }`;
1195
+
1196
+ if (!this.queryErrorMessage) {
1197
+ this.queryErrorMessage = 'Missing or malformed response from backend';
1198
+ // @ts-expect-error: Property 'Sentry' does not exist on type 'Window & typeof globalThis'
1199
+ window?.Sentry?.captureMessage?.(this.queryErrorMessage, 'error');
1200
+ }
1201
+
1202
+ this.setSearchResultsLoading(false);
1203
+ this.requestHostUpdate();
1204
+ this.host.emitSearchError();
1205
+ return;
1206
+ }
1207
+
1208
+ this.setTotalResultCount(success.response.totalResults - this.offset);
1209
+ if (this.activeOnHost && this.totalResults === 0) {
1210
+ // display event to offshoot when result count is zero.
1211
+ this.host.emitEmptyResults();
1212
+ }
1213
+
1214
+ this.sessionContext = success.sessionContext;
1215
+ if (withinCollection) {
1216
+ this.collectionExtraInfo = success.response.collectionExtraInfo;
1217
+
1218
+ // For collections, we want the UI to respect the default sort option
1219
+ // which can be specified in metadata, or otherwise assumed to be week:desc
1220
+ if (this.activeOnHost) {
1221
+ this.host.applyDefaultCollectionSort(this.collectionExtraInfo);
1222
+ }
1223
+
1224
+ if (this.collectionExtraInfo) {
1225
+ this.parentCollections = [].concat(
1226
+ this.collectionExtraInfo.public_metadata?.collection ?? [],
1227
+ );
1228
+
1229
+ // Update the TV collection status now that we know the parent collections
1230
+ this.host.isTVCollection =
1231
+ this.host.withinCollection?.startsWith('TV-') ||
1232
+ this.host.withinCollection === 'tvnews' ||
1233
+ this.host.withinCollection === 'tvarchive' ||
1234
+ this.parentCollections.includes('tvnews') ||
1235
+ this.parentCollections.includes('tvarchive');
1236
+ }
1237
+ } else if (withinProfile) {
1238
+ this.accountExtraInfo = success.response.accountExtraInfo;
1239
+ this.pageElements = success.response.pageElements;
1240
+ }
1241
+
1242
+ const { results, collectionTitles, tvChannelAliases } = success.response;
1243
+ if (results && results.length > 0) {
1244
+ // Load any collection titles present on the response into the cache,
1245
+ // or queue up preload fetches for them if none were present.
1246
+ if (collectionTitles) {
1247
+ for (const [id, title] of Object.entries(collectionTitles)) {
1248
+ this.collectionTitles.set(id, title);
1249
+ }
1250
+
1251
+ // Also add the target collection's title if available
1252
+ const targetTitle = this.collectionExtraInfo?.public_metadata?.title;
1253
+ if (withinCollection && targetTitle) {
1254
+ this.collectionTitles.set(withinCollection, targetTitle);
1255
+ }
1256
+ }
1257
+
1258
+ if (tvChannelAliases) {
1259
+ for (const [channel, network] of Object.entries(tvChannelAliases)) {
1260
+ this.tvChannelAliases.set(channel, network);
1261
+ }
1262
+ }
1263
+
1264
+ // Update the data source for each returned page.
1265
+ // For loans and web archives, we must account for receiving more pages than we asked for.
1266
+ const isUnpagedElement = ['lending', 'web_archives'].includes(
1267
+ this.host.profileElement!,
1268
+ );
1269
+ if (isUnpagedElement) {
1270
+ numPages = Math.ceil(results.length / this.pageSize);
1271
+ this.endOfDataReached = true;
1272
+ if (this.activeOnHost) this.host.setTileCount(this.totalResults);
1273
+ }
1274
+
1275
+ for (let i = 0; i < numPages; i += 1) {
1276
+ const pageStartIndex = this.pageSize * i;
1277
+ this.addFetchedResultsToDataSource(
1278
+ pageNumber + i,
1279
+ results.slice(pageStartIndex, pageStartIndex + this.pageSize),
1280
+ !isUnpagedElement || i === numPages - 1,
1281
+ );
1282
+ }
1283
+ }
1284
+
1285
+ // When we reach the end of the data, we can set the infinite scroller's
1286
+ // item count to the real total number of results (rather than the
1287
+ // temporary estimates based on pages rendered so far).
1288
+ if (this.size >= this.totalResults || results.length === 0) {
1289
+ this.endOfDataReached = true;
1290
+ if (this.activeOnHost) this.host.setTileCount(this.size);
1291
+ }
1292
+
1293
+ this.setSearchResultsLoading(false);
1294
+ this.requestHostUpdate();
1295
+ }
1296
+
1297
+ /**
1298
+ * Returns the type of request that produced the current set of hits,
1299
+ * based on the presence of a search query or profile/collection target
1300
+ * on the host.
1301
+ */
1302
+ private get hitRequestSource(): HitRequestSource {
1303
+ const { host } = this;
1304
+ if (host.baseQuery) return 'search_query';
1305
+ if (host.withinProfile) return 'profile_tab';
1306
+ if (host.withinCollection) return 'collection_members';
1307
+ return 'unknown';
1308
+ }
1309
+
1310
+ /**
1311
+ * Update the datasource from the fetch response
1312
+ *
1313
+ * @param pageNumber
1314
+ * @param results
1315
+ */
1316
+ private addFetchedResultsToDataSource(
1317
+ pageNumber: number,
1318
+ results: SearchResult[],
1319
+ needsReload = true,
1320
+ ): void {
1321
+ const tiles: TileModel[] = [];
1322
+ const requestSource = this.hitRequestSource;
1323
+ results?.forEach(result => {
1324
+ if (!result.identifier) return;
1325
+ tiles.push(new TileModel(result, requestSource));
1326
+ });
1327
+
1328
+ this.addPage(pageNumber, tiles);
1329
+
1330
+ if (needsReload) {
1331
+ this.refreshVisibleResults();
1332
+ }
1333
+ }
1334
+
1335
+ /**
1336
+ * Fetches the aggregation buckets for the given prefix filter type.
1337
+ */
1338
+ private async fetchPrefixFilterBuckets(
1339
+ filterType: PrefixFilterType,
1340
+ ): Promise<Bucket[]> {
1341
+ const trimmedQuery = this.host.baseQuery?.trim();
1342
+ if (!this.canPerformSearch) return [];
1343
+
1344
+ const filterAggregationKey = prefixFilterAggregationKeys[filterType];
1345
+ const sortParams = this.host.sortParam ? [this.host.sortParam] : [];
1346
+
1347
+ const params: SearchParams = {
1348
+ ...this.pageSpecifierParams,
1349
+ query: trimmedQuery || '',
1350
+ identifiers: this.host.identifiers,
1351
+ rows: 0,
1352
+ filters: this.filterMap,
1353
+ // Only fetch the firstTitle or firstCreator aggregation
1354
+ aggregations: { simpleParams: [filterAggregationKey] },
1355
+ // Fetch all 26 letter buckets
1356
+ aggregationsSize: 26,
1357
+ };
1358
+ params.uid = await this.requestUID(
1359
+ { ...params, sort: sortParams },
1360
+ 'aggregations',
1361
+ );
1362
+
1363
+ const searchResponse = await this.host.searchService?.search(
1364
+ params,
1365
+ this.effectiveSearchType,
1366
+ );
1367
+
1368
+ return (searchResponse?.success?.response?.aggregations?.[
1369
+ filterAggregationKey
1370
+ ]?.buckets ?? []) as Bucket[];
1371
+ }
1372
+
1373
+ /**
1374
+ * Fetches and caches the prefix filter counts for the given filter type.
1375
+ */
1376
+ async updatePrefixFilterCounts(filterType: PrefixFilterType): Promise<void> {
1377
+ const { facetFetchQueryKey } = this;
1378
+ const buckets = await this.fetchPrefixFilterBuckets(filterType);
1379
+
1380
+ // Don't update the filter counts for an outdated query (if it has been changed
1381
+ // since we sent the request)
1382
+ const queryChangedSinceFetch =
1383
+ facetFetchQueryKey !== this.facetFetchQueryKey;
1384
+ if (queryChangedSinceFetch) return;
1385
+
1386
+ // Unpack the aggregation buckets into a simple map like { 'A': 50, 'B': 25, ... }
1387
+ this.prefixFilterCountMap = { ...this.prefixFilterCountMap }; // Clone the object to trigger an update
1388
+ this.prefixFilterCountMap[filterType] = buckets.reduce(
1389
+ (acc: Record<string, number>, bucket: Bucket) => {
1390
+ acc[(bucket.key as string).toUpperCase()] = bucket.doc_count;
1391
+ return acc;
1392
+ },
1393
+ {},
1394
+ );
1395
+
1396
+ this.requestHostUpdate();
1397
+ }
1398
+
1399
+ /**
1400
+ * @inheritdoc
1401
+ */
1402
+ async updatePrefixFiltersForCurrentSort(): Promise<void> {
1403
+ if (['title', 'creator'].includes(this.host.selectedSort as SortField)) {
1404
+ const filterType = this.host.selectedSort as PrefixFilterType;
1405
+ if (!this.prefixFilterCountMap[filterType]) {
1406
+ this.updatePrefixFilterCounts(filterType);
1407
+ }
1408
+ }
1409
+ }
1410
+
1411
+ /**
1412
+ * @inheritdoc
1413
+ */
1414
+ refreshLetterCounts(): void {
1415
+ if (Object.keys(this.prefixFilterCountMap).length > 0) {
1416
+ this.prefixFilterCountMap = {};
1417
+ }
1418
+ this.updatePrefixFiltersForCurrentSort();
1419
+ this.requestHostUpdate();
1420
+ }
1421
+
1422
+ /**
1423
+ * @inheritdoc
1424
+ */
1425
+ populateTVChannelMaps(): Promise<TVChannelMaps> {
1426
+ // To ensure that we only make these requests once, cache the Promise returned by the
1427
+ // first call, and return the same Promise on repeated calls.
1428
+ // Resolves once both maps have been retrieved and saved in the data source.
1429
+ if (!this._tvMapsPromise) {
1430
+ this._tvMapsPromise = this._fetchTVChannelMaps();
1431
+ }
1432
+
1433
+ return this._tvMapsPromise;
1434
+ }
1435
+
1436
+ /**
1437
+ * Internal function implementing the actual fetches for TV channel mappings.
1438
+ * This should only called by the public populateTVChannelMaps method, which is guarded so
1439
+ * that we do not make extra requests for these rather large mappings.
1440
+ */
1441
+ private async _fetchTVChannelMaps(): Promise<TVChannelMaps> {
1442
+ const baseURL = 'https://av.archive.org/etc';
1443
+ const dateStr = new Date().toISOString().slice(0, 10); // YYYY-MM-DD
1444
+ const chan2networkPromise = fetch(
1445
+ `${baseURL}/chan2network.json?date=${dateStr}`,
1446
+ );
1447
+ const program2chansPromise = fetch(
1448
+ `${baseURL}/program2chans.json?date=${dateStr}`,
1449
+ );
1450
+
1451
+ const [chan2networkResponse, program2chansResponse] = await Promise.all([
1452
+ chan2networkPromise,
1453
+ program2chansPromise,
1454
+ ]);
1455
+ this.tvChannelMaps.channelToNetwork = new Map(
1456
+ Object.entries(await chan2networkResponse.json()),
1457
+ );
1458
+ this.tvChannelMaps.programToChannels = new Map(
1459
+ Object.entries(await program2chansResponse.json()),
1460
+ );
1461
+
1462
+ this.requestHostUpdate();
1463
+ return this.tvChannelMaps;
1464
+ }
1465
+ }