|
| 1 | +import { useState, useEffect } from 'react' |
| 2 | + |
| 3 | +const EU_COUNTRIES = [ |
| 4 | + 'AT', |
| 5 | + 'BE', |
| 6 | + 'BG', |
| 7 | + 'HR', |
| 8 | + 'CY', |
| 9 | + 'CZ', |
| 10 | + 'DK', |
| 11 | + 'EE', |
| 12 | + 'FI', |
| 13 | + 'FR', |
| 14 | + 'DE', |
| 15 | + 'GR', |
| 16 | + 'HU', |
| 17 | + 'IE', |
| 18 | + 'IT', |
| 19 | + 'LV', |
| 20 | + 'LT', |
| 21 | + 'LU', |
| 22 | + 'MT', |
| 23 | + 'NL', |
| 24 | + 'PL', |
| 25 | + 'PT', |
| 26 | + 'RO', |
| 27 | + 'SK', |
| 28 | + 'SI', |
| 29 | + 'ES', |
| 30 | + 'SE', |
| 31 | +] |
| 32 | + |
| 33 | +interface LocationResponse { |
| 34 | + country: string |
| 35 | +} |
| 36 | + |
| 37 | +// Cookie helper functions |
| 38 | +function setCookie(name: string, value: string, days: number) { |
| 39 | + const date = new Date() |
| 40 | + date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000) |
| 41 | + const expires = `expires=${date.toUTCString()}` |
| 42 | + document.cookie = `${name}=${value};${expires};path=/;SameSite=Lax` |
| 43 | +} |
| 44 | + |
| 45 | +function getCookie(name: string): string | null { |
| 46 | + const nameEQ = `${name}=` |
| 47 | + const ca = document.cookie.split(';') |
| 48 | + for (let i = 0; i < ca.length; i++) { |
| 49 | + let c = ca[i] |
| 50 | + while (c.charAt(0) === ' ') c = c.substring(1, c.length) |
| 51 | + if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length) |
| 52 | + } |
| 53 | + return null |
| 54 | +} |
| 55 | + |
| 56 | +export function ConsentPanel() { |
| 57 | + const [showConsent, setShowConsent] = useState(false) |
| 58 | + |
| 59 | + useEffect(() => { |
| 60 | + // Check if user is from EU |
| 61 | + const checkLocation = async () => { |
| 62 | + try { |
| 63 | + // In development, always show the consent panel |
| 64 | + if (import.meta.env.DEV) { |
| 65 | + const consent = getCookie('tracking-consent') |
| 66 | + if (!consent) { |
| 67 | + setShowConsent(true) |
| 68 | + } |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + const response = await fetch('/api/location') |
| 73 | + const data = (await response.json()) as LocationResponse |
| 74 | + const isEU = EU_COUNTRIES.includes(data.country) |
| 75 | + const consent = getCookie('tracking-consent') |
| 76 | + |
| 77 | + if (isEU && !consent) { |
| 78 | + setShowConsent(true) |
| 79 | + } |
| 80 | + } catch (error) { |
| 81 | + console.error('Error checking location:', error) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + checkLocation() |
| 86 | + }, []) |
| 87 | + |
| 88 | + const handleAccept = () => { |
| 89 | + // Store consent for 1 year |
| 90 | + setCookie('tracking-consent', 'accepted', 365) |
| 91 | + setShowConsent(false) |
| 92 | + } |
| 93 | + |
| 94 | + const handleDecline = () => { |
| 95 | + // Store consent for 1 year |
| 96 | + setCookie('tracking-consent', 'declined', 365) |
| 97 | + setShowConsent(false) |
| 98 | + } |
| 99 | + |
| 100 | + if (!showConsent) return null |
| 101 | + |
| 102 | + return ( |
| 103 | + <div className="fixed bottom-0 left-0 right-0 bg-white dark:bg-gray-800 p-4 shadow-lg z-50"> |
| 104 | + <div className="container mx-auto max-w-4xl"> |
| 105 | + <p className="text-sm mb-4"> |
| 106 | + We use cookies and similar technologies to help personalize content and analyze traffic. |
| 107 | + By clicking "Accept", you consent to our use of cookies and tracking technologies. |
| 108 | + </p> |
| 109 | + <div className="flex gap-4"> |
| 110 | + <button |
| 111 | + onClick={handleAccept} |
| 112 | + className="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700" |
| 113 | + > |
| 114 | + Accept |
| 115 | + </button> |
| 116 | + <button |
| 117 | + onClick={handleDecline} |
| 118 | + className="px-4 py-2 bg-gray-200 dark:bg-gray-700 rounded hover:bg-gray-300 dark:hover:bg-gray-600" |
| 119 | + > |
| 120 | + Decline |
| 121 | + </button> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + ) |
| 126 | +} |
0 commit comments