Spaces:
Paused
Paused
File size: 10,636 Bytes
587459d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | "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<string | null>(null);
const modalRef = useRef<HTMLDivElement>(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<HTMLFormElement>) => {
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 (
<>
<AnimatePresence>
{isOpen && (
<div className="fixed inset-0 z-[100] flex items-center justify-center p-4">
{/* Dark glass backdrop */}
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
onClick={handleClose}
className="absolute inset-0 bg-black/60 backdrop-blur-md"
/>
{/* Popup Dialog container */}
<motion.div
ref={modalRef}
role="dialog"
aria-modal="true"
aria-labelledby="exit-modal-title"
tabIndex={-1}
initial={{ scale: 0.95, opacity: 0, y: 20 }}
animate={{ scale: 1, opacity: 1, y: 0 }}
exit={{ scale: 0.95, opacity: 0, y: 20 }}
className="relative w-full max-w-lg p-8 sm:p-10 rounded-xl border border-primary/20 bg-secondary/95 dark:bg-background/95 backdrop-blur-lg shadow-2xl overflow-hidden [clip-path:polygon(20px_0,100%_0,100%_calc(100%_-_20px),calc(100%_-_20px)_100%,0_100%,0_20px)] focus:outline-none"
>
{/* Glowing Corner Effects */}
<div className="absolute top-0 end-0 w-24 h-24 bg-primary/5 rounded-full blur-2xl pointer-events-none" />
<div className="absolute bottom-0 start-0 w-24 h-24 bg-primary/5 rounded-full blur-2xl pointer-events-none" />
{/* Close Button */}
<button
onClick={handleClose}
className="absolute top-4 end-4 h-8 w-8 flex items-center justify-center rounded-lg border border-border hover:border-primary/40 text-foreground hover:text-primary transition-all duration-300 focus-visible:ring-1 focus-visible:ring-primary focus-visible:outline-none"
aria-label="Close modal"
>
<X className="h-4 w-4" />
</button>
<div className="flex flex-col gap-6 text-center sm:text-start">
{/* Heading Area */}
<div className="space-y-2">
<div className="inline-flex items-center rounded-full border border-destructive/20 bg-destructive/10 px-3 py-1 text-[10px] font-mono font-bold tracking-widest text-destructive">
<span className="w-1.5 h-1.5 bg-destructive rounded-full animate-ping me-2 shadow-[0_0_8px_rgba(239,68,68,0.5)]" />
EXIT_PREVENTED
</div>
<h3 id="exit-modal-title" className="text-2xl font-extrabold tracking-tight bg-clip-text text-transparent bg-gradient-to-br from-foreground to-muted-foreground mt-2">
{t("exitTitle")}
</h3>
<p className="text-xs text-muted-foreground/80 font-mono leading-relaxed mt-1">
{t("exitSubtitle")}
</p>
</div>
{/* Form Integration */}
<form onSubmit={handleSubmit} className="flex flex-col gap-4">
<div className="relative group">
<div className={`absolute -inset-0.5 rounded-lg blur opacity-0 group-focus-within:opacity-100 transition duration-500 ${error ? 'bg-destructive/45' : 'bg-primary/20'}`}></div>
<input
id="exit-intent-email-input"
name="exit_intent_email"
aria-label="Email address for exit prevention waitlist"
type="email"
placeholder={t("waitlistPlaceholder")}
disabled={isSubmitting}
value={email}
onChange={(e) => {
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'
}`}
/>
</div>
<ShimmerButton
type="submit"
disabled={isSubmitting}
borderRadius="6px"
className="w-full h-12 font-mono text-[10px] font-bold tracking-widest"
>
<span>{isSubmitting ? t("processing") : t("exitButton")}</span>
</ShimmerButton>
<button
type="button"
onClick={handleClose}
className="text-[10px] font-mono text-muted-foreground/50 hover:text-foreground transition-colors self-center mt-1"
>
{t("exitNoThanks")}
</button>
</form>
{error && (
<div className="flex items-center gap-2 font-mono text-[10px] text-destructive animate-fade-in ps-1 self-center">
<AlertTriangle className="h-3.5 w-3.5 shrink-0" />
<span>{error}</span>
</div>
)}
</div>
</motion.div>
</div>
)}
</AnimatePresence>
{/* Embedded CyberTicketModal */}
<CyberTicketModal
isOpen={isTicketOpen}
onClose={() => setIsTicketOpen(false)}
email={email}
/>
</>
);
}
|