refactor(award-catalog): remove hardcoded editorial seed, rely on /award/explore
User pushed back on the hardcoded list of Qobuz editorial award names across locales. Removed the seed map entirely — the catalog is now 100% what Qobuz returns from /award/explore in the user's current session locale, so id resolution always matches whatever labels the API is currently emitting. Kept: - Cache-first lookup (hit the in-memory map before touching the API). - Diacritic-insensitive + whitespace-agnostic normalization so accent or case differences don't miss a real match. - Single fetch per session, paginated. Added a console.log summary after the initial fetch and a warn when a name misses the catalog so future 'X award doesn't open' reports are debuggable without guessing. cc
This commit is contained in:
1 file changed
+32
-65
@@ -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<string, string>();
|
||||
let seeded = false;
|
||||
let catalogLoaded = false;
|
||||
let inflight: Promise<void> | 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<void> {
|
||||
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<ExploreResponse>('v2_get_award_explore', {
|
||||
limit: PAGE_SIZE,
|
||||
@@ -94,43 +55,49 @@ async function loadCatalog(): Promise<void> {
|
||||
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<string | null> {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user