Files
Momento/memento-note/hooks/use-cookie-consent.ts
Antigravity a623454347
Some checks failed
CI / Lint, Unit Tests & Build (push) Failing after 1m32s
CI / Deploy production (on server) (push) Has been skipped
perf: memo GridCard, fuse save fns, fix slash tab active color
2026-06-14 14:06:05 +00:00

44 lines
1.1 KiB
TypeScript

'use client'
import { useCallback, useEffect, useState } from 'react'
import {
CONSENT_CHANGE_EVENT,
getConsent,
hasConsentChoice,
loadConsentWithDBSync,
type ConsentRecord,
} from '@/lib/consent/cookie-consent'
export function useCookieConsent() {
const [consent, setConsentState] = useState<ConsentRecord | null>(null)
const [ready, setReady] = useState(false)
const refresh = useCallback(async () => {
// Try local first, then DB sync for authenticated users
const loaded = await loadConsentWithDBSync()
setConsentState(loaded)
setReady(true)
}, [])
useEffect(() => {
refresh()
const onChange = () => refresh()
const onStorage = (e: StorageEvent) => {
if (e.key === 'memento-consent-v1') refresh()
}
window.addEventListener(CONSENT_CHANGE_EVENT, onChange)
window.addEventListener('storage', onStorage)
return () => {
window.removeEventListener(CONSENT_CHANGE_EVENT, onChange)
window.removeEventListener('storage', onStorage)
}
}, [refresh])
return {
consent,
ready,
hasChoice: ready && hasConsentChoice(),
needsBanner: ready && !hasConsentChoice(),
}
}