'use client'; /** * Newsletter signup — confirmed opt-in. * * ⛔ THIS DOES NOT SUBSCRIBE ANYONE. `marketing.subscribe()` creates the contact * and mails them a confirmation link; the address is unmailable, and invisible * to every campaign audience, until the recipient clicks it. That is why the * success state below says "check your email" and never "you're subscribed" — * the second is untrue until a click lands in a mailbox this code cannot see, * and most people never click. * * ⛔ THE RESPONSE CARRIES NO INFORMATION. `{ ok: true }` comes back identically * for a brand-new address, one that confirmed months ago, one still inside its * 24-hour resend cooldown, and one suppressed after a hard bounce. That is * deliberate: a response that distinguished them would turn this public form * into a way to test whether a given person shops here. There is nothing to * branch on — render one success state and stop. * * Naming the spam folder is not padding. A filtered confirmation is the single * commonest reason a signup never becomes a subscriber, and the resend cooldown * means no second copy arrives for 24 hours. * * **The welcome offer.** `marketing.getBenefit()` returns what the merchant is * offering people who confirm, or `null` when they offer nothing. Show its * terms beside the field so the shopper knows what they are signing up for. * * ⛔ DO NOT RENDER A COUPON CODE HERE, now or after the success state. No coupon * exists at either moment. It is minted when the recipient clicks the link in * their own mailbox and is emailed to them there — which is exactly what stops a * forwarded link handing the discount to someone who never asked. The * confirmation page is served by the API and already shows the code, its expiry * and its terms, so there is nothing here to poll for either. * * ⛔ There is no eligibility check to call, deliberately: a per-address answer * would let anyone test who already subscribed. The offer is one per address per * store, forever. Say so in the terms rather than trying to detect it. * * Rate limited to 3 requests / 60s per IP. */ import { useEffect, useMemo, useState } from 'react'; import type { PublicNewsletterBenefitOffer } from 'brainerce'; import { getClient } from '@/core/lib/brainerce'; import { useTranslations } from '@/core/lib/translations'; export function NewsletterSignup() { const t = useTranslations('newsletter'); const [email, setEmail] = useState(''); const [honeypot, setHoneypot] = useState(''); const [loading, setLoading] = useState(false); const [done, setDone] = useState(false); const [error, setError] = useState(null); // null covers both "no offer configured" and "the read failed". Neither is // worth an error state on a footer signup: the form still works, it just // promises nothing, and promising nothing is always safe. const [offer, setOffer] = useState(null); // Same source the contact form uses — is set from the active // locale, and passing it is what decides the language of the confirmation // email. Omit it on a Hebrew storefront and the shopper gets English. const locale = useMemo(() => { if (typeof document !== 'undefined') return document.documentElement.lang || undefined; return undefined; }, []); // Read once per mount. The offer belongs to the store, not to the visitor, // so re-reading it per keystroke or per submit is wasted work. useEffect(() => { let cancelled = false; getClient() .marketing.getBenefit(locale) .then((result) => { // Only a complete, enabled offer is ever shown. An empty or partial body // (an older backend, a half-configured benefit) must read as "no offer", // never as "undefined off your first order". const usable = result && result.enabled === true && Number.isFinite(result.discountValue); if (!cancelled) setOffer(usable ? result : null); }) .catch(() => { // Silent on purpose. A failed offer read must not stop someone // subscribing, and there is nothing the shopper could do about it. }); return () => { cancelled = true; }; }, [locale]); const offerHeadline = offer ? offer.headline?.trim() || (offer.discountType === 'PERCENTAGE' ? t('offerPercent', { value: String(offer.discountValue) }) : t('offerAmount', { value: String(offer.discountValue) })) : null; async function handleSubmit(e: React.FormEvent) { e.preventDefault(); if (loading || !email.trim()) return; setLoading(true); setError(null); try { await getClient().marketing.subscribe({ email: email.trim(), locale, source: 'footer', honeypot, }); setDone(true); setEmail(''); } catch { // A 429 lands here too — the copy stays generic rather than explaining // the rate limit, which would only tell an abuser where the edge is. setError(t('genericError')); } finally { setLoading(false); } } if (done) { return (

{t('checkEmailTitle')}

{t('checkEmailBody')}

{/* Says where the code comes from, without ever showing one. The coupon is created by the confirmation click and arrives in the same inbox, so "check your email" is the whole instruction. */} {offer ?

{t('offerByEmail')}

: null}
); } return (

{t('subtitle')}

{/* The merchant's own words when they wrote any, a plain statement of the discount when they did not. Both are the promise the shopper is agreeing to, so they belong above the input, not under the button. */} {offerHeadline ? (

{offerHeadline}

{offer?.terms ?

{offer.terms}

: null}
) : null}
setEmail(e.target.value)} placeholder={t('placeholder')} className="border-border min-w-0 flex-1 rounded border px-3 py-2 text-sm" />
{/* Honeypot. Bots complete every text input; a human never sees this one, so a non-empty value rejects the request server-side. Positioned off-screen rather than `display:none` — some bots skip hidden inputs — and kept out of the tab order and the accessibility tree. */} setHoneypot(e.target.value)} style={HONEYPOT_STYLE} /> {error ? (

{error}

) : null}
); } /** * Honeypot field styling. It must stay in the DOM (bots fill any input) but never be seen or * reached. Do NOT park it at `left: -9999px`: in an RTL page that is the scrolling side, so it * stretches the document to ~10,000px and lets a Hebrew visitor scroll sideways into blank space. * A clipped 1px box inside its container adds no scrollable overflow in either direction. */ const HONEYPOT_STYLE = { position: 'absolute', insetInlineStart: 0, width: 1, height: 1, margin: -1, padding: 0, border: 0, overflow: 'hidden', opacity: 0, clipPath: 'inset(50%)', pointerEvents: 'none', } as const;