|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { useEffect, useState, useCallback } from "react" |
| 4 | +import Link from "next/link" |
| 5 | +import Cookies from "js-cookie" |
| 6 | +import { Button } from "@/components/ui/button" |
| 7 | + |
| 8 | +const COOKIE_KEY = "dsh_cookie_consent" |
| 9 | +const COOKIE_MAX_DAYS = 365 |
| 10 | + |
| 11 | +export function useCookieConsent() { |
| 12 | + const [consent, setConsentState] = useState<"all" | "none" | null>(() => { |
| 13 | + if (typeof window === "undefined") return null |
| 14 | + return (Cookies.get(COOKIE_KEY) as "all" | "none") ?? null |
| 15 | + }) |
| 16 | + |
| 17 | + useEffect(() => { |
| 18 | + const onStorage = () => { |
| 19 | + setConsentState((Cookies.get(COOKIE_KEY) as "all" | "none") ?? null) |
| 20 | + } |
| 21 | + const onCustom = (e: Event) => { |
| 22 | + const detail = (e as CustomEvent).detail as "all" | "none" |
| 23 | + setConsentState( |
| 24 | + detail ?? (Cookies.get(COOKIE_KEY) as "all" | "none") ?? null |
| 25 | + ) |
| 26 | + } |
| 27 | + |
| 28 | + window.addEventListener("storage", onStorage) |
| 29 | + window.addEventListener("dsh_cookie_consent", onCustom as EventListener) |
| 30 | + return () => { |
| 31 | + window.removeEventListener("storage", onStorage) |
| 32 | + window.removeEventListener( |
| 33 | + "dsh_cookie_consent", |
| 34 | + onCustom as EventListener |
| 35 | + ) |
| 36 | + } |
| 37 | + }, []) |
| 38 | + |
| 39 | + const setConsent = useCallback((value: "all" | "none") => { |
| 40 | + Cookies.set(COOKIE_KEY, value, { |
| 41 | + expires: COOKIE_MAX_DAYS, |
| 42 | + sameSite: "Lax", |
| 43 | + }) |
| 44 | + try { |
| 45 | + // trigger storage event in other tabs |
| 46 | + localStorage.setItem(COOKIE_KEY, value) |
| 47 | + localStorage.removeItem(COOKIE_KEY) |
| 48 | + } catch { |
| 49 | + /* ignore */ |
| 50 | + } |
| 51 | + // notify same-tab listeners |
| 52 | + window.dispatchEvent( |
| 53 | + new CustomEvent("dsh_cookie_consent", { detail: value }) |
| 54 | + ) |
| 55 | + setConsentState(value) |
| 56 | + }, []) |
| 57 | + |
| 58 | + return { consent, setConsent } |
| 59 | +} |
| 60 | + |
| 61 | +export default function CookieBanner() { |
| 62 | + const { consent, setConsent } = useCookieConsent() |
| 63 | + const [isVisible, setIsVisible] = useState(false) |
| 64 | + |
| 65 | + useEffect(() => { |
| 66 | + const timer = setTimeout(() => { |
| 67 | + if (consent === null) setIsVisible(true) |
| 68 | + }, 700) |
| 69 | + return () => clearTimeout(timer) |
| 70 | + }, [consent]) |
| 71 | + |
| 72 | + if (!isVisible) return null |
| 73 | + |
| 74 | + return ( |
| 75 | + <div className="fixed bottom-0 left-0 right-0 z-50 animate-slideUp"> |
| 76 | + <div className="bg-white border-t border-gray-200 shadow-lg"> |
| 77 | + <div className="max-w-7xl mx-auto p-4 md:p-6"> |
| 78 | + <div className="flex flex-col md:flex-row items-start md:items-center gap-4"> |
| 79 | + <div className="flex-1"> |
| 80 | + <p className="text-base text-[#1E1F1E] mb-2"> |
| 81 | + We use cookies to improve your browsing experience and to |
| 82 | + display embedded YouTube videos. Some cookies are set by third |
| 83 | + parties such as Google (Analytics and YouTube). |
| 84 | + </p> |
| 85 | + </div> |
| 86 | + |
| 87 | + <div className="flex flex-col sm:flex-row gap-3 items-center"> |
| 88 | + <Button |
| 89 | + onClick={() => { |
| 90 | + setConsent("all") |
| 91 | + setIsVisible(false) |
| 92 | + }} |
| 93 | + className="bg-[#2D6A4F] text-white hover:bg-[#1D593F] rounded-full w-full sm:w-auto" |
| 94 | + > |
| 95 | + Accept all |
| 96 | + </Button> |
| 97 | + |
| 98 | + <Button |
| 99 | + onClick={() => { |
| 100 | + setConsent("none") |
| 101 | + setIsVisible(false) |
| 102 | + }} |
| 103 | + variant="outline" |
| 104 | + className="border-[#17412C] text-[#0D261A] rounded-full w-full sm:w-auto" |
| 105 | + > |
| 106 | + Reject all |
| 107 | + </Button> |
| 108 | + |
| 109 | + <Link |
| 110 | + href="/cookies" |
| 111 | + className="text-[#0D6E4B] underline text-sm" |
| 112 | + > |
| 113 | + More information |
| 114 | + </Link> |
| 115 | + </div> |
| 116 | + </div> |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + ) |
| 121 | +} |
0 commit comments