SPB Git

spb/phi-conseil

Public
1.3 KB · 45 lines typescript
1"use client";23import {4  CONSENT_COOKIE,5  CONSENT_EVENT,6  CONSENT_MAX_AGE_S,7  CONSENT_OPEN_EVENT,8  readConsentCookie,9  serializeConsent,10  type Consent,11} from "@/lib/consent";1213/** Magasin minimal basé sur document.cookie + événements, pour useSyncExternalStore. */14export function subscribe(cb: () => void) {15  window.addEventListener(CONSENT_EVENT, cb);16  return () => window.removeEventListener(CONSENT_EVENT, cb);17}1819let lastRaw: string | null = null;20let lastParsed: Consent | null | undefined;2122/** Retourne un snapshot stable : `undefined` = pas de choix enregistré. */23export function getSnapshot(): Consent | undefined {24  const raw = document.cookie;25  if (raw !== lastRaw || lastParsed === undefined) {26    lastRaw = raw;27    lastParsed = readConsentCookie(raw);28  }29  return lastParsed ?? undefined;30}3132export function getServerSnapshot(): Consent | undefined | null {33  return null; // null = inconnu côté serveur (rien n'est rendu)34}3536export function writeConsent(c: Consent) {37  const secure = window.location.protocol === "https:" ? "; Secure" : "";38  document.cookie = `${CONSENT_COOKIE}=${serializeConsent(c)}; Max-Age=${CONSENT_MAX_AGE_S}; Path=/; SameSite=Lax${secure}`;39  window.dispatchEvent(new Event(CONSENT_EVENT));40}4142export function openPreferences() {43  window.dispatchEvent(new Event(CONSENT_OPEN_EVENT));44}45
Files · main
Menu

Theme