diff --git a/src/components/courses/DiscountProgressBar.tsx b/src/components/courses/DiscountProgressBar.tsx index eca8309a..88657a80 100644 --- a/src/components/courses/DiscountProgressBar.tsx +++ b/src/components/courses/DiscountProgressBar.tsx @@ -43,26 +43,40 @@ const TierIcon = ({ type }: { type: DiscountTier['icon'] }) => { }; export default function DiscountProgressBar({ currentSpend }: DiscountProgressBarProps) { - const maxThreshold = DISCOUNT_TIERS[DISCOUNT_TIERS.length - 1].threshold; - const normalizedSpend = Math.round(currentSpend * 100) / 100; - const progressPercent = Math.min((normalizedSpend / maxThreshold) * 100, 100); + const maxThreshold = useMemo(() => DISCOUNT_TIERS[DISCOUNT_TIERS.length - 1].threshold, []); + const normalizedSpend = useMemo(() => Math.round(currentSpend * 100) / 100, [currentSpend]); + const progressPercent = useMemo( + () => Math.min((normalizedSpend / maxThreshold) * 100, 100), + [normalizedSpend, maxThreshold] + ); // Find the next tier the user hasn't unlocked yet const nextTier = useMemo( () => DISCOUNT_TIERS.find((tier) => normalizedSpend < tier.threshold), - [normalizedSpend], + [normalizedSpend] ); // Find all unlocked tiers const unlockedTiers = useMemo( () => DISCOUNT_TIERS.filter((tier) => normalizedSpend >= tier.threshold), - [normalizedSpend], + [normalizedSpend] + ); + + const amountToNext = useMemo( + () => (nextTier ? Math.max(0, nextTier.threshold - normalizedSpend).toFixed(2) : null), + [nextTier, normalizedSpend] ); - const amountToNext = nextTier - ? Math.max(0, nextTier.threshold - normalizedSpend).toFixed(2) - : null; - const allUnlocked = !nextTier; + const allUnlocked = useMemo(() => !nextTier, [nextTier]); + + // Memoize tier list with unlocked status and computed positions + const memoizedTiers = useMemo(() => { + return DISCOUNT_TIERS.map((tier) => ({ + ...tier, + isUnlocked: normalizedSpend >= tier.threshold, + markerPercent: (tier.threshold / maxThreshold) * 100, + })); + }, [normalizedSpend, maxThreshold]); return (
{/* Tier markers */} - {DISCOUNT_TIERS.map((tier) => { - const markerPercent = (tier.threshold / maxThreshold) * 100; - const isUnlocked = currentSpend >= tier.threshold; + {memoizedTiers.map((tier) => { return (