Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions src/collection-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
InfiniteScrollerCellProviderInterface,
} from '@internetarchive/infinite-scroller';
import {
CollectionExtraInfo,

Check warning on line 19 in src/collection-browser.ts

View workflow job for this annotation

GitHub Actions / build

'CollectionExtraInfo' is defined but never used
PageElementName,
SearchServiceInterface,
SearchType,
Expand Down Expand Up @@ -1992,13 +1992,12 @@
}

/**
* Applies any default sort option for the current collection, by checking
* for one in the collection's metadata. If none is found, defaults to sorting
* descending by:
* Applies any default sort option for the current collection, using the sort applied
* by the backend if applicable. If none is found, defaults to sorting descending by:
* - Date Favorited for fav-* collections
* - Weekly views for all other collections
*/
applyDefaultCollectionSort(collectionInfo?: CollectionExtraInfo): void {
applyDefaultCollectionSort(backendSort?: string): void {
if (this.baseQuery) {
// If there's a query set, then we default to relevance sorting regardless of
// the collection metadata-specified sort.
Expand All @@ -2009,17 +2008,14 @@

// Favorite collections sort on Date Favorited by default.
// Other collections fall back to sorting on weekly views.
const baseDefaultSort: string =
collectionInfo?.public_metadata?.identifier?.startsWith('fav-')
? '-favoritedate'
: '-week';

// The collection metadata may override the default sorting with something else
const metadataSort: string | undefined =
collectionInfo?.public_metadata?.['sort-by'];

// Prefer the metadata-specified sort if one exists
const defaultSortToApply = metadataSort ?? baseDefaultSort;
const baseDefaultSort: string = this.withinCollection?.startsWith('fav-')
? '-favoritedate'
: '-week';

// The PPS may override the default sorting with something else
// E.g., sort-by metadata, or TV collection defaults
// Prefer the backend-specified sort if one exists
const defaultSortToApply = backendSort ?? baseDefaultSort;

// Account for both -field and field:dir formats
let [field, dir] = defaultSortToApply.split(':');
Expand Down
9 changes: 8 additions & 1 deletion src/data-source/collection-browser-data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,14 @@ export class CollectionBrowserDataSource
// For collections, we want the UI to respect the default sort option
// which can be specified in metadata, or otherwise assumed to be week:desc
if (this.activeOnHost) {
this.host.applyDefaultCollectionSort(this.collectionExtraInfo);
// Normalize any backend-applied sort to an array and take the first element.
// This accounts for the backend sort being a string, a string[], or undefined entirely.
const backendSortRaw =
success.request.backendRequests['search']?.finalized_parameters.sort;
const backendSort = ([] as (string | undefined)[]).concat(
backendSortRaw,
)?.[0];
this.host.applyDefaultCollectionSort(backendSort);
}

if (this.collectionExtraInfo) {
Expand Down
2 changes: 1 addition & 1 deletion src/data-source/collection-browser-query-state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {
CollectionExtraInfo,

Check warning on line 2 in src/data-source/collection-browser-query-state.ts

View workflow job for this annotation

GitHub Actions / build

'CollectionExtraInfo' is defined but never used
PageElementName,
SearchServiceInterface,
SearchType,
Expand Down Expand Up @@ -49,7 +49,7 @@
setFacetsLoading(loading: boolean): void;
setTotalResultCount(count: number): void;
setTileCount(count: number): void;
applyDefaultCollectionSort(collectionInfo?: CollectionExtraInfo): void;
applyDefaultCollectionSort(backendSort?: string): void;
emitEmptyResults(): void;
emitQueryStateChanged(): void;
refreshVisibleResults(): void;
Expand Down
2 changes: 1 addition & 1 deletion src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ export const SORT_OPTIONS: Record<SortField, SortOption> = {
handledBySearchService: false,
searchServiceKey: 'favoritedate',
displayName: 'Date favorited',
urlNames: ['favoritedate'],
urlNames: ['favoritedate', 'date_favorited'],
Copy link
Contributor Author

@latonv latonv Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PPS identifies this sort using date_favorited, a format that differs from the other date sorts because of the different backing store for favorites & date favorited. Adding the new one so that we recognize it correctly, and leaving the old one for backwards-compatibility. We don't actually send this sort string to the PPS as it is always the default in context.

},
[SortField.creator]: {
field: SortField.creator,
Expand Down
4 changes: 2 additions & 2 deletions test/collection-browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ describe('Collection Browser', () => {
);
});

it('sets default sort from collection metadata', async () => {
it('sets default sort from backend applied sort', async () => {
const searchService = new MockSearchService();
const el = await fixture<CollectionBrowser>(
html`<collection-browser
Expand All @@ -1117,7 +1117,7 @@ describe('Collection Browser', () => {
expect(sortBar.sortDirection).to.be.null;
});

it('sets default sort from collection metadata in "-field" format', async () => {
it('sets default sort from backend applied sort in "-field" format', async () => {
const searchService = new MockSearchService();
const el = await fixture<CollectionBrowser>(
html`<collection-browser
Expand Down
14 changes: 2 additions & 12 deletions test/mocks/mock-search-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ export const getMockSuccessWithDefaultSort: () => Result<
sort: [],
},
backendRequests: {
primary: {
search: {
kind: 'hits',
finalized_parameters: {
user_query: 'default-sort',
Expand All @@ -745,11 +745,6 @@ export const getMockSuccessWithDefaultSort: () => Result<
},
}),
],
collectionExtraInfo: {
public_metadata: {
'sort-by': 'titleSorter',
},
},
},
responseHeader: {
succeeded: true,
Expand All @@ -770,7 +765,7 @@ export const getMockSuccessWithConciseDefaultSort: () => Result<
sort: [],
},
backendRequests: {
primary: {
search: {
kind: 'hits',
finalized_parameters: {
user_query: 'default-sort-concise',
Expand All @@ -792,11 +787,6 @@ export const getMockSuccessWithConciseDefaultSort: () => Result<
},
}),
],
collectionExtraInfo: {
public_metadata: {
'sort-by': '-addeddate',
},
},
},
responseHeader: {
succeeded: true,
Expand Down
Loading