"use client"; import { useEffect, useState, useRef, useCallback } from "react"; import { motion, AnimatePresence } from "framer-motion"; import { X, AlertTriangle } from "lucide-react"; import { useLanguage } from "@/components/LanguageContext"; import { useSplitFlapAudio } from "@/components/ui/split-flap-text"; import { registerWaitlist } from "@/app/actions/waitlist"; import { ShimmerButton } from "@/components/ui/shimmer-button"; import { CyberTicketModal } from "@/components/CyberTicketModal"; export function ExitIntentPopup() { const { t, language } = useLanguage(); const audio = useSplitFlapAudio(); const [isOpen, setIsOpen] = useState(false); const [email, setEmail] = useState(""); const [isSubmitting, setIsSubmitting] = useState(false); const [isTicketOpen, setIsTicketOpen] = useState(false); const [error, setError] = useState(null); const modalRef = useRef(null); useEffect(() => { // If already registered, don't track exit intent const isRegistered = localStorage.getItem("hubapi_waitlist_registered"); if (isRegistered === "true") return; // Check if exit intent was already shown in this session const exitShown = sessionStorage.getItem("hubapi_exit_intent_shown"); if (exitShown === "true") return; const handleMouseLeave = (e: MouseEvent) => { // Trigger when mouse moves out of window top boundary (y < 10) if (e.clientY < 10) { setIsOpen(true); sessionStorage.setItem("hubapi_exit_intent_shown", "true"); // Play audio warning beep if (audio && !audio.isMuted) { audio.playClick(); } } }; document.addEventListener("mouseleave", handleMouseLeave); return () => { document.removeEventListener("mouseleave", handleMouseLeave); }; }, [audio]); const handleClose = useCallback(() => { setIsOpen(false); if (audio && !audio.isMuted) { audio.playClick(); } }, [audio]); // Focus trap and escape key listener useEffect(() => { if (!isOpen) return; const modal = modalRef.current; if (!modal) return; // Focus the first element or the modal container itself const focusableElements = modal.querySelectorAll( 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])' ); const firstElement = focusableElements[0] as HTMLElement; const lastElement = focusableElements[focusableElements.length - 1] as HTMLElement; // Save previous active element to restore later const previousActiveElement = document.activeElement as HTMLElement; // Set a tiny timeout to ensure animation didn't disrupt focus const focusTimeout = setTimeout(() => { if (firstElement) { firstElement.focus(); } else { modal.focus(); } }, 50); const handleKeyDown = (e: KeyboardEvent) => { if (e.key === "Escape") { handleClose(); } if (e.key !== "Tab") return; if (e.shiftKey) { if (document.activeElement === firstElement) { lastElement.focus(); e.preventDefault(); } } else { if (document.activeElement === lastElement) { firstElement.focus(); e.preventDefault(); } } }; window.addEventListener("keydown", handleKeyDown); return () => { clearTimeout(focusTimeout); window.removeEventListener("keydown", handleKeyDown); if (previousActiveElement) { previousActiveElement.focus(); } }; }, [isOpen, handleClose]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(null); if (!email) { setError(language === "ar" ? "البريد الإلكتروني مطلوب." : "Email address is required."); return; } const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(email)) { setError(language === "ar" ? "يرجى إدخال بريد إلكتروني صالح." : "Please enter a valid email address."); return; } setIsSubmitting(true); try { const formData = new FormData(); formData.append("email", email); const result = await registerWaitlist(formData); if (result.success) { localStorage.setItem("hubapi_waitlist_registered", "true"); setIsOpen(false); setIsTicketOpen(true); } else { setError(result.error || "An error occurred. Please try again."); } } catch { setError("Failed to connect to waitlist service."); } finally { setIsSubmitting(false); } }; // handleClose is defined above with useCallback for effect compatibility return ( <> {isOpen && (
{/* Dark glass backdrop */} {/* Popup Dialog container */} {/* Glowing Corner Effects */}
{/* Close Button */}
{/* Heading Area */}
EXIT_PREVENTED

{t("exitTitle")}

{t("exitSubtitle")}

{/* Form Integration */}
{ setEmail(e.target.value); if (error) setError(null); }} className={`relative w-full h-12 px-5 bg-muted/40 dark:bg-white/5 border font-mono text-xs text-foreground placeholder:text-muted-foreground/40 outline-none focus-visible:outline-none focus-visible:ring-1 transition-colors duration-300 [clip-path:polygon(10px_0,100%_0,100%_calc(100%_-_10px),calc(100%_-_10px)_100%,0_100%,0_10px)] ${ error ? 'border-destructive focus-visible:ring-destructive focus-visible:ring-destructive/50' : 'border-border focus-visible:ring-primary focus-visible:ring-primary/50' }`} />
{isSubmitting ? t("processing") : t("exitButton")}
{error && (
{error}
)}
)} {/* Embedded CyberTicketModal */} setIsTicketOpen(false)} email={email} /> ); }