import { __ } from "@wordpress/i18n"; import { createPortal, useState, useEffect, useCallback, } from "@wordpress/element"; import { PRO_FEATURES, PROMO_CONFIG, getFeatureImage } from "./upsell-data"; import { upsellIcon } from "./upsell-icon"; declare global { interface Window { sliderbergData?: { isPro: boolean; upgradeUrl: string; upsellImages?: Record< string, string >; }; } } const ChevronLeft = () => ( ); const ChevronRight = () => ( ); interface UpsellModalProps { initialFeature: string; onClose: () => void; } function UpsellModal( { initialFeature, onClose }: UpsellModalProps ) { const pricingLink = window.sliderbergData?.upgradeUrl ?? "https://sliderberg.com/pricing"; const { code: promoCode, text: promoText } = PROMO_CONFIG; const totalFeatures = PRO_FEATURES.length; const startIndex = PRO_FEATURES.findIndex( ( f ) => f.name === initialFeature ); const [ activeIndex, setActiveIndex ] = useState( startIndex > -1 ? startIndex : 0, ); const [ slideDirection, setSlideDirection ] = useState( "" ); const [ slideKey, setSlideKey ] = useState( 0 ); const goToPrev = useCallback( () => { setSlideDirection( "slide-left" ); setSlideKey( ( k ) => k + 1 ); setActiveIndex( ( prev ) => ( prev > 0 ? prev - 1 : totalFeatures - 1 ) ); }, [ totalFeatures ] ); const goToNext = useCallback( () => { setSlideDirection( "slide-right" ); setSlideKey( ( k ) => k + 1 ); setActiveIndex( ( prev ) => prev < totalFeatures - 1 ? prev + 1 : 0, ); }, [ totalFeatures ] ); useEffect( () => { const handleKeyDown = ( e: KeyboardEvent ) => { if ( e.key === "Escape" ) onClose(); if ( e.key === "ArrowLeft" ) goToPrev(); if ( e.key === "ArrowRight" ) goToNext(); }; document.addEventListener( "keydown", handleKeyDown ); return () => document.removeEventListener( "keydown", handleKeyDown ); }, [ onClose, goToPrev, goToNext ] ); const currentFeature = PRO_FEATURES[ activeIndex ]; return createPortal(
{ upsellIcon } { currentFeature.title }
{ getFeatureImage( currentFeature.name ) && ( { ) } { currentFeature.description && (

{ currentFeature.title }{ " " } { currentFeature.description }

) } { promoCode && promoText && (
{ promoText } { promoCode }
) }
{ e.preventDefault(); window.open( pricingLink, "_blank", "noopener,noreferrer" ); } } > { __( "Get Sliderberg PRO →", "sliderberg" ) }
, document.body, ); } export default UpsellModal;