Fix Download Discography on library page
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.
This commit is contained in:
parent
77e4671236
commit
89b5178838
1 changed files with 43 additions and 21 deletions
|
|
@ -40955,34 +40955,56 @@ async function openDiscographyModal() {
|
||||||
let discography = artistsPageState.artistDiscography;
|
let discography = artistsPageState.artistDiscography;
|
||||||
let completionCache = artistsPageState.cache.completionData;
|
let completionCache = artistsPageState.cache.completionData;
|
||||||
|
|
||||||
// Fallback to Library page state if Artists page has no data
|
// Fallback to Library page state if Artists page has no data for THIS artist
|
||||||
if (!artist || !discography) {
|
const libId = artistDetailPageState.currentArtistId;
|
||||||
const libId = artistDetailPageState.currentArtistId;
|
const libName = artistDetailPageState.currentArtistName;
|
||||||
const libName = artistDetailPageState.currentArtistName;
|
const isLibraryPage = libId && libName;
|
||||||
if (libId && libName) {
|
const artistsPageMatchesLibrary = artist && isLibraryPage && artist.name?.toLowerCase() === libName?.toLowerCase();
|
||||||
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 (isLibraryPage && (!artist || !discography || !artistsPageMatchesLibrary)) {
|
||||||
if (!discography) {
|
// On library page — don't trust stale artistsPageState from a previous Artists page search
|
||||||
try {
|
artist = { id: libId, name: libName, image_url: document.getElementById('artist-detail-image')?.src || '' };
|
||||||
showToast('Loading discography...', 'info');
|
discography = null;
|
||||||
const res = await fetch(`/api/artist/${libId}/discography?artist_name=${encodeURIComponent(libName)}`);
|
|
||||||
const data = await res.json();
|
let metadataArtistId = null;
|
||||||
if (data && (data.albums || data.eps || data.singles)) {
|
try {
|
||||||
discography = data;
|
showToast('Loading discography...', 'info');
|
||||||
artistsPageState.artistDiscography = data;
|
|
||||||
artistsPageState.selectedArtist = artist;
|
// Fetch the artist's metadata IDs from the DB (enhanced view may not be loaded)
|
||||||
}
|
let lookupId = libId;
|
||||||
} catch (e) {
|
try {
|
||||||
console.error('Failed to load discography:', e);
|
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) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue