'use client'; import { useState, useEffect, useCallback } from 'react'; import { updateConsentDetailed, trackButtonClick, CookiePreferences, getStoredPreferences, storePreferences, getDefaultPreferences, } from '@/lib/analytics'; import { motion, AnimatePresence } from 'framer-motion'; const LEGACY_CONSENT_KEY = 'ga_consent'; export function CookieConsent() { const [showConsent, setShowConsent] = useState(false); const [showSettings, setShowSettings] = useState(false); const [isAnimating, setIsAnimating] = useState(false); const [preferences, setPreferences] = useState(getDefaultPreferences()); useEffect(() => { const stored = getStoredPreferences(); if (stored) { updateConsentDetailed(stored); } else { const legacyConsent = localStorage.getItem(LEGACY_CONSENT_KEY); if (legacyConsent) { const migratedPrefs: CookiePreferences = { necessary: true, analytics: legacyConsent === 'granted', marketing: false, timestamp: Date.now(), }; storePreferences(migratedPrefs); updateConsentDetailed(migratedPrefs); localStorage.removeItem(LEGACY_CONSENT_KEY); } else { const timer = setTimeout(() => { setShowConsent(true); }, 2000); return () => clearTimeout(timer); } } return undefined; }, []); const handleSavePreferences = useCallback((prefs: CookiePreferences) => { setIsAnimating(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, timestamp: Date.now(), }; handleSavePreferences(allAccepted); trackButtonClick('accept_all_cookies', 'consent_banner'); }; const handleRejectAll = () => { const allRejected: CookiePreferences = { necessary: true, analytics: false, marketing: false, 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-gray-300 text-[#C41E3A] focus:ring-[#C41E3A] cursor-pointer" aria-label="分析 Cookie" />
分析 Cookie

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

handleTogglePreference('marketing')} className="mt-1 h-4 w-4 rounded border-gray-300 text-[#C41E3A] focus:ring-[#C41E3A] 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 ( ); }