Files
Momento/memento-note/components/legal/ai-consent-provider.tsx
Antigravity f385d43d5d
All checks were successful
CI / Lint, Unit Tests & Build (push) Successful in 7m27s
CI / Deploy production (on server) (push) Successful in 23s
feat: redirect to AI consent settings when consent is missing
- requestAiConsent now redirects to /settings/general?highlight=aiConsent
  instead of opening the modal when called outside the settings page
- Settings page highlights the consent section and scrolls to it
- Adds consent.ai.settingsBanner i18n key (fr/en)
- Keeps inline modal on /settings/general so the grant button still works
2026-07-18 17:52:40 +00:00

215 lines
6.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use client'
import { createContext, useContext, useState, useEffect, useRef, useCallback } from 'react'
import { useSession } from 'next-auth/react'
import { useRouter } from 'next/navigation'
import { getLocalStorageAiConsent, setLocalStorageAiConsent, removeLocalStorageAiConsent } from '@/lib/consent/ai-consent-client'
import { redirectToAiConsentSettings } from '@/lib/consent/ai-consent-redirect'
import { updateAISettings } from '@/app/actions/ai-settings'
import { AiConsentModal } from './ai-consent-modal'
import { toast } from 'sonner'
import { useLanguage } from '@/lib/i18n'
interface AiConsentContextType {
hasAiConsent: boolean
requestAiConsent: () => Promise<boolean>
revokeConsent: () => Promise<void>
}
const AiConsentContext = createContext<AiConsentContextType | undefined>(undefined)
interface AiConsentProviderProps {
children: React.ReactNode
initialPersistentConsent?: boolean
}
export function AiConsentProvider({ children, initialPersistentConsent = false }: AiConsentProviderProps) {
const { data: session, update: updateSession } = useSession()
const { t } = useLanguage()
const router = useRouter()
const [persistentConsent, setPersistentConsent] = useState(false)
const [sessionConsent, setSessionConsent] = useState(false)
const [modalOpen, setModalOpen] = useState(false)
const [hydrated, setHydrated] = useState(false)
const pendingResolveRef = useRef<((value: boolean) => void) | null>(null)
useEffect(() => {
const local = getLocalStorageAiConsent()
if (local || initialPersistentConsent) {
setPersistentConsent(true)
if (initialPersistentConsent && !local) {
setLocalStorageAiConsent(true)
}
}
setHydrated(true)
}, [initialPersistentConsent])
useEffect(() => {
if (session?.aiSessionConsent === true) {
setSessionConsent(true)
} else if (session?.aiSessionConsent === false) {
setSessionConsent(false)
}
}, [session?.aiSessionConsent])
const hasAiConsent =
hydrated &&
(persistentConsent || sessionConsent || session?.aiSessionConsent === true)
const requestAiConsent = useCallback((): Promise<boolean> => {
if (hasAiConsent) {
return Promise.resolve(true)
}
// On settings page we keep the inline modal; elsewhere redirect to settings
// so the user sees exactly where to grant consent.
if (typeof window !== 'undefined' && window.location.pathname.startsWith('/settings/general')) {
setModalOpen(true)
return new Promise<boolean>((resolve) => {
pendingResolveRef.current = resolve
})
}
redirectToAiConsentSettings(router, { replace: false })
return new Promise<boolean>((resolve) => {
pendingResolveRef.current = resolve
pendingResolveRef.current(false)
pendingResolveRef.current = null
})
}, [hasAiConsent, router])
const handleConfirm = async (remember: boolean) => {
setModalOpen(false)
const grantConsentLocally = async () => {
if (remember) {
setLocalStorageAiConsent(true)
setPersistentConsent(true)
await updateAISettings({ aiProcessingConsent: true })
} else {
await updateSession({ aiSessionConsent: true })
setSessionConsent(true)
}
}
try {
const res = await fetch('/api/user/ai-consent', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ consent: true, remember }),
})
if (res.ok) {
const data = await res.json().catch(() => ({}))
if (data?.auditLogged === false) {
console.warn('[AiConsentProvider] Consent saved without audit trail')
}
if (remember) {
setLocalStorageAiConsent(true)
setPersistentConsent(true)
try {
await updateAISettings({ aiProcessingConsent: true })
} catch (e) {
console.error('[AiConsentProvider] Failed to sync consent to DB:', e)
}
} else {
await updateSession({ aiSessionConsent: true })
setSessionConsent(true)
}
} else {
try {
await grantConsentLocally()
} catch (fallbackError) {
console.error('[AiConsentProvider] Consent fallback failed:', fallbackError)
toast.error(t('consent.ai.auditFailed') || 'Impossible denregistrer votre consentement. Réessayez.')
pendingResolveRef.current?.(false)
pendingResolveRef.current = null
return
}
}
pendingResolveRef.current?.(true)
pendingResolveRef.current = null
} catch (e) {
console.error('[AiConsentProvider] Failed to log consent audit:', e)
try {
await grantConsentLocally()
pendingResolveRef.current?.(true)
} catch (fallbackError) {
console.error('[AiConsentProvider] Consent fallback failed:', fallbackError)
toast.error(t('consent.ai.auditFailed') || 'Impossible denregistrer votre consentement. Réessayez.')
pendingResolveRef.current?.(false)
}
pendingResolveRef.current = null
}
}
const handleClose = () => {
setModalOpen(false)
toast.warning(t('consent.ai.aborted') || 'Traitement IA annulé (consentement refusé).')
pendingResolveRef.current?.(false)
pendingResolveRef.current = null
}
const revokeConsent = async () => {
removeLocalStorageAiConsent()
setPersistentConsent(false)
setSessionConsent(false)
try {
await fetch('/api/user/ai-consent', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ consent: false, remember: true }),
})
} catch (e) {
console.error('[AiConsentProvider] Failed to log revocation:', e)
}
try {
await updateSession({ aiSessionConsent: false })
} catch (e) {
console.error('[AiConsentProvider] Failed to clear session consent:', e)
}
if (session?.user) {
try {
await updateAISettings({ aiProcessingConsent: false })
} catch (e) {
console.error('[AiConsentProvider] Failed to sync revocation to DB:', e)
}
}
toast.success(t('consent.ai.revokedToast') || 'Consentement IA révoqué avec succès.')
}
return (
<AiConsentContext.Provider
value={{
hasAiConsent,
requestAiConsent,
revokeConsent,
}}
>
{children}
<AiConsentModal
open={modalOpen}
onClose={handleClose}
onConfirm={handleConfirm}
/>
</AiConsentContext.Provider>
)
}
export function useAiConsent() {
const context = useContext(AiConsentContext)
if (context === undefined) {
throw new Error('useAiConsent must be used within an AiConsentProvider')
}
return context
}