"use client"; import { useEffect, useRef, useState } from "react"; import { getCookie, setCookie } from "../../../utils/cookie"; import { TypeComponentRichTextFields } from "./type"; import { MaterialIcon } from "@shared/components/material-icon"; import { Button } from "@shared/contentful/blocks/button"; export default function CookieBanner({ content, }: { content: TypeComponentRichTextFields; }) { let marginBottom = 3; const [isBannerVisible, setIsBannerVisible] = useState(false); const [isStickyFooterPresent, setStickyFooterPresent] = useState(false); const bannerRef = useRef(null); useEffect(() => { const cookieBannerClosed = getCookie("cookieBannerClosed"); if (cookieBannerClosed === "true") { setIsBannerVisible(false); } else { setIsBannerVisible(true); } }, []); useEffect(() => { const checkElementPresence = () => { const element = document.getElementById("anchored-banner"); setStickyFooterPresent(!!element); }; checkElementPresence(); }, []); // Add body class and calculate height for chat positioning useEffect(() => { if (isBannerVisible) { window?.document?.body?.classList.add("cookie-banner-visible"); } else { window?.document?.body?.classList.remove("cookie-banner-visible"); } return () => { window?.document?.body?.classList.remove("cookie-banner-visible"); }; }, [isBannerVisible]); // Calculate cookie banner height and set CSS custom property useEffect(() => { if (!isBannerVisible) return; const calculateCookieBannerHeight = () => { if (bannerRef.current) { const rect = bannerRef.current.getBoundingClientRect(); const viewportHeight = window.innerHeight; // Calculate how much space the banner occupies from the bottom of the viewport // This is: viewport height - distance from top const spaceFromBottom = viewportHeight - rect.top; document.documentElement.style.setProperty( "--cookie-banner-height", `${spaceFromBottom}px` ); } }; // Calculate initially after render and after a short delay to ensure layout is complete const timer1 = setTimeout(calculateCookieBannerHeight, 100); const timer2 = setTimeout(calculateCookieBannerHeight, 300); // Recalculate on window resize with debounce let resizeTimeout: ReturnType; const handleResize = () => { clearTimeout(resizeTimeout); resizeTimeout = setTimeout(calculateCookieBannerHeight, 150); }; window.addEventListener("resize", handleResize); return () => { clearTimeout(timer1); clearTimeout(timer2); clearTimeout(resizeTimeout); window.removeEventListener("resize", handleResize); document.documentElement.style.removeProperty("--cookie-banner-height"); }; }, [isBannerVisible, isStickyFooterPresent]); const handleClose = () => { const expirationDate = new Date(Date.now() + 43200 * 60 * 1000); setIsBannerVisible(false); setCookie("cookieBannerClosed", true, { expires: expirationDate, }); }; if (isStickyFooterPresent) marginBottom = 14; // Calculate margin values in pixels for inline styles const marginBottomPx = marginBottom * 4; // Tailwind: 3 = 12px, 14 = 56px (each unit is 4px) const marginLeftPx = 12; // mx-3 = 12px if (!isBannerVisible) return null; return ( ); }