fix(favorites): ensure favorites sync with Qobuz API and show loading state

Issues fixed:
- playbackService was bypassing the favorites store, causing UI desync
- Album favorites weren't loaded on login (only track favorites were)
- No visual feedback while favorite API call was in progress

Changes:
- Add toggling state tracking to favoritesStore and albumFavoritesStore
- Add isTrackToggling() and isAlbumToggling() exports
- Prevent double-toggle while API call is in progress
- Update TrackRow and AlbumCard to show pulse animation while toggling
- Update playbackService to use store's toggle (centralized API calls)
- Load both track and album favorites on login
This commit is contained in:
vicrodh committed 2026-01-29 19:24:13 -06:00
1 parent 4858faa696
commit defd2f0fe1
6 files changed
+94 -29

No files matched your search

+21 -2
View File
@@ -5,6 +5,7 @@
import {
subscribe as subscribeAlbumFavorites,
isAlbumFavorite,
isAlbumToggling,
loadAlbumFavorites,
toggleAlbumFavorite
} from '$lib/stores/albumFavoritesStore';
@@ -86,6 +87,7 @@
const artistDuration = $derived(artistOverflow > 0 ? `${(artistOverflow + 16) / tickerSpeed}s` : '0s');
let favoriteFromStore = $state(false);
let isToggling = $state(false);
const isFavorite = $derived(albumId ? favoriteFromStore : false);
const hasMenu = $derived(!!(onPlayNext || onPlayLater || onShareQobuz || onShareSonglink || onDownload));
const showFavoriteButton = $derived(showFavorite ?? !!albumId);
@@ -158,8 +160,10 @@
if (albumId) {
void loadAlbumFavorites();
favoriteFromStore = isAlbumFavorite(albumId);
isToggling = isAlbumToggling(albumId);
const unsubscribe = subscribeAlbumFavorites(() => {
favoriteFromStore = isAlbumFavorite(albumId);
isToggling = isAlbumToggling(albumId);
});
return () => unsubscribe();
}
@@ -217,10 +221,11 @@
<button
class="overlay-btn overlay-btn--minor"
class:is-active={isFavorite}
class:is-toggling={isToggling}
class:disabled={!favoriteAvailable}
type="button"
aria-disabled={!favoriteAvailable}
disabled={!favoriteAvailable}
aria-disabled={!favoriteAvailable || isToggling}
disabled={!favoriteAvailable || isToggling}
onclick={handleToggleFavorite}
title={isFavorite ? 'Remove from favorites' : 'Add to favorites'}
>
@@ -466,6 +471,20 @@
transform: none;
}
.overlay-btn.is-toggling {
cursor: wait;
animation: favorite-pulse 0.8s ease-in-out infinite;
}
@keyframes favorite-pulse {
0%, 100% {
opacity: 0.5;
}
50% {
opacity: 1;
}
}
.overlay-menu {
display: flex;
align-items: center;
+21
View File
@@ -6,6 +6,7 @@
import {
subscribe as subscribeFavorites,
isTrackFavorite,
isTrackToggling,
toggleTrackFavorite
} from '$lib/stores/favoritesStore';
import { togglePlay } from '$lib/stores/playerStore';
@@ -82,6 +83,7 @@
let isHovered = $state(false);
let favoriteFromStore = $state(false);
let isToggling = $state(false);
// Use override if provided, otherwise use store
const isFavorite = $derived(isFavoriteOverride ?? favoriteFromStore);
@@ -93,8 +95,10 @@
onMount(() => {
if (trackId !== undefined) {
favoriteFromStore = isTrackFavorite(trackId);
isToggling = isTrackToggling(trackId);
const unsubscribe = subscribeFavorites(() => {
favoriteFromStore = isTrackFavorite(trackId);
isToggling = isTrackToggling(trackId);
});
return unsubscribe;
}
@@ -208,8 +212,10 @@
type="button"
class="favorite-btn"
class:is-favorite={isFavorite}
class:is-toggling={isToggling}
onclick={handleToggleFavorite}
title={isFavorite ? 'Remove from favorites' : 'Add to favorites'}
disabled={isToggling}
>
{#if isFavorite}
<Heart size={14} fill="var(--accent-primary)" color="var(--accent-primary)" />
@@ -494,6 +500,21 @@
opacity: 1;
}
.favorite-btn.is-toggling {
opacity: 1;
cursor: wait;
animation: favorite-pulse 0.8s ease-in-out infinite;
}
@keyframes favorite-pulse {
0%, 100% {
opacity: 0.4;
}
50% {
opacity: 1;
}
}
.download-indicator {
display: flex;
align-items: center;
+12 -22
View File
@@ -41,6 +41,10 @@ import {
markScrobblesSent,
cleanupSentScrobbles
} from '$lib/stores/offlineStore';
import {
toggleTrackFavorite as storeToggleTrackFavorite,
addTrackFavorite as storeAddTrackFavorite
} from '$lib/stores/favoritesStore';
// ============ Types ============
@@ -661,26 +665,17 @@ export async function checkTrackFavorite(trackId: number): Promise<boolean> {
/**
* Toggle favorite status for a track
* Uses the centralized favorites store to ensure UI stays in sync
*/
export async function toggleTrackFavorite(
trackId: number,
currentlyFavorite: boolean
): Promise<{ success: boolean; isFavorite: boolean }> {
const newFavoriteState = !currentlyFavorite;
try {
if (newFavoriteState) {
await invoke('add_favorite', { favType: 'track', itemId: String(trackId) });
// Log favorite event for recommendations
void logRecoEvent({
eventType: 'favorite',
itemType: 'track',
trackId
});
} else {
await invoke('remove_favorite', { favType: 'track', itemId: String(trackId) });
}
return { success: true, isFavorite: newFavoriteState };
// Use the store's toggle function - it handles API calls, optimistic updates,
// loading states, and notifies all subscribed components
const newState = await storeToggleTrackFavorite(trackId);
return { success: true, isFavorite: newState };
} catch (err) {
console.error('Failed to toggle favorite:', err);
return { success: false, isFavorite: currentlyFavorite };
@@ -689,17 +684,12 @@ export async function toggleTrackFavorite(
/**
* Add a track to favorites
* Uses the centralized favorites store to ensure UI stays in sync
*/
export async function addTrackToFavorites(trackId: number): Promise<boolean> {
try {
await invoke('add_favorite', { favType: 'track', itemId: String(trackId) });
// Log favorite event for recommendations
void logRecoEvent({
eventType: 'favorite',
itemType: 'track',
trackId
});
return true;
// Use the store's add function - it handles API calls and UI sync
return await storeAddTrackFavorite(trackId);
} catch (err) {
console.error('Failed to add to favorites:', err);
return false;
+16 -1
View File
@@ -8,6 +8,7 @@ import { invoke } from '@tauri-apps/api/core';
import { logRecoEvent } from '$lib/services/recoService';
let favoriteAlbumIds = new Set<string>();
let togglingAlbumIds = new Set<string>(); // Album IDs currently being toggled (API in progress)
let isLoaded = false;
let isLoading = false;
const listeners = new Set<() => void>();
@@ -27,6 +28,10 @@ export function isAlbumFavorite(albumId: string): boolean {
return favoriteAlbumIds.has(albumId);
}
export function isAlbumToggling(albumId: string): boolean {
return togglingAlbumIds.has(albumId);
}
export function getFavoriteAlbumIds(): string[] {
return Array.from(favoriteAlbumIds);
}
@@ -72,14 +77,21 @@ export async function loadAlbumFavorites(): Promise<void> {
}
export async function toggleAlbumFavorite(albumId: string): Promise<boolean> {
// Prevent double-toggling while API call is in progress
if (togglingAlbumIds.has(albumId)) {
return favoriteAlbumIds.has(albumId);
}
const wasFavorite = favoriteAlbumIds.has(albumId);
const newState = !wasFavorite;
// Optimistic update + mark as toggling
if (newState) {
favoriteAlbumIds.add(albumId);
} else {
favoriteAlbumIds.delete(albumId);
}
togglingAlbumIds.add(albumId);
notifyListeners();
try {
@@ -100,9 +112,12 @@ export async function toggleAlbumFavorite(albumId: string): Promise<boolean> {
} else {
favoriteAlbumIds.add(albumId);
}
notifyListeners();
console.error('Failed to toggle album favorite:', err);
return wasFavorite;
} finally {
// Always clear toggling state
togglingAlbumIds.delete(albumId);
notifyListeners();
}
}
+20 -2
View File
@@ -10,6 +10,7 @@ import { logRecoEvent } from '$lib/services/recoService';
// State
let favoriteTrackIds = new Set<number>();
let togglingTrackIds = new Set<number>(); // Track IDs currently being toggled (API in progress)
let isLoaded = false;
let isLoading = false;
const listeners = new Set<() => void>();
@@ -37,6 +38,13 @@ export function isTrackFavorite(trackId: number): boolean {
return favoriteTrackIds.has(trackId);
}
/**
* Check if a track favorite is currently being toggled (API call in progress)
*/
export function isTrackToggling(trackId: number): boolean {
return togglingTrackIds.has(trackId);
}
/**
* Get all favorite track IDs
*/
@@ -88,15 +96,21 @@ export async function loadFavorites(): Promise<void> {
* Returns the new favorite state
*/
export async function toggleTrackFavorite(trackId: number): Promise<boolean> {
// Prevent double-toggling while API call is in progress
if (togglingTrackIds.has(trackId)) {
return favoriteTrackIds.has(trackId);
}
const wasFavorite = favoriteTrackIds.has(trackId);
const newState = !wasFavorite;
// Optimistic update
// Optimistic update + mark as toggling
if (newState) {
favoriteTrackIds.add(trackId);
} else {
favoriteTrackIds.delete(trackId);
}
togglingTrackIds.add(trackId);
notifyListeners();
try {
@@ -118,9 +132,12 @@ export async function toggleTrackFavorite(trackId: number): Promise<boolean> {
} else {
favoriteTrackIds.add(trackId);
}
notifyListeners();
console.error('Failed to toggle favorite:', err);
return wasFavorite;
} finally {
// Always clear toggling state
togglingTrackIds.delete(trackId);
notifyListeners();
}
}
@@ -166,6 +183,7 @@ export function unmarkAsFavorite(trackId: number): void {
*/
export function reset(): void {
favoriteTrackIds = new Set();
togglingTrackIds = new Set();
isLoaded = false;
isLoading = false;
notifyListeners();