Files
Momento/memento-note/app/actions/user-settings.ts
Antigravity e2672cd2c2
Some checks failed
CI / Lint, Test & Build (push) Failing after 1m19s
CI / Deploy production (on server) (push) Has been skipped
feat(notes): liens internes, onglet Réseau, living blocks et consentement IA
Rend les liens entre notes visibles et persistants (sync NoteLink au save, auto-save, graphe réseau rafraîchi), ajoute living blocks, Memory Echo, recherche globale, consentement IA explicite et consolide les prototypes design en architectural-grid.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-24 14:27:29 +00:00

91 lines
2.4 KiB
TypeScript

'use server'
import { auth } from '@/auth'
import { prisma } from '@/lib/prisma'
import { revalidatePath, updateTag } from 'next/cache'
import { unstable_cache } from 'next/cache'
import { normalizeThemeId, type ThemeId } from '@/lib/apply-document-theme'
export type UserSettingsData = {
theme?: ThemeId
cardSizeMode?: 'variable' | 'uniform'
accentColor?: string
}
/**
* Update user settings (theme, etc.)
*/
export async function updateUserSettings(settings: UserSettingsData) {
const session = await auth()
if (!session?.user?.id) {
console.error('[updateUserSettings] Unauthorized')
throw new Error('Unauthorized')
}
try {
const data: { theme?: string; cardSizeMode?: 'variable' | 'uniform'; accentColor?: string } = {}
if (settings.theme !== undefined) data.theme = normalizeThemeId(settings.theme)
if (settings.cardSizeMode !== undefined) data.cardSizeMode = settings.cardSizeMode
if (settings.accentColor !== undefined) data.accentColor = settings.accentColor
await prisma.user.update({
where: { id: session.user.id },
data,
})
revalidatePath('/', 'layout')
updateTag('user-settings')
return { success: true }
} catch (error) {
console.error('Error updating user settings:', error)
throw new Error('Failed to update user settings: ' + (error instanceof Error ? error.message : String(error)))
}
}
const getCachedUserSettings = unstable_cache(
async (userId: string) => {
try {
const user = await prisma.user.findUnique({
where: { id: userId },
select: { theme: true, cardSizeMode: true, accentColor: true },
})
return {
theme: normalizeThemeId(user?.theme || 'light'),
cardSizeMode: (user?.cardSizeMode || 'variable') as 'variable' | 'uniform',
accentColor: user?.accentColor || '#A47148',
}
} catch (error) {
console.error('Error getting user settings:', error)
return {
theme: 'light' as const satisfies ThemeId,
cardSizeMode: 'variable' as const,
accentColor: '#A47148',
}
}
},
['user-settings'],
{ tags: ['user-settings'] }
)
export async function getUserSettings(userId?: string) {
let id = userId
if (!id) {
const session = await auth()
id = session?.user?.id
}
if (!id) {
return {
theme: 'light' as const satisfies ThemeId,
cardSizeMode: 'variable' as const,
accentColor: '#A47148',
}
}
return getCachedUserSettings(id)
}