feat(home): filter user sections by genre
- Add getSelectedGenreName() to genre filter store - Add filterAlbumsByGenre() helper in HomeView - Apply genre filter to Recently Played and Favorite Albums - Fetch 3x more items when filtering to ensure enough results
This commit is contained in:
1 parent
eee364e29c
commit
22ba76a8d1
2 files changed
+29
-6
No files matched your search
@@ -18,6 +18,7 @@
|
||||
} from '$lib/stores/homeSettingsStore';
|
||||
import {
|
||||
getSelectedGenreId,
|
||||
getSelectedGenreName,
|
||||
hasActiveFilter as hasGenreFilter
|
||||
} from '$lib/stores/genreFilterStore';
|
||||
import { setPlaybackContext } from '$lib/stores/playbackContextStore';
|
||||
@@ -445,6 +446,15 @@
|
||||
loadHome();
|
||||
}
|
||||
|
||||
function filterAlbumsByGenre(albums: AlbumCardData[]): AlbumCardData[] {
|
||||
const selectedGenreName = getSelectedGenreName();
|
||||
if (!selectedGenreName) return albums;
|
||||
// Filter albums whose genre contains the selected genre name (case-insensitive)
|
||||
return albums.filter(album =>
|
||||
album.genre.toLowerCase().includes(selectedGenreName.toLowerCase())
|
||||
);
|
||||
}
|
||||
|
||||
async function loadHome() {
|
||||
isInitializing = true;
|
||||
isOverlayVisible = true;
|
||||
@@ -558,9 +568,12 @@
|
||||
// Recently Played (albums) - start immediately with seeds
|
||||
if (isSectionVisible('recentAlbums')) {
|
||||
const recentAlbumIds = normalizeAlbumIds(seeds.recentlyPlayedAlbumIds);
|
||||
fetchAlbums(recentAlbumIds.slice(0, homeLimits.recentAlbums)).then(async albums => {
|
||||
recentAlbums = albums;
|
||||
await loadAllAlbumDownloadStatuses(albums);
|
||||
// Fetch more if filtering, to have enough after filter
|
||||
const fetchLimit = hasGenreFilter() ? homeLimits.recentAlbums * 3 : homeLimits.recentAlbums;
|
||||
fetchAlbums(recentAlbumIds.slice(0, fetchLimit)).then(async albums => {
|
||||
const filtered = filterAlbumsByGenre(albums).slice(0, homeLimits.recentAlbums);
|
||||
recentAlbums = filtered;
|
||||
await loadAllAlbumDownloadStatuses(filtered);
|
||||
loadingRecentAlbums = false;
|
||||
markSectionFinished();
|
||||
});
|
||||
@@ -587,9 +600,12 @@
|
||||
...seeds.favoriteAlbumIds,
|
||||
...favoriteTrackDetails.map(track => track.albumId)
|
||||
]);
|
||||
const albums = await fetchAlbums(favoriteAlbumIds.slice(0, homeLimits.favoriteAlbums));
|
||||
favoriteAlbums = albums;
|
||||
await loadAllAlbumDownloadStatuses(albums);
|
||||
// Fetch more if filtering, to have enough after filter
|
||||
const fetchLimit = hasGenreFilter() ? homeLimits.favoriteAlbums * 3 : homeLimits.favoriteAlbums;
|
||||
const albums = await fetchAlbums(favoriteAlbumIds.slice(0, fetchLimit));
|
||||
const filtered = filterAlbumsByGenre(albums).slice(0, homeLimits.favoriteAlbums);
|
||||
favoriteAlbums = filtered;
|
||||
await loadAllAlbumDownloadStatuses(filtered);
|
||||
loadingFavoriteAlbums = false;
|
||||
markSectionFinished();
|
||||
});
|
||||
|
||||
@@ -109,6 +109,13 @@ export function getSelectedGenreId(): number | undefined {
|
||||
return ids.length === 1 ? ids[0] : undefined;
|
||||
}
|
||||
|
||||
export function getSelectedGenreName(): string | undefined {
|
||||
const id = getSelectedGenreId();
|
||||
if (!id) return undefined;
|
||||
const genre = state.availableGenres.find(g => g.id === id);
|
||||
return genre?.name;
|
||||
}
|
||||
|
||||
export function isGenreSelected(genreId: number): boolean {
|
||||
return state.selectedGenreIds.has(genreId);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user