fixed a hydratioin issue

This commit is contained in:
2025-12-30 14:14:43 -05:00
parent e69205e6f8
commit 85d4629d3c
+24 -22
View File
@@ -420,33 +420,35 @@ export default function GunbuilderPage() {
const data: GunbuilderProductFromApi[] = await res.json(); const data: GunbuilderProductFromApi[] = await res.json();
const hydrated: Part[] = data // Map products by id for quick lookup
.map((p): Part | null => { const byId = new Map<string, GunbuilderProductFromApi>();
const normalizedRole = (p.partRole ?? "") for (const p of data) byId.set(String(p.id), p);
.trim()
.toLowerCase()
.replace(/_/g, "-");
const categoryId = resolveCategoryId(normalizedRole); // Build the hydrated parts by iterating the BUILD STATE (slot -> id)
if (!categoryId) return null; const hydrated: Part[] = Object.entries(build)
.filter(([, id]) => Boolean(id))
.map(([slot, id]) => {
const p = byId.get(String(id));
if (!p) return null;
const buyUrl = p.buyUrl ?? undefined; const buyUrl = p.buyUrl ?? undefined;
return { return {
id: String(p.id), id: String(p.id),
categoryId, categoryId: slot as any, // ✅ slot is the source of truth
name: p.name, name: p.name,
brand: p.brand, brand: p.brand,
price: p.price ?? 0, price: p.price ?? 0,
imageUrl: p.imageUrl ?? undefined, imageUrl: p.imageUrl ?? undefined,
affiliateUrl: buyUrl, affiliateUrl: buyUrl,
url: buyUrl, url: buyUrl,
notes: undefined, notes: undefined,
}; } as Part;
}) })
.filter((p): p is Part => p !== null); .filter((x): x is Part => x !== null);
setParts(hydrated); setParts(hydrated);
} catch (err: any) { } catch (err: any) {
if (err?.name === "AbortError") return; if (err?.name === "AbortError") return;
setError(err?.message ?? "Failed to hydrate selected parts"); setError(err?.message ?? "Failed to hydrate selected parts");