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})`); if (!res.ok) throw new Error(`Failed to hydrate parts (${res.status})`);
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() // Build the hydrated parts by iterating the BUILD STATE (slot -> id)
.replace(/_/g, "-"); const hydrated: Part[] = Object.entries(build)
.filter(([, id]) => Boolean(id))
const categoryId = resolveCategoryId(normalizedRole); .map(([slot, id]) => {
if (!categoryId) return null; const p = byId.get(String(id));
if (!p) return null;
const buyUrl = p.buyUrl ?? undefined;
const buyUrl = p.buyUrl ?? undefined;
return {
id: String(p.id), return {
categoryId, id: String(p.id),
name: p.name, categoryId: slot as any, // ✅ slot is the source of truth
brand: p.brand, name: p.name,
price: p.price ?? 0, brand: p.brand,
imageUrl: p.imageUrl ?? undefined, price: p.price ?? 0,
affiliateUrl: buyUrl, imageUrl: p.imageUrl ?? undefined,
url: buyUrl, affiliateUrl: buyUrl,
notes: undefined, url: buyUrl,
}; 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");