Display first book in series when collapsed

This commit is contained in:
aditya.chandel
2025-07-02 23:12:43 -06:00
committed by Aditya Chandel
parent 7562ae65fd
commit 1c9c09628a
2 changed files with 29 additions and 13 deletions

View File

@@ -2,9 +2,11 @@ name-template: 'v$NEXT_PATCH_VERSION 🌟'
tag-template: 'v$NEXT_PATCH_VERSION'
categories:
- title: '🚀 Features'
- title: '🚀 New Features'
labels:
- 'feature'
- title: '✨ Enhancements'
labels:
- 'enhancement'
- title: '🐛 Bug Fixes'
labels:

View File

@@ -49,24 +49,38 @@ export class SeriesCollapseFilter implements BookFilter {
map(isCollapsed => {
if (!isCollapsed || !bookState.books) return bookState;
const seenSeries = new Set<string>();
const books = [...bookState.books];
const seriesMap = new Map<string, Book[]>();
const collapsedBooks: Book[] = [];
for (const book of bookState.books) {
const name = book.metadata?.seriesName?.trim();
if (name && !seenSeries.has(name)) {
const count = bookState.books.filter(b => b.metadata?.seriesName?.trim() === name).length;
collapsedBooks.push({
...book,
seriesCount: count,
});
seenSeries.add(name);
} else if (!name) {
for (const book of books) {
const seriesName = book.metadata?.seriesName?.trim();
if (seriesName) {
if (!seriesMap.has(seriesName)) {
seriesMap.set(seriesName, []);
}
seriesMap.get(seriesName)!.push(book);
} else {
collapsedBooks.push(book);
}
}
for (const [seriesName, group] of seriesMap.entries()) {
const sortedGroup = group.slice().sort((a, b) => {
const aNum = a.metadata?.seriesNumber ?? Number.MAX_VALUE;
const bNum = b.metadata?.seriesNumber ?? Number.MAX_VALUE;
return aNum - bNum;
});
return {...bookState, books: collapsedBooks};
const firstBook = sortedGroup[0];
collapsedBooks.push({
...firstBook,
seriesCount: group.length
});
}
return { ...bookState, books: collapsedBooks };
})
);
}