|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useEffect, useState } from 'react'; |
| 4 | +import Link from 'next/link'; |
| 5 | +import { Button } from '@/components/ui/button'; |
| 6 | +import { firebaseConfig } from '@/firebase/config'; |
| 7 | +import { |
| 8 | + OPEN_SETTINGS_EVENT, acceptTorNotice, isOnionHost, onConsentChange, savedAnalyticsChoice, |
| 9 | + sendsGlobalPrivacyControl, setAnalyticsChoice, shouldAskAboutAnalytics, torNoticeAccepted, |
| 10 | +} from '@/lib/consent'; |
| 11 | + |
| 12 | +type Showing = 'tor' | 'analytics' | 'settings' | null; |
| 13 | + |
| 14 | +/** Which notice to show: the Tor notice first (on the onion address), then the analytics question */ |
| 15 | +function pick(): Showing { |
| 16 | + if (isOnionHost() && !torNoticeAccepted()) return 'tor'; |
| 17 | + if (shouldAskAboutAnalytics()) return 'analytics'; |
| 18 | + return null; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * The privacy notices at the bottom of the page. On the Tor onion address, |
| 23 | + * visitors first accept a notice about the Google services the site uses. |
| 24 | + * Everyone is asked once whether Google Analytics may count their visits |
| 25 | + * (it stays off until they accept); the footer's Cookie settings link opens |
| 26 | + * it again. Renders nothing on the server, so pages hydrate cleanly. |
| 27 | + */ |
| 28 | +export default function ConsentBanner() { |
| 29 | + const [showing, setShowing] = useState<Showing>(null); |
| 30 | + |
| 31 | + useEffect(() => { |
| 32 | + // Read in the browser after hydration: the choices live in localStorage |
| 33 | + const update = () => setShowing(current => current === 'settings' ? current : pick()); |
| 34 | + const timer = window.setTimeout(update, 0); |
| 35 | + const stop = onConsentChange(update); |
| 36 | + const open = () => setShowing('settings'); |
| 37 | + window.addEventListener(OPEN_SETTINGS_EVENT, open); |
| 38 | + return () => { |
| 39 | + window.clearTimeout(timer); |
| 40 | + stop(); |
| 41 | + window.removeEventListener(OPEN_SETTINGS_EVENT, open); |
| 42 | + }; |
| 43 | + }, []); |
| 44 | + |
| 45 | + if (!showing) return null; |
| 46 | + |
| 47 | + const choose = (choice: 'granted' | 'denied') => { |
| 48 | + setAnalyticsChoice(choice, firebaseConfig.measurementId); |
| 49 | + setShowing(pick()); |
| 50 | + }; |
| 51 | + |
| 52 | + if (showing === 'tor') { |
| 53 | + return ( |
| 54 | + <div role="dialog" aria-modal="true" aria-labelledby="tor-notice-title" className="fixed inset-0 z-[100] flex items-end sm:items-center justify-center bg-black/60 p-4"> |
| 55 | + <div className="w-full max-w-lg rounded-lg border bg-background p-5 shadow-lg space-y-3"> |
| 56 | + <h2 id="tor-notice-title" className="text-lg font-semibold">You're visiting w3Develops over Tor</h2> |
| 57 | + <p className="text-sm text-muted-foreground"> |
| 58 | + Tor hides your IP address and location from us and from Google. But this site still loads Google services: |
| 59 | + Firebase (sign-in and our database) and reCAPTCHA, which checks you're not a bot. Google can |
| 60 | + see that someone is using w3Develops, and if you sign in, what you do is linked to your account. |
| 61 | + </p> |
| 62 | + <p className="text-sm text-muted-foreground"> |
| 63 | + You can browse without an account. <Link href="/tor" className="underline">Read more about using w3Develops over Tor</Link>. |
| 64 | + </p> |
| 65 | + <div className="flex justify-end"> |
| 66 | + <Button onClick={acceptTorNotice}>I understand, continue</Button> |
| 67 | + </div> |
| 68 | + </div> |
| 69 | + </div> |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + const current = savedAnalyticsChoice(); |
| 74 | + return ( |
| 75 | + <div role="region" aria-label="Cookie choices" className="fixed bottom-0 inset-x-0 z-[90] p-3 sm:p-4"> |
| 76 | + <div className="mx-auto max-w-3xl rounded-lg border bg-background p-4 shadow-lg sm:flex sm:items-center sm:gap-4 space-y-3 sm:space-y-0"> |
| 77 | + <p className="text-sm text-muted-foreground flex-1"> |
| 78 | + We use Google Analytics to count visits, which sets cookies. It stays off unless you allow it. Sign-in and our |
| 79 | + bot check are needed for the site to work. <Link href="/cookies" className="underline">Cookies</Link> |
| 80 | + {' · '}<Link href="/privacy" className="underline">Privacy</Link> |
| 81 | + {showing === 'settings' && ( |
| 82 | + <span className="block mt-1 text-xs"> |
| 83 | + Now: {current === 'granted' ? 'allowed' : current === 'denied' ? 'off' : sendsGlobalPrivacyControl() ? 'off (your browser sends Global Privacy Control)' : 'off'}. |
| 84 | + </span> |
| 85 | + )} |
| 86 | + </p> |
| 87 | + <div className="flex gap-2 shrink-0"> |
| 88 | + <Button variant="outline" size="sm" onClick={() => choose('denied')}>Reject</Button> |
| 89 | + <Button size="sm" onClick={() => choose('granted')}>Allow analytics</Button> |
| 90 | + </div> |
| 91 | + </div> |
| 92 | + </div> |
| 93 | + ); |
| 94 | +} |
| 95 | + |
| 96 | +/** The footer's Cookie settings link, which reopens the banner */ |
| 97 | +export function CookieSettingsButton({ className }: { className?: string }) { |
| 98 | + return ( |
| 99 | + <button type="button" className={className} onClick={() => window.dispatchEvent(new Event(OPEN_SETTINGS_EVENT))}> |
| 100 | + Cookie settings |
| 101 | + </button> |
| 102 | + ); |
| 103 | +} |
0 commit comments