'use client'; import { Check } from 'lucide-react'; import { formatPrice } from 'brainerce'; import { useStoreInfo, useCart } from '@/core/providers/store-provider'; import { useCurrency } from '@/core/lib/use-currency'; import { useTranslations } from '@/core/lib/translations'; import { cn } from '@/core/lib/utils'; interface FreeShippingBarProps { className?: string; } export function FreeShippingBar({ className }: FreeShippingBarProps) { // Hooks must be called unconditionally and in the same order on every // render — keep all of them above any early `return null` branch. const t = useTranslations('cart'); const { storeInfo } = useStoreInfo(); const { totals } = useCart(); const currency = useCurrency(); const upsell = storeInfo?.upsell; const threshold = upsell?.freeShippingThreshold; const enabled = upsell?.freeShippingBarEnabled !== false; // Don't render if disabled or no threshold configured if (!enabled || !threshold || threshold <= 0) return null; const subtotal = totals.subtotal; const remaining = Math.max(0, threshold - subtotal); const progress = Math.min(100, (subtotal / threshold) * 100); const qualified = remaining <= 0; // Don't show if already qualified if (qualified) { return (
); } return (

{t('freeShippingRemaining', { amount: formatPrice(remaining, { currency }) as string, })}

); }