"use client"; import Image from "next/image"; import { useCallback, useEffect, useRef, useState } from "react"; import { createPortal } from "react-dom"; import { Ic } from "@/icons"; import { useHardwareBackHandler } from "@/hooks/use-hardware-back-handler"; import { useI18n } from "@/translations/provider"; const EXIT_ANIMATION_MS = 200; export type SupportSheetProps = { isOpen: boolean; onClose: () => void; }; export function SupportSheet({ isOpen, onClose }: SupportSheetProps) { const { dictionary: t } = useI18n(); const [mounted, setMounted] = useState(false); const [isRendered, setIsRendered] = useState(isOpen); const [isClosing, setIsClosing] = useState(false); const isClosingRef = useRef(false); const isMountedRef = useRef(true); const prevIsOpenRef = useRef(isOpen); const timerRef = useRef(null); useEffect(() => { isMountedRef.current = true; setMounted(true); return () => { isMountedRef.current = false; if (timerRef.current) clearTimeout(timerRef.current); }; }, []); const closeSheet = useCallback(() => { if (isClosingRef.current) return; isClosingRef.current = true; setIsClosing(true); if (timerRef.current) clearTimeout(timerRef.current); timerRef.current = window.setTimeout(() => { if (isMountedRef.current) { setIsRendered(false); setIsClosing(false); isClosingRef.current = false; } onClose(); }, EXIT_ANIMATION_MS); }, [onClose]); // When isOpen changes from outside useEffect(() => { if (prevIsOpenRef.current !== isOpen) { prevIsOpenRef.current = isOpen; if (isOpen) { if (timerRef.current) clearTimeout(timerRef.current); isClosingRef.current = false; setIsClosing(false); setIsRendered(true); } else if (!isClosingRef.current && isRendered) { closeSheet(); } } }, [isOpen, isRendered, closeSheet]); useHardwareBackHandler(closeSheet, isRendered && !isClosing); // Lock body scroll useEffect(() => { if (!isRendered || !mounted) return; const previousBodyOverflow = document.body.style.overflow; const previousHtmlOverflow = document.documentElement.style.overflow; document.body.style.overflow = "hidden"; document.documentElement.style.overflow = "hidden"; return () => { document.body.style.overflow = previousBodyOverflow; document.documentElement.style.overflow = previousHtmlOverflow; }; }, [isRendered, mounted]); const handleSupportContact = () => { if (typeof window !== "undefined" && "HabibApp" in window) { try { const app = ( window as Window & { HabibApp?: { postMessage: (msg: string) => void }; } ).HabibApp; app?.postMessage( JSON.stringify({ action: "open_consultant_page", data: { consultant: "marriage_specialist@habibapp.com" }, }), ); } catch (err) { console.error("Error calling HabibApp bridge", err); } } else { window.open("mailto:marriage_specialist@habibapp.com", "_blank"); } closeSheet(); }; if (!isRendered || !mounted) return null; return createPortal(
{ if (event.target === event.currentTarget) { closeSheet(); } }} >
{/* Support Icon */}
{/* Text Content */}

{t["Contact Advisors"] || t["Contact Support"]}

{ t[ "If you encounter any issues, please feel free to contact our advisors in WhatsApp" ] || t[ "If you encounter any issues, please feel free to contact our support specialists in WhatsApp" ] }

{/* Buttons Row */}
{/* Cancel Button */} {/* Contact WhatsApp Button */}
, document.body, ); } export default SupportSheet;