import React, { useState, useEffect } from 'react'; import { __ } from '@wordpress/i18n'; import { X, Copy, Check, Tag, Clock } from 'lucide-react'; import { toast } from 'sonner'; const COUPON_CODE = 'TPTHANKS25'; const TIMER_SECONDS = 10 * 60; // 10 minutes const TIMER_KEY = 'thumbpress_exit_popup_expiry'; function getRemainingSeconds(): number { const expiry = sessionStorage.getItem( TIMER_KEY ); if ( expiry ) { const remaining = Math.floor( ( parseInt( expiry ) - Date.now() ) / 1000 ); return Math.max( 0, remaining ); } const expiryTs = Date.now() + TIMER_SECONDS * 1000; sessionStorage.setItem( TIMER_KEY, String( expiryTs ) ); return TIMER_SECONDS; } function formatTime( seconds: number ): string { const m = Math.floor( seconds / 60 ).toString().padStart( 2, '0' ); const s = ( seconds % 60 ).toString().padStart( 2, '0' ); return `${ m }:${ s }`; } interface Props { onClose: () => void; onScrollToPricing: () => void; } export default function ExitIntentPopup( { onClose, onScrollToPricing }: Props ) { const [ copied, setCopied ] = useState( false ); const [ timeLeft, setTimeLeft ] = useState( getRemainingSeconds ); useEffect( () => { if ( timeLeft <= 0 ) return; const id = setInterval( () => { setTimeLeft( getRemainingSeconds() ); }, 1000 ); return () => clearInterval( id ); }, [] ); const copyCode = ( onDone?: () => void ) => { const done = () => { onDone?.(); }; if ( navigator.clipboard ) { navigator.clipboard.writeText( COUPON_CODE ).then( done ).catch( () => fallbackCopy( done ) ); } else { fallbackCopy( done ); } }; const handleCopy = () => { copyCode( () => { setCopied( true ); setTimeout( () => setCopied( false ), 2000 ); } ); }; const fallbackCopy = ( done: () => void ) => { const el = document.createElement( 'textarea' ); el.value = COUPON_CODE; el.style.position = 'fixed'; el.style.opacity = '0'; document.body.appendChild( el ); el.focus(); el.select(); try { document.execCommand( 'copy' ); done(); } catch {} document.body.removeChild( el ); }; const handleGetDiscount = () => { copyCode( () => toast.success( __( 'Coupon code TPTHANKS25 copied!', 'image-sizes' ), { description: __( 'Paste it at checkout for 25% off.', 'image-sizes' ) } ) ); onScrollToPricing(); }; const expired = timeLeft <= 0; return (
e.stopPropagation() } >

{ __( 'A Special Offer Just for You!', 'image-sizes' ) }

{ __( 'As an existing user, we\'re giving you an exclusive 25% discount on ThumbPress Pro. Copy the code and use it at checkout!', 'image-sizes' ) }

{ /* Countdown timer */ }
{ expired ? __( 'Offer expired', 'image-sizes' ) : `${ __( 'Offer expires in', 'image-sizes' ) } ${ formatTime( timeLeft ) }` }
{ COUPON_CODE }
); }