'use client' import React, { useState, useEffect, useCallback } from 'react' const COOKIE_NAME = 'fd-cookie-consent' const COOKIE_TIMESTAMP_NAME = 'fd-cookie-consent-ts' // Default expiry durations (in days) // 0 = session only (sessionStorage, not persisted across browser close) const DEFAULT_ACCEPTED_DAYS = 365 const DEFAULT_DECLINED_DAYS = 30 type ConsentStatus = 'accepted' | 'declined' | null function getCookie(name: string): string | null { if (typeof document === 'undefined') return null const match = document.cookie.match(new RegExp(`(?:^|; )${name}=([^;]*)`)) return match ? decodeURIComponent(match[1]) : null } function setCookie(name: string, value: string, days: number) { if (days === 0) { // Session cookie — no expires attribute document.cookie = `${name}=${encodeURIComponent(value)}; path=/; SameSite=Lax` } else { const expires = new Date(Date.now() + days * 864e5).toUTCString() document.cookie = `${name}=${encodeURIComponent(value)}; expires=${expires}; path=/; SameSite=Lax` } } function deleteCookie(name: string) { document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;` } /** * Check whether the stored consent is still valid for the given number of days. * If days = 0, we rely purely on the session cookie existing (no timestamp check). */ function isConsentExpired(days: number): boolean { if (days === 0) return false // Session cookies handle their own expiry const ts = getCookie(COOKIE_TIMESTAMP_NAME) if (!ts) return true const storedAt = parseInt(ts, 10) if (isNaN(storedAt)) return true return Date.now() - storedAt > days * 864e5 } interface CookieConsentProps { privacyPolicyUrl?: string /** Days to remember "Acceptera" — 0 = browser session only */ acceptedDays?: number /** Days to remember "Avvisa" before asking again — 0 = browser session only */ declinedDays?: number onAccept?: () => void onDecline?: () => void } export const CookieConsent: React.FC = ({ privacyPolicyUrl = '/integritetspolicy', acceptedDays = DEFAULT_ACCEPTED_DAYS, declinedDays = DEFAULT_DECLINED_DAYS, onAccept, onDecline, }) => { const [visible, setVisible] = useState(false) useEffect(() => { const consent = getCookie(COOKIE_NAME) if (!consent) { // No consent stored at all — show the banner const timer = setTimeout(() => setVisible(true), 800) return () => clearTimeout(timer) } // Consent exists — check if it has expired for its type const expiryDays = consent === 'accepted' ? acceptedDays : declinedDays if (isConsentExpired(expiryDays)) { // Consent period has lapsed — clear old cookies and ask again deleteCookie(COOKIE_NAME) deleteCookie(COOKIE_TIMESTAMP_NAME) const timer = setTimeout(() => setVisible(true), 800) return () => clearTimeout(timer) } }, [acceptedDays, declinedDays]) const handleAccept = useCallback(() => { setCookie(COOKIE_NAME, 'accepted', acceptedDays) setCookie(COOKIE_TIMESTAMP_NAME, String(Date.now()), acceptedDays) setVisible(false) if (typeof window !== 'undefined' && window._paq) { window._paq.push(['setConsentGiven']) } onAccept?.() }, [acceptedDays, onAccept]) const handleDecline = useCallback(() => { setCookie(COOKIE_NAME, 'declined', declinedDays) setCookie(COOKIE_TIMESTAMP_NAME, String(Date.now()), declinedDays) setVisible(false) if (typeof window !== 'undefined' && window._paq) { window._paq.push(['forgetConsentGiven']) } onDecline?.() }, [declinedDays, onDecline]) if (!visible) return null const policyLink = ( Läs vår integritetspolicy ) return (
{/* Inner layout — full-width, no rounded corners */}
{/* Text block */}

Vi använder cookies

Vi använder cookies för att förbättra din upplevelse på vår webbplats och analysera trafik. Genom att klicka "Acceptera" godkänner du vår användning av cookies.{' '} {policyLink}

{/* Action buttons */}
) } // ─── Utility exports (unchanged) ───────────────────────────────────────────── export function getConsentStatus(): ConsentStatus { const value = getCookie(COOKIE_NAME) if (value === 'accepted' || value === 'declined') return value return null } export function loadScriptIfConsented(src: string, attributes?: Record): boolean { if (getConsentStatus() !== 'accepted') return false if (document.querySelector(`script[src="${src}"]`)) return true const script = document.createElement('script') script.src = src script.async = true if (attributes) { Object.entries(attributes).forEach(([key, val]) => script.setAttribute(key, val)) } document.head.appendChild(script) return true }