fix album date and artist on search
This commit is contained in:
parent
89f2b6c154
commit
3294eaf2fa
3 changed files with 50 additions and 38 deletions
|
|
@ -304,10 +304,10 @@ export class LosslessAPI {
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
const normalized = this.normalizeSearchResponse(data, 'tracks');
|
const normalized = this.normalizeSearchResponse(data, 'tracks');
|
||||||
const preparedTracks = normalized.items.map((t) => this.prepareTrack(t));
|
const preparedTracks = normalized.items.map((t) => this.prepareTrack(t));
|
||||||
// Note: Skipping enrichTracksWithAlbumDates for search results to avoid excessive album API calls
|
const enrichedTracks = await this.enrichTracksWithAlbumDates(preparedTracks);
|
||||||
const result = {
|
const result = {
|
||||||
...normalized,
|
...normalized,
|
||||||
items: preparedTracks,
|
items: enrichedTracks,
|
||||||
};
|
};
|
||||||
|
|
||||||
await this.cache.set('search_tracks', query, result);
|
await this.cache.set('search_tracks', query, result);
|
||||||
|
|
|
||||||
|
|
@ -206,26 +206,33 @@ export const apiSettings = {
|
||||||
const targetUrls = instancesObj[type] || instancesObj.api || [];
|
const targetUrls = instancesObj[type] || instancesObj.api || [];
|
||||||
if (targetUrls.length === 0) return [];
|
if (targetUrls.length === 0) return [];
|
||||||
|
|
||||||
// Use cached speed results to sort if available, but DON'T run new speed tests
|
|
||||||
// Speed tests should only run explicitly via refreshSpeedTests() to avoid
|
|
||||||
// mass /track API calls when playing a song
|
|
||||||
const speedCache = this.getCachedSpeedTests();
|
const speedCache = this.getCachedSpeedTests();
|
||||||
|
// Construct cache key based on type
|
||||||
const getCacheKey = (u) => (type === 'streaming' ? `${u}#streaming` : u);
|
const getCacheKey = (u) => (type === 'streaming' ? `${u}#streaming` : u);
|
||||||
|
|
||||||
// Sort by cached speeds if we have any cached data
|
const urlsToTest = targetUrls.filter((url) => !speedCache.speeds[getCacheKey(url)]);
|
||||||
const hasCachedData = targetUrls.some((url) => speedCache.speeds[getCacheKey(url)]);
|
|
||||||
|
|
||||||
if (hasCachedData) {
|
if (urlsToTest.length > 0) {
|
||||||
const sortedList = [...targetUrls].sort((a, b) => {
|
const results = await this.testSpecificUrls(urlsToTest, type);
|
||||||
|
this.updateSpeedCache(results);
|
||||||
|
Object.assign(speedCache, this.getCachedSpeedTests());
|
||||||
|
}
|
||||||
|
|
||||||
|
const sortList = (list) => {
|
||||||
|
return [...list].sort((a, b) => {
|
||||||
const speedA = speedCache.speeds[getCacheKey(a)]?.speed ?? Infinity;
|
const speedA = speedCache.speeds[getCacheKey(a)]?.speed ?? Infinity;
|
||||||
const speedB = speedCache.speeds[getCacheKey(b)]?.speed ?? Infinity;
|
const speedB = speedCache.speeds[getCacheKey(b)]?.speed ?? Infinity;
|
||||||
return speedA - speedB;
|
return speedA - speedB;
|
||||||
});
|
});
|
||||||
return sortedList;
|
};
|
||||||
}
|
|
||||||
|
|
||||||
// No cached data - return in default order without testing
|
const sortedList = sortList(targetUrls);
|
||||||
return targetUrls;
|
|
||||||
|
// Persist the sorted order
|
||||||
|
instancesObj[type] = sortedList;
|
||||||
|
this.saveInstances(instancesObj);
|
||||||
|
|
||||||
|
return sortedList;
|
||||||
},
|
},
|
||||||
|
|
||||||
async refreshSpeedTests() {
|
async refreshSpeedTests() {
|
||||||
|
|
|
||||||
55
js/ui.js
55
js/ui.js
|
|
@ -1693,39 +1693,44 @@ export class UIRenderer {
|
||||||
const signal = this.searchAbortController.signal;
|
const signal = this.searchAbortController.signal;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Optimize: Only make 2 API calls (tracks and playlists), extract artists/albums from tracks
|
const [tracksResult, artistsResult, albumsResult, playlistsResult] = await Promise.all([
|
||||||
const [tracksResult, playlistsResult] = await Promise.all([
|
|
||||||
this.api.searchTracks(query, { signal }),
|
this.api.searchTracks(query, { signal }),
|
||||||
|
this.api.searchArtists(query, { signal }),
|
||||||
|
this.api.searchAlbums(query, { signal }),
|
||||||
this.api.searchPlaylists(query, { signal }),
|
this.api.searchPlaylists(query, { signal }),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
let finalTracks = tracksResult.items;
|
let finalTracks = tracksResult.items;
|
||||||
|
let finalArtists = artistsResult.items;
|
||||||
|
let finalAlbums = albumsResult.items;
|
||||||
let finalPlaylists = playlistsResult.items;
|
let finalPlaylists = playlistsResult.items;
|
||||||
|
|
||||||
// Extract artists from tracks
|
if (finalArtists.length === 0 && finalTracks.length > 0) {
|
||||||
const artistMap = new Map();
|
const artistMap = new Map();
|
||||||
finalTracks.forEach((track) => {
|
finalTracks.forEach((track) => {
|
||||||
if (track.artist && !artistMap.has(track.artist.id)) {
|
if (track.artist && !artistMap.has(track.artist.id)) {
|
||||||
artistMap.set(track.artist.id, track.artist);
|
artistMap.set(track.artist.id, track.artist);
|
||||||
}
|
}
|
||||||
if (track.artists) {
|
if (track.artists) {
|
||||||
track.artists.forEach((artist) => {
|
track.artists.forEach((artist) => {
|
||||||
if (!artistMap.has(artist.id)) {
|
if (!artistMap.has(artist.id)) {
|
||||||
artistMap.set(artist.id, artist);
|
artistMap.set(artist.id, artist);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let finalArtists = Array.from(artistMap.values());
|
finalArtists = Array.from(artistMap.values());
|
||||||
|
}
|
||||||
|
|
||||||
// Extract albums from tracks
|
if (finalAlbums.length === 0 && finalTracks.length > 0) {
|
||||||
const albumMap = new Map();
|
const albumMap = new Map();
|
||||||
finalTracks.forEach((track) => {
|
finalTracks.forEach((track) => {
|
||||||
if (track.album && !albumMap.has(track.album.id)) {
|
if (track.album && !albumMap.has(track.album.id)) {
|
||||||
albumMap.set(track.album.id, track.album);
|
albumMap.set(track.album.id, track.album);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let finalAlbums = Array.from(albumMap.values());
|
finalAlbums = Array.from(albumMap.values());
|
||||||
|
}
|
||||||
|
|
||||||
if (finalTracks.length) {
|
if (finalTracks.length) {
|
||||||
this.renderListWithTracks(tracksContainer, finalTracks, true);
|
this.renderListWithTracks(tracksContainer, finalTracks, true);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue