import { Editable } from '@components/common/page-builder/index.js'; import { buttonVariants } from '@components/common/ui/Button.js'; import { cn } from '@evershop/evershop/lib/util/cn'; import React from 'react'; /** * Coupon block — a centred promotional panel with a copy-able code and a * CTA. The visual code box + Copy button + Shop CTA pattern consistently * outperforms hidden automatic discounts. * * Expiry handling: when set, an inline script reads the embedded * `data-expires` attribute on page load and hides the widget if the time * has passed. SSR always renders the block; we don't want to invalidate * the SSR cache every time someone's coupon ends. The script handles the * real-time suppression for live visitors. */ export type CouponBorderStyle = 'solid' | 'dashed' | 'none'; export interface CouponBlockProps { couponBlockWidget: { eyebrow: string | null; heading: string; body: string | null; code: string; ctaLabel: string | null; ctaLink: string; ctaNewTab: boolean | null; expires: string | null; borderStyle: CouponBorderStyle | null; backgroundColor: string | null; }; } const BORDER_CLASS: Record = { solid: 'border border-foreground/20', dashed: 'border border-dashed border-foreground/30', none: '' }; // Self-contained script: reads `data-expires`, hides widget when past. // Inlined as a string so SSR ships it without hydration boundaries. const EXPIRY_SCRIPT = `(() => { document.querySelectorAll('[data-evershop-coupon-expires]').forEach((el) => { const exp = el.getAttribute('data-evershop-coupon-expires'); if (!exp) return; const t = Date.parse(exp); if (Number.isFinite(t) && Date.now() >= t) { el.setAttribute('hidden', ''); } }); })();`; // Self-contained script: copy-to-clipboard handler. Falls back to // selection if clipboard API is unavailable (non-HTTPS or denied). const COPY_SCRIPT = `(() => { document.querySelectorAll('[data-evershop-coupon-copy]').forEach((btn) => { btn.addEventListener('click', async () => { const code = btn.getAttribute('data-evershop-coupon-copy') || ''; const label = btn.querySelector('[data-evershop-coupon-copy-label]'); const original = label ? label.textContent : 'Copy'; const setLabel = (t) => { if (label) label.textContent = t; }; try { if (navigator.clipboard) { await navigator.clipboard.writeText(code); } else { throw new Error('no clipboard'); } setLabel('Copied ✓'); } catch (_) { const box = document.getElementById(btn.getAttribute('data-evershop-coupon-copy-target') || ''); if (box) { const range = document.createRange(); range.selectNodeContents(box); const sel = window.getSelection(); if (sel) { sel.removeAllRanges(); sel.addRange(range); } } setLabel('Press ⌘C'); } setTimeout(() => setLabel(original || 'Copy'), 2000); }); }); })();`; export default function CouponBlock({ couponBlockWidget }: CouponBlockProps) { const { eyebrow, heading, body, code, ctaLabel, ctaLink, ctaNewTab, expires, borderStyle, backgroundColor } = couponBlockWidget; if (!code || !heading) return null; const codeUpper = code.toUpperCase(); const codeBoxId = `coupon-code-${code .replace(/[^a-z0-9]/gi, '') .toLowerCase()}`; const borderClass = BORDER_CLASS[borderStyle ?? 'dashed']; return ( <>
{eyebrow && ( {eyebrow} )} {heading} {body && ( {body} )}
{codeUpper}
{ctaLabel || 'Shop now →'}