Fix Qobuz album shuffle to start with random track and add debug logs

- Modified handleShuffleAlbum to pick random starting track (same fix as Local Library)
- Added comprehensive debug logs to handleAlbumTrackPlay to diagnose queue issues
- Fixed favoritesStore crash when response.tracks is undefined (defensive checks)

Fixes:
- Qobuz album shuffle no longer always starts with first track
- App no longer crashes on startup with "undefined is not an object (evaluating 'response.tracks.items')"
This commit is contained in:
vicrodh committed 2026-01-19 19:02:46 -06:00
1 parent a1220b3a06
commit 3d8c049226
2 files changed
+28 -4

No files matched your search

+11 -2
View File
@@ -59,16 +59,25 @@ export async function loadFavorites(): Promise<void> {
isLoading = true;
try {
const response = await invoke<{ tracks: { items: Array<{ id: number }> } }>('get_favorites', {
const response = await invoke<{ tracks?: { items?: Array<{ id: number }> } }>('get_favorites', {
favType: 'track',
limit: 500
});
favoriteTrackIds = new Set(response.tracks.items.map(t => t.id));
// Defensive: handle missing tracks or items
if (response?.tracks?.items) {
favoriteTrackIds = new Set(response.tracks.items.map(t => t.id));
} else {
// No favorites or unexpected response structure
favoriteTrackIds = new Set();
}
isLoaded = true;
notifyListeners();
} catch (err) {
console.error('Failed to load favorites:', err);
// On error, initialize as empty set
favoriteTrackIds = new Set();
} finally {
isLoading = false;
}
+17 -2
View File
@@ -724,6 +724,8 @@
// Build queue from album tracks before playing
if (selectedAlbum?.tracks) {
console.log('[Album Queue] Building queue from', selectedAlbum.tracks.length, 'album tracks');
const trackIndex = selectedAlbum.tracks.findIndex(t => t.id === track.id);
const queueTracks: BackendQueueTrack[] = selectedAlbum.tracks.map(t => ({
id: t.id,
@@ -738,8 +740,13 @@
is_local: false
}));
console.log('[Album Queue] Mapped to', queueTracks.length, 'queue tracks, startIndex:', trackIndex);
console.log('[Album Queue] Track IDs:', queueTracks.map(t => t.id));
// Set the queue starting at the clicked track
await setQueue(queueTracks, trackIndex >= 0 ? trackIndex : 0, true);
console.log('[Album Queue] Queue set successfully');
}
// Play track using unified service
@@ -1025,6 +1032,8 @@
async function handleShuffleAlbum() {
if (!selectedAlbum?.tracks?.length) return;
console.log('[Album Shuffle] Starting shuffle with', selectedAlbum.tracks.length, 'tracks');
// Set shuffle mode first
try {
await invoke('set_shuffle', { enabled: true });
@@ -1033,8 +1042,14 @@
console.error('Failed to enable shuffle:', err);
}
// Then play from first track (queue will be shuffled)
await handlePlayAllAlbum();
// Pick a random track to start with
const randomIndex = Math.floor(Math.random() * selectedAlbum.tracks.length);
const randomTrack = selectedAlbum.tracks[randomIndex];
console.log('[Album Shuffle] Starting from random track index:', randomIndex, 'track:', randomTrack.title);
// Play from random track (queue will be shuffled by backend)
await handleAlbumTrackPlay(randomTrack);
showToast('Shuffle play enabled', 'info');
}