From 7da337754b993ff508b06d306c7f76379ab2d11d Mon Sep 17 00:00:00 2001 From: James Ward Date: Mon, 27 Apr 2026 11:10:48 -0400 Subject: [PATCH] fix(ui): fall back the book sorting service to primary file (#944) --- .../src/app/features/book/service/sort.service.spec.ts | 10 ++++++++++ frontend/src/app/features/book/service/sort.service.ts | 6 +++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/frontend/src/app/features/book/service/sort.service.spec.ts b/frontend/src/app/features/book/service/sort.service.spec.ts index 8e938bb54..dfbebbd8d 100644 --- a/frontend/src/app/features/book/service/sort.service.spec.ts +++ b/frontend/src/app/features/book/service/sort.service.spec.ts @@ -43,6 +43,16 @@ describe('SortService', () => { expect(service.applySort(books, sortOption).map(book => book.id)).toEqual([1, 2]); }); + it('sorts by primaryFile file name if file name is missing', () => { + const books = [ + makeBook(2, {fileName: undefined, primaryFile: { id: 2, bookId: 2, fileName: 'Book 2' }, metadata: {bookId: 2, title: 'Book 10'}}), + makeBook(1, {fileName: 'Book 1', primaryFile: { id: 1, bookId: 1, fileName: 'Book 3' }, metadata: {bookId: 1, title: 'Book 2'}}), + ]; + const sortOption: SortOption = {label: 'File Name', field: 'fileName', direction: SortDirection.ASCENDING}; + + expect(service.applySort(books, sortOption).map(book => book.id)).toEqual([1, 2]); + }); + it('sorts by array fields and read status rank', () => { const books = [ makeBook(1, {metadata: {bookId: 1, authors: ['Jane Zed']}, readStatus: ReadStatus.READ}), diff --git a/frontend/src/app/features/book/service/sort.service.ts b/frontend/src/app/features/book/service/sort.service.ts index e3c525fa9..b18276076 100644 --- a/frontend/src/app/features/book/service/sort.service.ts +++ b/frontend/src/app/features/book/service/sort.service.ts @@ -89,9 +89,9 @@ export class SortService { locked: (book) => book.metadata?.allMetadataLocked ?? false, lastReadTime: (book) => book.lastReadTime ? new Date(book.lastReadTime).getTime() : null, addedOn: (book) => book.addedOn ? new Date(book.addedOn).getTime() : null, - fileSizeKb: (book) => book.fileSizeKb || null, - fileName: (book) => book.fileName, - filePath: (book) => book.filePath, + fileSizeKb: (book) => book.fileSizeKb ?? book.primaryFile?.fileSizeKb ?? null, + fileName: (book) => book.fileName ?? book.primaryFile?.fileName ?? null, + filePath: (book) => book.filePath ?? book.primaryFile?.filePath ?? null, random: () => Math.random(), seriesName: (book) => book.metadata?.seriesName?.toLowerCase() || null, seriesNumber: (book) => book.metadata?.seriesNumber ?? null,