perf: memo GridCard, fuse save fns, fix slash tab active color
This commit is contained in:
71
memento-note/app/actions/cookie-consent.ts
Normal file
71
memento-note/app/actions/cookie-consent.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
'use server'
|
||||
|
||||
import { auth } from '@/auth'
|
||||
import { updateAISettings } from './ai-settings'
|
||||
import { getAISettings } from './ai-settings'
|
||||
import type { ConsentRecord } from '@/lib/consent/cookie-consent'
|
||||
|
||||
/**
|
||||
* Get cookie consent from database for authenticated users.
|
||||
* Returns null for guests or when no DB preference exists.
|
||||
*/
|
||||
export async function getCookieConsentFromDB(): Promise<ConsentRecord | null> {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return null
|
||||
}
|
||||
|
||||
try {
|
||||
const settings = await getAISettings(session.user.id)
|
||||
// Only return consent if user has explicitly set it
|
||||
// If anonymousAnalytics is undefined or false, treat as "no preference set"
|
||||
if (settings.anonymousAnalytics === true) {
|
||||
return {
|
||||
version: 1,
|
||||
necessary: true,
|
||||
analytics: true,
|
||||
marketing: false,
|
||||
updatedAt: new Date().toISOString(),
|
||||
}
|
||||
}
|
||||
// If explicitly false, return that too
|
||||
if (settings.anonymousAnalytics === false) {
|
||||
return {
|
||||
version: 1,
|
||||
necessary: true,
|
||||
analytics: false,
|
||||
marketing: false,
|
||||
updatedAt: new Date().toISOString(),
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[getCookieConsentFromDB] Failed to load:', error)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Save cookie consent and sync to database for authenticated users.
|
||||
*
|
||||
* For logged-in users, this updates UserAISettings.anonymousAnalytics in the database.
|
||||
* For guests, this no-ops silently (they rely on localStorage only).
|
||||
*
|
||||
* Call this from client components after setting local consent.
|
||||
*/
|
||||
export async function saveCookieConsent(consent: ConsentRecord) {
|
||||
const session = await auth()
|
||||
|
||||
// Only sync to DB for authenticated users
|
||||
if (session?.user?.id) {
|
||||
try {
|
||||
await updateAISettings({ anonymousAnalytics: consent.analytics })
|
||||
} catch (error) {
|
||||
console.error('[saveCookieConsent] Failed to sync to DB:', error)
|
||||
// Don't throw — local consent is already set, DB sync is best-effort
|
||||
}
|
||||
}
|
||||
|
||||
return { success: true }
|
||||
}
|
||||
Reference in New Issue
Block a user