fixing platform switcher bugs
This commit is contained in:
@@ -21,7 +21,10 @@ import Pagination from "@/components/parts/Pagination";
|
||||
import PlatformSwitcher from "@/components/parts/PlatformSwitcher";
|
||||
|
||||
import type { CategoryId } from "@/types/gunbuilder";
|
||||
import { PART_ROLE_TO_CATEGORY, normalizePartRole } from "@/lib/catalogMappings";
|
||||
import {
|
||||
PART_ROLE_TO_CATEGORY,
|
||||
normalizePartRole,
|
||||
} from "@/lib/catalogMappings";
|
||||
|
||||
type ViewMode = "card" | "list";
|
||||
type SortOption = "relevance" | "price-asc" | "price-desc" | "brand-asc";
|
||||
@@ -196,7 +199,8 @@ export default function PartsBrowseClient(props: {
|
||||
);
|
||||
|
||||
const priceBounds = useMemo(() => {
|
||||
if (!parts.length) return { min: null as number | null, max: null as number | null };
|
||||
if (!parts.length)
|
||||
return { min: null as number | null, max: null as number | null };
|
||||
return {
|
||||
min: Math.min(...parts.map((p) => p.price)),
|
||||
max: Math.max(...parts.map((p) => p.price)),
|
||||
@@ -208,11 +212,13 @@ export default function PartsBrowseClient(props: {
|
||||
const minBound = priceBounds.min;
|
||||
const maxBound = priceBounds.max;
|
||||
if (minBound == null || maxBound == null) return;
|
||||
|
||||
|
||||
setPriceRange((prev) => {
|
||||
const nextMin = prev.min == null ? minBound : Math.max(minBound, prev.min);
|
||||
const nextMax = prev.max == null ? maxBound : Math.min(maxBound, prev.max);
|
||||
|
||||
const nextMin =
|
||||
prev.min == null ? minBound : Math.max(minBound, prev.min);
|
||||
const nextMax =
|
||||
prev.max == null ? maxBound : Math.min(maxBound, prev.max);
|
||||
|
||||
// Ensure min never exceeds max (in case user typed weird values)
|
||||
return {
|
||||
min: Math.min(nextMin, nextMax),
|
||||
@@ -232,15 +238,13 @@ export default function PartsBrowseClient(props: {
|
||||
if (brandFilter.length)
|
||||
result = result.filter((p) => brandFilter.includes(p.brand));
|
||||
|
||||
if (inStockOnly)
|
||||
result = result.filter((p) => p.inStock ?? true);
|
||||
if (inStockOnly) result = result.filter((p) => p.inStock ?? true);
|
||||
|
||||
const q = searchQuery.toLowerCase().trim();
|
||||
if (q)
|
||||
result = result.filter(
|
||||
(p) =>
|
||||
p.name.toLowerCase().includes(q) ||
|
||||
p.brand.toLowerCase().includes(q)
|
||||
p.name.toLowerCase().includes(q) || p.brand.toLowerCase().includes(q)
|
||||
);
|
||||
|
||||
switch (sortBy) {
|
||||
@@ -318,12 +322,22 @@ export default function PartsBrowseClient(props: {
|
||||
</p>
|
||||
|
||||
{!isBuilderMode && (
|
||||
<div className="mt-3">
|
||||
<div className="mt-4 flex flex-wrap items-center gap-3">
|
||||
<PlatformSwitcher
|
||||
currentPlatform={effectivePlatform}
|
||||
partRole={partRole}
|
||||
preserveQuery
|
||||
mode="browse"
|
||||
/>
|
||||
|
||||
<Link
|
||||
href={`/builder?platform=${encodeURIComponent(
|
||||
effectivePlatform
|
||||
)}`}
|
||||
className="inline-flex items-center rounded-md bg-amber-400 px-3 py-2 text-xs font-semibold text-black hover:bg-amber-300 transition-colors"
|
||||
>
|
||||
Start New Build →
|
||||
</Link>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
@@ -331,7 +345,9 @@ export default function PartsBrowseClient(props: {
|
||||
{isBuilderMode && (
|
||||
<div className="flex items-center gap-2">
|
||||
<Link
|
||||
href={`/builder?platform=${encodeURIComponent(effectivePlatform)}`}
|
||||
href={`/builder?platform=${encodeURIComponent(
|
||||
effectivePlatform
|
||||
)}`}
|
||||
className="rounded-md border border-zinc-700 bg-zinc-900/70 px-3 py-2 text-xs font-semibold hover:bg-zinc-800"
|
||||
>
|
||||
← Back to Build
|
||||
@@ -399,7 +415,9 @@ export default function PartsBrowseClient(props: {
|
||||
)}
|
||||
|
||||
{loading ? (
|
||||
<p className="py-8 text-center text-sm text-zinc-500">Loading…</p>
|
||||
<p className="py-8 text-center text-sm text-zinc-500">
|
||||
Loading…
|
||||
</p>
|
||||
) : error ? (
|
||||
<p className="py-8 text-center text-sm text-red-400">{error}</p>
|
||||
) : filteredParts.length === 0 ? (
|
||||
@@ -434,4 +452,4 @@ export default function PartsBrowseClient(props: {
|
||||
</div>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user