fix: unify theme system - fix theme switching persistence
- Unified localStorage key to 'theme-preference' across all components
- Fixed header.tsx using wrong localStorage key ('theme' instead of 'theme-preference')
- Added localStorage hybrid persistence for instant theme changes
- Removed router.refresh() which was causing stale data revert
- Replaced Blue theme with Sepia
- Consolidated auth() calls to prevent race conditions
- Updated UserSettingsData types to include all themes
This commit is contained in:
@@ -14,20 +14,29 @@ export type UserAISettingsData = {
|
||||
preferredLanguage?: 'auto' | 'en' | 'fr' | 'es' | 'de' | 'fa' | 'it' | 'pt' | 'ru' | 'zh' | 'ja' | 'ko' | 'ar' | 'hi' | 'nl' | 'pl'
|
||||
demoMode?: boolean
|
||||
showRecentNotes?: boolean
|
||||
emailNotifications?: boolean
|
||||
desktopNotifications?: boolean
|
||||
anonymousAnalytics?: boolean
|
||||
theme?: 'light' | 'dark' | 'auto'
|
||||
fontSize?: 'small' | 'medium' | 'large'
|
||||
}
|
||||
|
||||
/**
|
||||
* Update AI settings for the current user
|
||||
*/
|
||||
export async function updateAISettings(settings: UserAISettingsData) {
|
||||
console.log('[updateAISettings] Started with:', JSON.stringify(settings, null, 2))
|
||||
const session = await auth()
|
||||
console.log('[updateAISettings] Session User ID:', session?.user?.id)
|
||||
|
||||
if (!session?.user?.id) {
|
||||
console.error('[updateAISettings] Unauthorized: No session or user ID')
|
||||
throw new Error('Unauthorized')
|
||||
}
|
||||
|
||||
try {
|
||||
// Upsert settings (create if not exists, update if exists)
|
||||
await prisma.userAISettings.upsert({
|
||||
const result = await prisma.userAISettings.upsert({
|
||||
where: { userId: session.user.id },
|
||||
create: {
|
||||
userId: session.user.id,
|
||||
@@ -35,6 +44,7 @@ export async function updateAISettings(settings: UserAISettingsData) {
|
||||
},
|
||||
update: settings
|
||||
})
|
||||
console.log('[updateAISettings] Database upsert successful:', result)
|
||||
|
||||
revalidatePath('/settings/ai')
|
||||
revalidatePath('/')
|
||||
@@ -49,11 +59,16 @@ export async function updateAISettings(settings: UserAISettingsData) {
|
||||
/**
|
||||
* Get AI settings for the current user
|
||||
*/
|
||||
export async function getAISettings() {
|
||||
const session = await auth()
|
||||
export async function getAISettings(userId?: string) {
|
||||
let id = userId
|
||||
|
||||
if (!id) {
|
||||
const session = await auth()
|
||||
id = session?.user?.id
|
||||
}
|
||||
|
||||
// Return defaults for non-logged-in users
|
||||
if (!session?.user?.id) {
|
||||
if (!id) {
|
||||
return {
|
||||
titleSuggestions: true,
|
||||
semanticSearch: true,
|
||||
@@ -63,33 +78,21 @@ export async function getAISettings() {
|
||||
aiProvider: 'auto' as const,
|
||||
preferredLanguage: 'auto' as const,
|
||||
demoMode: false,
|
||||
showRecentNotes: false
|
||||
showRecentNotes: false,
|
||||
emailNotifications: false,
|
||||
desktopNotifications: false,
|
||||
anonymousAnalytics: false,
|
||||
theme: 'light' as const,
|
||||
fontSize: 'medium' as const
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// Use raw SQL query to get showRecentNotes until Prisma client is regenerated
|
||||
const settingsRaw = await prisma.$queryRaw<Array<{
|
||||
titleSuggestions: number
|
||||
semanticSearch: number
|
||||
paragraphRefactor: number
|
||||
memoryEcho: number
|
||||
memoryEchoFrequency: string
|
||||
aiProvider: string
|
||||
preferredLanguage: string
|
||||
fontSize: string
|
||||
demoMode: number
|
||||
showRecentNotes: number
|
||||
}>>`
|
||||
SELECT titleSuggestions, semanticSearch, paragraphRefactor, memoryEcho,
|
||||
memoryEchoFrequency, aiProvider, preferredLanguage, fontSize,
|
||||
demoMode, showRecentNotes
|
||||
FROM UserAISettings
|
||||
WHERE userId = ${session.user.id}
|
||||
`
|
||||
const settings = await prisma.userAISettings.findUnique({
|
||||
where: { userId: id }
|
||||
})
|
||||
|
||||
// Return settings or defaults if not found
|
||||
if (!settingsRaw || settingsRaw.length === 0) {
|
||||
if (!settings) {
|
||||
return {
|
||||
titleSuggestions: true,
|
||||
semanticSearch: true,
|
||||
@@ -99,28 +102,30 @@ export async function getAISettings() {
|
||||
aiProvider: 'auto' as const,
|
||||
preferredLanguage: 'auto' as const,
|
||||
demoMode: false,
|
||||
showRecentNotes: false
|
||||
showRecentNotes: false,
|
||||
emailNotifications: false,
|
||||
desktopNotifications: false,
|
||||
anonymousAnalytics: false,
|
||||
theme: 'light' as const,
|
||||
fontSize: 'medium' as const
|
||||
}
|
||||
}
|
||||
|
||||
const settings = settingsRaw[0]
|
||||
|
||||
// Type-cast database values to proper union types
|
||||
// Handle NULL values - SQLite can return NULL for showRecentNotes if column was added later
|
||||
const showRecentNotesValue = settings.showRecentNotes !== null && settings.showRecentNotes !== undefined
|
||||
? settings.showRecentNotes === 1
|
||||
: false
|
||||
|
||||
return {
|
||||
titleSuggestions: settings.titleSuggestions === 1,
|
||||
semanticSearch: settings.semanticSearch === 1,
|
||||
paragraphRefactor: settings.paragraphRefactor === 1,
|
||||
memoryEcho: settings.memoryEcho === 1,
|
||||
titleSuggestions: settings.titleSuggestions,
|
||||
semanticSearch: settings.semanticSearch,
|
||||
paragraphRefactor: settings.paragraphRefactor,
|
||||
memoryEcho: settings.memoryEcho,
|
||||
memoryEchoFrequency: (settings.memoryEchoFrequency || 'daily') as 'daily' | 'weekly' | 'custom',
|
||||
aiProvider: (settings.aiProvider || 'auto') as 'auto' | 'openai' | 'ollama',
|
||||
preferredLanguage: (settings.preferredLanguage || 'auto') as 'auto' | 'en' | 'fr' | 'es' | 'de' | 'fa' | 'it' | 'pt' | 'ru' | 'zh' | 'ja' | 'ko' | 'ar' | 'hi' | 'nl' | 'pl',
|
||||
demoMode: settings.demoMode === 1,
|
||||
showRecentNotes: showRecentNotesValue
|
||||
demoMode: settings.demoMode,
|
||||
showRecentNotes: settings.showRecentNotes,
|
||||
emailNotifications: settings.emailNotifications,
|
||||
desktopNotifications: settings.desktopNotifications,
|
||||
anonymousAnalytics: settings.anonymousAnalytics,
|
||||
// theme: 'light' as const, // REMOVED: Should not be handled here or hardcoded
|
||||
fontSize: (settings.fontSize || 'medium') as 'small' | 'medium' | 'large'
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error getting AI settings:', error)
|
||||
@@ -134,7 +139,12 @@ export async function getAISettings() {
|
||||
aiProvider: 'auto' as const,
|
||||
preferredLanguage: 'auto' as const,
|
||||
demoMode: false,
|
||||
showRecentNotes: false
|
||||
showRecentNotes: false,
|
||||
emailNotifications: false,
|
||||
desktopNotifications: false,
|
||||
anonymousAnalytics: false,
|
||||
theme: 'light' as const,
|
||||
fontSize: 'medium' as const
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user