"use client"; import { useState, useEffect } from "react"; import Link from "next/link"; type ConsentState = { necessary: boolean; analytics: boolean; preferences: boolean; timestamp: string; }; const CONSENT_COOKIE_NAME = "gdpr_consent"; const CONSENT_VERSION = "1.0"; export function CookieConsent() { const [showBanner, setShowBanner] = useState(false); const [showDetails, setShowDetails] = useState(false); const [consent, setConsent] = useState({ necessary: true, // Always required analytics: false, preferences: false, timestamp: "", }); useEffect(() => { // Check if consent has already been given const savedConsent = localStorage.getItem(CONSENT_COOKIE_NAME); if (savedConsent) { try { const parsed = JSON.parse(savedConsent); setConsent(parsed); // Apply saved consent settings applyConsent(parsed); } catch { // Invalid consent data, show banner setShowBanner(true); } } else { // No consent yet, show banner setShowBanner(true); } }, []); const applyConsent = (consentState: ConsentState) => { // Enable/disable analytics based on consent if (consentState.analytics) { // Enable analytics (e.g., Vercel Analytics, Plausible, etc.) window.localStorage.setItem("va_disabled", "false"); // If using Google Analytics, you would enable it here // window.gtag?.('consent', 'update', { analytics_storage: 'granted' }); } else { // Disable analytics window.localStorage.setItem("va_disabled", "true"); // window.gtag?.('consent', 'update', { analytics_storage: 'denied' }); } }; const saveConsent = (consentState: ConsentState) => { const consentWithTimestamp = { ...consentState, timestamp: new Date().toISOString(), version: CONSENT_VERSION, }; localStorage.setItem(CONSENT_COOKIE_NAME, JSON.stringify(consentWithTimestamp)); setConsent(consentWithTimestamp); applyConsent(consentWithTimestamp); setShowBanner(false); }; const acceptAll = () => { saveConsent({ necessary: true, analytics: true, preferences: true, timestamp: "", }); }; const rejectAll = () => { saveConsent({ necessary: true, analytics: false, preferences: false, timestamp: "", }); }; const saveCustom = () => { saveConsent(consent); }; if (!showBanner) return null; return (
{!showDetails ? ( // Simple banner view

We value your privacy

We use cookies to improve your experience and analyze site usage. You can choose which cookies to accept.{" "} Learn more

) : ( // Detailed settings view

Cookie Settings

{/* Necessary Cookies */}

Strictly Necessary

Always Active

Essential for the website to function properly. These cannot be disabled.

{/* Analytics Cookies */}

Analytics

Help us understand how visitors interact with our website by collecting anonymous usage data.

setConsent({ ...consent, analytics: e.target.checked })} className="mt-1 h-4 w-4 rounded border-border cursor-pointer" aria-label="Enable analytics cookies" />
{/* Preference Cookies */}

Preferences

Remember your settings and preferences like theme choice and language.

setConsent({ ...consent, preferences: e.target.checked })} className="mt-1 h-4 w-4 rounded border-border cursor-pointer" aria-label="Enable preference cookies" />
)}
); } // Hook to check consent status from other components export function useGDPRConsent() { const [consent, setConsent] = useState(null); useEffect(() => { const savedConsent = localStorage.getItem(CONSENT_COOKIE_NAME); if (savedConsent) { try { setConsent(JSON.parse(savedConsent)); } catch { setConsent(null); } } }, []); return { hasConsented: consent !== null, analyticsEnabled: consent?.analytics ?? false, preferencesEnabled: consent?.preferences ?? false, resetConsent: () => { localStorage.removeItem(CONSENT_COOKIE_NAME); window.location.reload(); }, }; } // Component to conditionally render based on consent export function ConsentGate({ children, type, fallback, }: { children: React.ReactNode; type: "analytics" | "preferences"; fallback?: React.ReactNode; }) { const { analyticsEnabled, preferencesEnabled } = useGDPRConsent(); const hasConsent = type === "analytics" ? analyticsEnabled : preferencesEnabled; if (!hasConsent) { return fallback || null; } return <>{children}; }