Initial commit
This commit is contained in:
Vendored
-51
@@ -1,51 +0,0 @@
|
||||
'use client';
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { ACCEPT_ALL_CONSENT, DEFAULT_CONSENT, REJECT_ALL_CONSENT } from './categories.js';
|
||||
import { updateConsent } from './googleConsent.js';
|
||||
import { CONSENT_COOKIE, CONSENT_MAX_AGE, parseConsent, serializeConsent } from './storage.js';
|
||||
export function useConsent() {
|
||||
const [state, setState] = useState(DEFAULT_CONSENT);
|
||||
const [decided, setDecided] = useState(false);
|
||||
useEffect(()=>{
|
||||
const raw = document.cookie.split('; ').find((c)=>c.startsWith(`${CONSENT_COOKIE}=`))?.split('=')[1];
|
||||
const parsed = parseConsent(raw);
|
||||
if (parsed) {
|
||||
setState(parsed);
|
||||
setDecided(true);
|
||||
}
|
||||
}, []);
|
||||
const persist = useCallback((next)=>{
|
||||
document.cookie = `${CONSENT_COOKIE}=${serializeConsent(next)};path=/;max-age=${CONSENT_MAX_AGE};samesite=lax`;
|
||||
setState(next);
|
||||
setDecided(true);
|
||||
// Tell Google right away. Tags are already on the page holding whatever
|
||||
// defaults Analytics set at load; without this update they keep them for
|
||||
// the rest of the session and never write their cookies — the banner would
|
||||
// look like it worked while nothing changed.
|
||||
updateConsent(next);
|
||||
}, []);
|
||||
const acceptAll = useCallback(()=>persist(ACCEPT_ALL_CONSENT), [
|
||||
persist
|
||||
]);
|
||||
const rejectAll = useCallback(()=>persist(REJECT_ALL_CONSENT), [
|
||||
persist
|
||||
]);
|
||||
const savePreferences = useCallback((choices)=>persist({
|
||||
...DEFAULT_CONSENT,
|
||||
...choices,
|
||||
necessary: true
|
||||
}), [
|
||||
persist
|
||||
]);
|
||||
const reopen = useCallback(()=>setDecided(false), []);
|
||||
return {
|
||||
acceptAll,
|
||||
decided,
|
||||
rejectAll,
|
||||
reopen,
|
||||
savePreferences,
|
||||
state
|
||||
};
|
||||
}
|
||||
|
||||
//# sourceMappingURL=useConsent.js.map
|
||||
Reference in New Issue
Block a user