fixed a hydratioin issue

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