import React, { useState, useEffect } from 'react'; import { X, Check, Cookie } from 'lucide-react'; import { Link } from 'react-router-dom'; import { useAuth } from '../context/AuthContext'; import { supabase, isSupabaseConfigured } from '../lib/supabaseClient'; export const CookieBanner: React.FC = () => { const { user } = useAuth(); const [isVisible, setIsVisible] = useState(false); const [hasChecked, setHasChecked] = useState(false); useEffect(() => { const checkAndSyncConsent = async () => { // 1. Check Local Storage choice const localConsent = localStorage.getItem('cookie_consent'); // If a choice was already made locally, we hide the banner immediately if (localConsent !== null) { setIsVisible(false); setHasChecked(true); // AUTO-SYNC: If user is logged in, ensure the local choice is synced to the database if (user && isSupabaseConfigured) { try { const consentValue = localConsent === 'true'; await supabase .from('profiles') .update({ cookie_consent: consentValue }) .eq('id', user.id); } catch (e) { console.error("Error auto-syncing cookie consent to DB:", e); } } return; } // 2. If NO local choice but user IS logged in, try to fetch from DB if (user && isSupabaseConfigured) { try { const { data, error } = await supabase .from('profiles') .select('cookie_consent') .eq('id', user.id) .maybeSingle(); if (!error && data && data.cookie_consent !== null) { // Save DB choice to local storage and hide localStorage.setItem('cookie_consent', data.cookie_consent.toString()); setIsVisible(false); setHasChecked(true); return; } } catch (e) { console.error("Error checking cookie consent in DB:", e); } } // 3. If NO choice found anywhere, show the banner setIsVisible(true); setHasChecked(true); }; checkAndSyncConsent(); }, [user]); const handleConsent = async (allowed: boolean) => { // Save to local storage immediately to hide banner localStorage.setItem('cookie_consent', allowed.toString()); setIsVisible(false); // If logged in, also update the database if (user && isSupabaseConfigured) { try { await supabase .from('profiles') .update({ cookie_consent: allowed }) .eq('id', user.id); } catch (e) { console.error("Error saving cookie consent to DB manually:", e); } } // Trigger storage event for AnalyticsTracker and other listeners window.dispatchEvent(new Event('storage')); }; if (!isVisible || !hasChecked) return null; return (

Sütik és Adatvédelem

Az élmény fokozása érdekében sütiket használunk. Az elfogadással hozzájárul az anonim látogatottsági adatok gyűjtéséhez.

További információkért olvassa el az Adatkezelési tájékoztatónkat.
); };