'use client'; /** * Non-binding tax preview for the cart. * * Shoppers in VAT markets expect a tax figure before they reach the address * form, and "calculated at checkout" reads as an unpleasant surprise waiting * to happen. `estimateTax` gives them a number early without pretending it is * final. * * ⛔ NEVER PRESENT THIS AS THE REAL TAX. The authoritative calculation runs at * checkout against the full shipping address. A preview has no province, so a * Canadian estimate shows federal GST alone and the provincial PST/QST joins * it at checkout. That is what `note` is for, and why the "calculated at * checkout" caveat stays on the row even when a figure is showing. * * ⛔ NO ESTIMATE IS RENDERED when `appliesTax` is false, which is what an * empty or freshly created store returns: tax is off, no country reached us, * or no active rate covers it. In that case (and when the call fails, and * while it is in flight, and on an empty cart) this falls back to EXACTLY the * row the cart shipped with before the estimate existed: "Tax / calculated at * checkout". There is no spinner, no error box, and no empty amount. * * `getStoreTaxClasses()` would let you add a separate "9% VAT" transparency * badge, but `rateName` + `rate` already come back on this response and say * the same thing, so a second request would buy nothing. */ import { useEffect, useState } from 'react'; import type { TaxEstimateResponse } from 'brainerce'; import { formatPrice } from 'brainerce'; import { getClient } from '@/core/lib/brainerce'; import { useTranslations } from '@/core/lib/translations'; import { useCart, useRegion } from '@/core/providers/store-provider'; export function useTaxEstimate(): TaxEstimateResponse | null { const { totals } = useCart(); // The same country the region lookup already extracted from the edge. Asking // for it twice would be two sources that can disagree. const { buyerCountry } = useRegion(); const [estimate, setEstimate] = useState(null); const subtotal = totals.subtotal; useEffect(() => { // An empty cart has nothing to tax. Skipping the call also keeps a // brand-new store from firing a request on every page load forever. if (!(subtotal > 0)) { setEstimate(null); return; } let cancelled = false; getClient() .estimateTax({ ...(buyerCountry ? { country: buyerCountry } : {}), subtotal }) .then((result) => { if (!cancelled) setEstimate(result); }) .catch(() => { // Swallowed on purpose. A tax preview is a courtesy; it must never // take the cart down with it. The caller falls back to the plain // "calculated at checkout" row. if (!cancelled) setEstimate(null); }); return () => { cancelled = true; }; }, [subtotal, buyerCountry]); return estimate; } export function TaxEstimateLine() { const t = useTranslations('cart'); const tc = useTranslations('common'); const estimate = useTaxEstimate(); // No estimate: the row the cart has always shown. if (!estimate || !estimate.appliesTax) { return (
{tc('tax')} {t('taxAtCheckout')}
); } // `rateName` is one rate or several joined ("GST + QST"); `rate` is the // summed effective percent and is null when no rule matched. const label = estimate.rateName && estimate.rate != null ? `${estimate.rateName} ${estimate.rate}%` : (estimate.rateName ?? tc('tax')); return (
{estimate.pricesIncludeTax ? `${label} ${tc('inclSuffix')}` : label} {formatPrice(estimate.estimatedTax, { currency: estimate.currency }) as string}
{/* The caveat travels WITH the number, not in a footnote nobody reads. A VAT-inclusive store has nothing left to calculate: the tax is already inside the prices above, so "calculated at checkout" would promise an extra charge that never comes. */} {estimate.pricesIncludeTax ? null : (

{t('taxAtCheckout')}

)} {/* Server-written and already localized. Never replace it with copy of your own, it is what explains a partial estimate. */} {estimate.note ?

{estimate.note}

: null}
); }