diff --git a/src/lib/stores/awardCatalogStore.ts b/src/lib/stores/awardCatalogStore.ts index 1823b6b67..9d43b08e1 100644 --- a/src/lib/stores/awardCatalogStore.ts +++ b/src/lib/stores/awardCatalogStore.ts @@ -1,15 +1,15 @@ /** * Award catalog — resolves award ids by name for the cases where - * /album/get returns an award with only a name and no id. Sources: - * 1. Hardcoded seed of Qobuz editorial awards with stable ids and - * known locale aliases (Qobuzissime / Album of the Week / Ideal - * Discography) — resolves instantly, survives locale changes. - * 2. /award/explore catalog fetched lazily on first unknown name. + * /album/get returns an award entry without an id. Backed by + * /award/explore (paginated), fetched once per session and cached + * in-memory. Names are compared normalized (NFD + diacritics + * stripped + lowercase + whitespace collapsed) so locale changes + * affecting accent/case don't break the match. * - * Names are compared normalized: lowercased + diacritics stripped + - * whitespace collapsed, so "Discoteca Ideal Qobuz", - * "Qobuz Ideal Discography" and "Discothèque Idéale Qobuz" all hash - * to the same key for their respective aliases. + * No hardcoded name→id mappings — the catalog is entirely what the + * Qobuz API returns in the user's current session locale, so the + * match is agnostic and always in sync with whatever Qobuz is + * currently labelling awards. */ import { invoke } from '@tauri-apps/api/core'; @@ -20,40 +20,10 @@ interface ExploreResponse { } const PAGE_SIZE = 100; - -/** Known Qobuz editorial awards. IDs captured from the API (stable, - * locale-independent). Names include every locale we ship. */ -const SEED_AWARDS: Array<{ id: string; aliases: string[] }> = [ - { - id: '88', - aliases: ['Qobuzissime'], - }, - { - id: '151', - aliases: [ - 'Qobuz Album of the Week', - 'Álbum de la semana Qobuz', - 'Album der Woche Qobuz', - 'Album de la semaine Qobuz', - 'Álbum da semana Qobuz', - ], - }, - { - id: '70', - aliases: [ - 'Qobuz Ideal Discography', - 'Discoteca Ideal Qobuz', - 'Diskografie Qobuz', - 'Ideale Qobuz-Diskografie', - 'Discothèque idéale Qobuz', - 'Discografia ideal Qobuz', - ], - }, -]; +const MAX_PAGES = 40; // 4000 items ceiling — safety net, real catalog is ~hundreds // normalized name → id let byName = new Map(); -let seeded = false; let catalogLoaded = false; let inflight: Promise | null = null; @@ -66,24 +36,15 @@ function normalize(name: string): string { .replace(/\s+/g, ' '); } -function seed() { - if (seeded) return; - for (const entry of SEED_AWARDS) { - for (const alias of entry.aliases) { - byName.set(normalize(alias), entry.id); - } - } - seeded = true; -} - async function loadCatalog(): Promise { if (catalogLoaded) return; if (inflight) return inflight; - seed(); inflight = (async () => { let offset = 0; - while (true) { + let pages = 0; + let totalSeen = 0; + while (pages < MAX_PAGES) { try { const result = await invoke('v2_get_award_explore', { limit: PAGE_SIZE, @@ -94,43 +55,49 @@ async function loadCatalog(): Promise { if (item?.id == null || !item?.name) continue; byName.set(normalize(item.name), String(item.id)); } + totalSeen += items.length; + pages += 1; if (items.length < PAGE_SIZE) break; if (result.has_more === false) break; offset += items.length; - // Reasonable upper bound to avoid infinite loops on a broken API. - if (offset >= 2000) break; } catch (err) { - console.error('[AwardCatalog] explore failed:', err); + console.error('[AwardCatalog] /award/explore failed at offset', offset, err); break; } } catalogLoaded = true; inflight = null; + console.log(`[AwardCatalog] cached ${byName.size} distinct name keys from ${totalSeen} items across ${pages} page(s)`); })(); return inflight; } /** - * Return the award id for a given name. Tries the seeded mapping - * first (instant for editorial Qobuz awards in any locale), then - * falls back to the /award/explore catalog (fetched once per - * session). Resolves to null if still not found. + * Return the award id for a given name. Fetches the /award/explore + * catalog on first call (cache-first afterwards). Resolves to null + * if the name isn't in the catalog. */ export async function resolveAwardIdByName(name: string): Promise { if (!name) return null; - seed(); const key = normalize(name); - const fromSeed = byName.get(key); - if (fromSeed) return fromSeed; + + // Cache-first: if we already resolved this name (either from a prior + // lookup or from the catalog), return immediately. + const cached = byName.get(key); + if (cached) return cached; + await loadCatalog(); - return byName.get(key) ?? null; + const hit = byName.get(key) ?? null; + if (!hit) { + console.warn(`[AwardCatalog] no match for award name "${name}" (normalized: "${key}") among ${byName.size} cached entries`); + } + return hit; } -/** Synchronous lookup — hits only the seed + already-loaded catalog. */ +/** Synchronous lookup — hits only the already-loaded catalog. */ export function lookupAwardIdByName(name: string): string | null { if (!name) return null; - seed(); return byName.get(normalize(name)) ?? null; }