Files
Momento/memento-note/components/legal/ai-consent-provider.tsx
Antigravity 0784c94242
Some checks failed
CI / Lint, Test & Build (push) Failing after 57s
CI / Deploy production (on server) (push) Has been skipped
feat(notes): vues structurées tableau/kanban, flashcards et MCP robuste
Ajoute la base organisable par carnet (schéma, champs partagés, valeurs par note)
avec activation guidée, tableau éditable, kanban et suppression de colonnes.
Corrige le multiselect en vue tableau et enrichit sidebar, grille et i18n FR/EN.
Inclut aussi les améliorations flashcards SM-2, l'audit consentement IA et la
robustesse du serveur MCP (config, validation, rate-limit, métriques).

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-24 23:03:16 +00:00

201 lines
6.0 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 { getLocalStorageAiConsent, setLocalStorageAiConsent, removeLocalStorageAiConsent } from '@/lib/consent/ai-consent-client'
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 [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)
}
setModalOpen(true)
return new Promise<boolean>((resolve) => {
pendingResolveRef.current = resolve
})
}, [hasAiConsent])
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
}