'use client'; import { useState, useEffect, useCallback, useRef } from 'react'; import { updateConsentDetailed, trackButtonClick, CookiePreferences, getStoredPreferences, storePreferences, getDefaultPreferences, } from '@/lib/analytics'; import { motion, AnimatePresence } from 'framer-motion'; const LEGACY_CONSENT_KEY = 'ga_consent'; let hasConsentBeenHandled = false; function getInitialShowConsent(): boolean { if (typeof window === 'undefined') {return false;} if (hasConsentBeenHandled) {return false;} const stored = getStoredPreferences(); if (stored) {return false;} const legacyConsent = localStorage.getItem(LEGACY_CONSENT_KEY); if (legacyConsent) {return false;} return false; } export function CookieConsent() { const [showConsent, setShowConsent] = useState(getInitialShowConsent); const [showSettings, setShowSettings] = useState(false); const [isAnimating, setIsAnimating] = useState(false); const [preferences, setPreferences] = useState(getDefaultPreferences()); const consentCheckedRef = useRef(false); useEffect(() => { if (consentCheckedRef.current) {return;} consentCheckedRef.current = true; const stored = getStoredPreferences(); if (stored) { hasConsentBeenHandled = true; updateConsentDetailed(stored); } else { const legacyConsent = localStorage.getItem(LEGACY_CONSENT_KEY); if (legacyConsent) { hasConsentBeenHandled = true; const migratedPrefs: CookiePreferences = { necessary: true, analytics: legacyConsent === 'granted', marketing: false, functionality: true, timestamp: Date.now(), }; storePreferences(migratedPrefs); updateConsentDetailed(migratedPrefs); localStorage.removeItem(LEGACY_CONSENT_KEY); } else { const timer = setTimeout(() => { setShowConsent(true); }, 2000); return () => clearTimeout(timer); } } return undefined; }, []); useEffect(() => { const handleOpenSettings = () => { setShowSettings(true); setShowConsent(true); }; window.addEventListener('open-cookie-settings', handleOpenSettings); return () => window.removeEventListener('open-cookie-settings', handleOpenSettings); }, []); const handleSavePreferences = useCallback((prefs: CookiePreferences) => { setIsAnimating(true); hasConsentBeenHandled = true; const finalPrefs = { ...prefs, timestamp: Date.now() }; storePreferences(finalPrefs); updateConsentDetailed(finalPrefs); trackButtonClick('save_cookie_preferences', 'consent_banner'); setTimeout(() => { setShowConsent(false); setShowSettings(false); setIsAnimating(false); }, 300); }, []); const handleAcceptAll = () => { const allAccepted: CookiePreferences = { necessary: true, analytics: true, marketing: false, functionality: true, timestamp: Date.now(), }; handleSavePreferences(allAccepted); trackButtonClick('accept_all_cookies', 'consent_banner'); }; const handleRejectAll = () => { const allRejected: CookiePreferences = { necessary: true, analytics: false, marketing: false, functionality: true, timestamp: Date.now(), }; handleSavePreferences(allRejected); trackButtonClick('reject_all_cookies', 'consent_banner'); }; const handleTogglePreference = (key: 'analytics' | 'marketing') => { setPreferences((prev) => ({ ...prev, [key]: !prev[key] })); }; const handleSaveCustom = () => { handleSavePreferences(preferences); }; return ( {showConsent && (
{!showSettings ? (

我们使用 Cookie 和类似技术来改善您的体验、分析网站流量。 继续使用即表示您同意我们的{' '} 隐私政策

) : (

Cookie 偏好设置

必要 Cookie 始终启用

网站正常运行所必需,无法禁用

handleTogglePreference('analytics')} className="mt-1 h-4 w-4 rounded border-[var(--color-border-secondary)] text-[var(--color-brand-primary)] focus:ring-[var(--color-brand-primary)] cursor-pointer" aria-label="分析 Cookie" />
分析 Cookie

帮助我们了解访客如何使用网站,改进用户体验

handleTogglePreference('marketing')} className="mt-1 h-4 w-4 rounded border-[var(--color-border-secondary)] text-[var(--color-brand-primary)] focus:ring-[var(--color-brand-primary)] cursor-pointer" aria-label="营销 Cookie" />
营销 Cookie

用于个性化广告(当前未使用)

)}
)}
); } export function CookieSettingsButton() { const [isVisible] = useState(() => { if (typeof window === 'undefined') {return false;} return !!getStoredPreferences(); }); if (!isVisible) {return null;} return ( ); }