From 89b5178838a12ff0ceda366fd9e6a6eb5680f285 Mon Sep 17 00:00:00 2001 From: Broque Thomas <26755000+Nezreka@users.noreply.github.com> Date: Wed, 1 Apr 2026 16:26:25 -0700 Subject: [PATCH] Fix Download Discography on library page MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three issues fixed: 1. Stale discography data from a previous Artists page search was used instead of fetching for the current library artist. Now detects library page context and forces fresh fetch when names differ. 2. Enhanced view may not be loaded, so metadata IDs (spotify/itunes/ deezer) are fetched from the enhanced endpoint on demand. 3. Fixed undefined spotifyId reference — now uses properly scoped metadataArtistId variable. Falls back to name-based search when no metadata IDs are available. Better error message directs users to Artists page as alternative. --- webui/static/script.js | 66 ++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 22 deletions(-) diff --git a/webui/static/script.js b/webui/static/script.js index 1d3fc795..25bf6fca 100644 --- a/webui/static/script.js +++ b/webui/static/script.js @@ -40955,34 +40955,56 @@ async function openDiscographyModal() { let discography = artistsPageState.artistDiscography; let completionCache = artistsPageState.cache.completionData; - // Fallback to Library page state if Artists page has no data - if (!artist || !discography) { - const libId = artistDetailPageState.currentArtistId; - const libName = artistDetailPageState.currentArtistName; - if (libId && libName) { - artist = { id: libId, name: libName, image_url: document.getElementById('artist-detail-image')?.src || '' }; - discography = artistsPageState.artistDiscography; - - // If discography not loaded, fetch it on-demand - if (!discography) { - try { - showToast('Loading discography...', 'info'); - const res = await fetch(`/api/artist/${libId}/discography?artist_name=${encodeURIComponent(libName)}`); - const data = await res.json(); - if (data && (data.albums || data.eps || data.singles)) { - discography = data; - artistsPageState.artistDiscography = data; - artistsPageState.selectedArtist = artist; - } - } catch (e) { - console.error('Failed to load discography:', e); + // Fallback to Library page state if Artists page has no data for THIS artist + const libId = artistDetailPageState.currentArtistId; + const libName = artistDetailPageState.currentArtistName; + const isLibraryPage = libId && libName; + const artistsPageMatchesLibrary = artist && isLibraryPage && artist.name?.toLowerCase() === libName?.toLowerCase(); + + if (isLibraryPage && (!artist || !discography || !artistsPageMatchesLibrary)) { + // On library page — don't trust stale artistsPageState from a previous Artists page search + artist = { id: libId, name: libName, image_url: document.getElementById('artist-detail-image')?.src || '' }; + discography = null; + + let metadataArtistId = null; + try { + showToast('Loading discography...', 'info'); + + // Fetch the artist's metadata IDs from the DB (enhanced view may not be loaded) + let lookupId = libId; + try { + const idRes = await fetch(`/api/library/artist/${libId}/enhanced`); + const idData = await idRes.json(); + if (idData.success && idData.artist) { + const a = idData.artist; + metadataArtistId = a.spotify_artist_id || a.itunes_artist_id || a.deezer_id || null; + lookupId = metadataArtistId || libId; } + } catch (e) { + console.debug('[Discography] Could not fetch artist IDs, using DB id'); } + + const res = await fetch(`/api/artist/${encodeURIComponent(lookupId)}/discography?artist_name=${encodeURIComponent(libName)}`); + const data = await res.json(); + + if (!data.error) { + discography = { albums: data.albums || [], singles: data.singles || [] }; + if (discography.albums.length > 0 || discography.singles.length > 0) { + artistsPageState.artistDiscography = discography; + // Use metadata source ID for the modal (needed for download API calls) + if (metadataArtistId) artist.id = metadataArtistId; + artistsPageState.selectedArtist = artist; + } else { + discography = null; + } + } + } catch (e) { + console.error('Failed to load discography:', e); } } if (!artist || !discography) { - showToast('No discography data available. Artist may not be on Spotify/iTunes.', 'error'); + showToast('No discography found. Try searching this artist on the Artists page instead.', 'error'); return; }