'use client'; import { useState, useEffect } from 'react'; import { Box, Paper, Typography, Button, IconButton, Collapse, FormControlLabel, Switch, Link, Slide, } from '@mui/material'; import { Close, Settings as SettingsIcon, Cookie } from '@mui/icons-material'; import { useRouter } from 'next/navigation'; interface CookiePreferences { essential: boolean; // Always true, cannot be disabled analytics: boolean; marketing: boolean; } const COOKIE_CONSENT_KEY = 'parentflow_cookie_consent'; const COOKIE_PREFERENCES_KEY = 'parentflow_cookie_preferences'; export function CookieConsent() { const router = useRouter(); const [showBanner, setShowBanner] = useState(false); const [showCustomize, setShowCustomize] = useState(false); const [preferences, setPreferences] = useState({ essential: true, analytics: false, marketing: false, }); useEffect(() => { // Check if user has already given consent const consent = localStorage.getItem(COOKIE_CONSENT_KEY); if (!consent) { // Show banner after a short delay const timer = setTimeout(() => setShowBanner(true), 1000); return () => clearTimeout(timer); } else { // Load saved preferences const savedPrefs = localStorage.getItem(COOKIE_PREFERENCES_KEY); if (savedPrefs) { setPreferences(JSON.parse(savedPrefs)); } } }, []); const savePreferences = (prefs: CookiePreferences) => { localStorage.setItem(COOKIE_CONSENT_KEY, 'true'); localStorage.setItem(COOKIE_PREFERENCES_KEY, JSON.stringify(prefs)); // Apply analytics based on user choice if (prefs.analytics) { console.log('📊 Analytics enabled'); // TODO: Initialize analytics (Google Analytics, etc.) } else { console.log('📊 Analytics disabled'); // TODO: Disable analytics } setShowBanner(false); }; const handleAcceptAll = () => { const allPrefs: CookiePreferences = { essential: true, analytics: true, marketing: true, }; setPreferences(allPrefs); savePreferences(allPrefs); }; const handleRejectAll = () => { const essentialOnly: CookiePreferences = { essential: true, analytics: false, marketing: false, }; setPreferences(essentialOnly); savePreferences(essentialOnly); }; const handleSaveCustom = () => { savePreferences(preferences); }; const handleToggleCustomize = () => { setShowCustomize(!showCustomize); }; if (!showBanner) { return null; } return ( theme.zIndex.snackbar, borderRadius: '16px 16px 0 0', maxWidth: { xs: '100%', md: 600 }, mx: 'auto', mb: 0, }} > {/* Close button */} {/* Cookie icon and title */} Cookie Preferences {/* Description */} We use cookies to enhance your experience, analyze site usage, and assist in our marketing efforts. Essential cookies are required for the app to function.{' '} { e.preventDefault(); router.push('/legal/cookies'); }} sx={{ fontWeight: 'bold', textDecoration: 'underline', cursor: 'pointer' }} > Learn more {/* Customize section */} } label={ Essential Cookies Required for the app to function. Cannot be disabled. } sx={{ mb: 1, alignItems: 'flex-start' }} /> setPreferences({ ...preferences, analytics: e.target.checked })} color="primary" /> } label={ Analytics Cookies Help us understand how you use the app (anonymized data). } sx={{ mb: 1, alignItems: 'flex-start' }} /> setPreferences({ ...preferences, marketing: e.target.checked })} color="primary" /> } label={ Marketing Cookies Used to deliver relevant content and track campaign performance. } sx={{ alignItems: 'flex-start' }} /> {/* Action buttons */} {showCustomize ? ( <> ) : ( <> )} ); }