import React, { forwardRef, useState, useEffect } from "react"; import { BaseComponentProps } from "../../types"; import { Button } from "../ui/Button"; import { Card, CardContent, CardHeader, CardTitle } from "../ui/Card"; export interface ConsentCategory { id: string; name: string; description: string; required: boolean; enabled: boolean; } export interface ConsentManagerProps extends BaseComponentProps { categories?: ConsentCategory[]; onConsentUpdate?: (consents: Record) => void; onAcceptAll?: () => void; onRejectAll?: () => void; showBanner?: boolean; showSettings?: boolean; onSettingsToggle?: () => void; privacyPolicyUrl?: string; termsUrl?: string; companyName?: string; bannerText?: string; settingsText?: string; } const defaultCategories: ConsentCategory[] = [ { id: "necessary", name: "Necessary Cookies", description: "These cookies are essential for the website to function properly. They cannot be disabled.", required: true, enabled: true, }, { id: "analytics", name: "Analytics Cookies", description: "These cookies help us understand how visitors interact with our website by collecting and reporting information anonymously.", required: false, enabled: false, }, { id: "marketing", name: "Marketing Cookies", description: "These cookies are used to track visitors across websites to display relevant and engaging advertisements.", required: false, enabled: false, }, { id: "preferences", name: "Preference Cookies", description: "These cookies allow the website to remember choices you make to provide enhanced, more personal features.", required: false, enabled: false, }, ]; export const ConsentManager = forwardRef( ( { className = "", categories = defaultCategories, onConsentUpdate, onAcceptAll, onRejectAll, showBanner = true, showSettings = false, onSettingsToggle, privacyPolicyUrl, termsUrl, bannerText = "We use cookies and similar technologies to enhance your browsing experience, analyze site traffic, and personalize content.", settingsText = "Manage your cookie preferences below. You can change these settings at any time.", ...props }, ref ) => { const [consentData, setConsentData] = useState(categories); const [hasInteracted, setHasInteracted] = useState(false); useEffect(() => { // Load saved consent preferences from localStorage const savedConsent = localStorage.getItem("consent-preferences"); if (savedConsent) { try { const parsed = JSON.parse(savedConsent); setConsentData((prev) => prev.map((category) => ({ ...category, enabled: category.required || parsed[category.id] || false, })) ); setHasInteracted(true); } catch (error) { console.error("Failed to parse saved consent preferences:", error); } } }, []); const saveConsent = (newConsentData: ConsentCategory[]) => { const consentObject = newConsentData.reduce((acc, category) => { acc[category.id] = category.enabled; return acc; }, {} as Record); localStorage.setItem( "consent-preferences", JSON.stringify(consentObject) ); onConsentUpdate?.(consentObject); setHasInteracted(true); }; const handleAcceptAll = () => { const updatedData = consentData.map((category) => ({ ...category, enabled: true, })); setConsentData(updatedData); saveConsent(updatedData); onAcceptAll?.(); }; const handleRejectAll = () => { const updatedData = consentData.map((category) => ({ ...category, enabled: category.required, })); setConsentData(updatedData); saveConsent(updatedData); onRejectAll?.(); }; const handleCategoryToggle = (categoryId: string) => { const updatedData = consentData.map((category) => category.id === categoryId && !category.required ? { ...category, enabled: !category.enabled } : category ); setConsentData(updatedData); }; const handleSaveSettings = () => { saveConsent(consentData); onSettingsToggle?.(); }; const renderToggleSwitch = ( enabled: boolean, onChange: () => void, disabled = false ) => ( ); const renderBanner = () => { if (!showBanner || hasInteracted) return null; return (

{bannerText} {(privacyPolicyUrl || termsUrl) && ( Learn more in our{" "} {privacyPolicyUrl && ( Privacy Policy )} {privacyPolicyUrl && termsUrl && " and "} {termsUrl && ( Terms of Service )} . )}

); }; const renderSettings = () => { if (!showSettings) return null; return (
Cookie Preferences

{settingsText}

{consentData.map((category) => (

{category.name} {category.required && ( Required )}

{category.description}

{renderToggleSwitch( category.enabled, () => handleCategoryToggle(category.id), category.required )}
))}
); }; return (
{renderBanner()} {renderSettings()}
); } ); ConsentManager.displayName = "ConsentManager";