'use client'; import type { ShippingRate } from 'brainerce'; import { formatPrice } from 'brainerce'; import { useTranslations } from '@/core/lib/translations'; import { useCurrency } from '@/core/lib/use-currency'; import { cn } from '@/core/lib/utils'; interface ShippingStepProps { rates: ShippingRate[]; selectedRateId: string | null; onSelect: (rateId: string) => void; loading?: boolean; /** * Currency to print the prices in, with `amountScale` the multiplier from the store currency * (`checkout.presentment.fxChargingRate`). Rates arrive in the store currency; a checkout charged in * the region currency passes both so the list reads in the same currency as the summary beside it. */ currency?: string; amountScale?: number; className?: string; } /** * Live carrier rates arrive labelled with the carrier's own service identifier * — `USPS PriorityMailInternational`, `USAExportPBA USAExportStandard` — which * answers a question no shopper asked. They are choosing between how fast and * how much, so we render `speedTier` in the store's own language instead. * * Manual zone rates carry no `speedTier`: the merchant named those deliberately, * so those keep `rate.name` exactly as written. */ const TIER_KEYS = { cheapest: 'shippingTierCheapest', balanced: 'shippingTierBalanced', fastest: 'shippingTierFastest', } as const; export function ShippingStep({ rates, selectedRateId, onSelect, loading = false, currency: currencyOverride, amountScale, className, }: ShippingStepProps) { const t = useTranslations('checkout'); const tc = useTranslations('common'); const storeCurrency = useCurrency(); const currency = currencyOverride ?? storeCurrency; if (rates.length === 0) { return (

{t('noShippingOptions')}

{t('noShippingOptionsHint')}

); } // A rate with `requiresPickupPoint` delivers to a carrier point the shopper picks next, the rest // deliver to the address. Headings appear only when the shopper actually has both to choose from. const pickupRates = rates.filter((rate) => rate.requiresPickupPoint); const homeRates = rates.filter((rate) => !rate.requiresPickupPoint); const grouped = pickupRates.length > 0 && homeRates.length > 0; const renderRate = (rate: ShippingRate) => { const price = parseFloat(rate.price); const isFree = price === 0; // Same arithmetic the checkout uses for its shipping row (store amount times the pinned charging rate, // to the cent), so the figure here is the one the summary shows once the rate is picked. const shownPrice = amountScale != null ? Math.round(price * amountScale * 100) / 100 : price; const isSelected = selectedRateId === rate.id; const label = rate.speedTier ? t(TIER_KEYS[rate.speedTier]) : rate.name; return ( ); }; if (!grouped) { return
{rates.map(renderRate)}
; } return (

{t('shippingGroupHome')}

{homeRates.map(renderRate)}

{t('shippingGroupPickup')}

{pickupRates.map(renderRate)}
); }