diff --git a/estore/src/components/ProductCard.tsx b/estore/src/components/ProductCard.tsx index ccb408d..8c82988 100644 --- a/estore/src/components/ProductCard.tsx +++ b/estore/src/components/ProductCard.tsx @@ -10,15 +10,18 @@ interface Props { item: CatalogItem } -/** Lowest stock count across tracked variations. null = nothing tracked. */ -function lowestTrackedInventory(item: CatalogItem): number | null { - const tracked = item.variations.filter((v) => v.inventory !== null) - if (!tracked.length) return null - return Math.min(...tracked.map((v) => v.inventory as number)) -} - const LOW_STOCK_THRESHOLD = 5 +function stockStats(item: CatalogItem): { soldOut: boolean; lowStock: boolean; lowestAvailable: number | null } { + const tracked = item.variations.filter((v) => v.inventory !== null) + if (!tracked.length) return { soldOut: false, lowStock: false, lowestAvailable: null } + const allSoldOut = tracked.every((v) => (v.inventory as number) <= 0) + const available = tracked.filter((v) => (v.inventory as number) > 0) + const lowestAvailable = available.length ? Math.min(...available.map((v) => v.inventory as number)) : null + const lowStock = lowestAvailable !== null && lowestAvailable <= LOW_STOCK_THRESHOLD + return { soldOut: allSoldOut, lowStock, lowestAvailable } +} + export default function ProductCard({ item }: Props) { const [showPicker, setShowPicker] = useState(false) // Prefer admin-set colorMax; fall back to name-based heuristic @@ -26,9 +29,7 @@ export default function ProductCard({ item }: Props) { ? (item.colorMax !== null && item.colorMax !== undefined ? item.colorMax : maxColorsFor(item.name)) : null - const stock = lowestTrackedInventory(item) - const soldOut = stock !== null && stock <= 0 - const lowStock = stock !== null && stock > 0 && stock <= LOW_STOCK_THRESHOLD + const { soldOut, lowStock, lowestAvailable: stock } = stockStats(item) const priceDisplay = item.price ? `From ${fmt(item.price)}` : 'Custom quote'