'use client' import { useState } from 'react' import { updateAISettings } from '@/app/actions/ai-settings' import { DemoModeToggle } from '@/components/demo-mode-toggle' import { toast } from 'sonner' import { Loader2, Sparkles, Brain, Languages, Tag, History, Wand2 } from 'lucide-react' import { useLanguage } from '@/lib/i18n' import { motion } from 'motion/react' import { cn } from '@/lib/utils' interface AISettingsPanelProps { initialSettings: { titleSuggestions: boolean semanticSearch: boolean paragraphRefactor: boolean memoryEcho: boolean memoryEchoFrequency: 'daily' | 'weekly' | 'custom' aiProvider: 'auto' | 'openai' | 'ollama' preferredLanguage: 'auto' | 'en' | 'fr' | 'es' | 'de' | 'fa' | 'it' | 'pt' | 'ru' | 'zh' | 'ja' | 'ko' | 'ar' | 'hi' | 'nl' | 'pl' demoMode: boolean languageDetection: boolean autoLabeling: boolean noteHistory: boolean noteHistoryMode: 'manual' | 'auto' } } export function AISettingsPanel({ initialSettings }: AISettingsPanelProps) { const [settings, setSettings] = useState(initialSettings) const [isPending, setIsPending] = useState(false) const { t } = useLanguage() const handleToggle = async (feature: string, value: boolean) => { setSettings(prev => ({ ...prev, [feature]: value })) try { setIsPending(true) await updateAISettings({ [feature]: value }) toast.success(t('aiSettings.saved')) } catch { toast.error(t('aiSettings.error')) setSettings(initialSettings) } finally { setIsPending(false) } } const handleFrequencyChange = async (value: 'daily' | 'weekly' | 'custom') => { setSettings(prev => ({ ...prev, memoryEchoFrequency: value })) try { setIsPending(true) await updateAISettings({ memoryEchoFrequency: value }) toast.success(t('aiSettings.saved')) } catch { toast.error(t('aiSettings.error')) setSettings(initialSettings) } finally { setIsPending(false) } } const handleHistoryModeChange = async (value: 'manual' | 'auto') => { setSettings(prev => ({ ...prev, noteHistoryMode: value })) try { setIsPending(true) await updateAISettings({ noteHistoryMode: value }) toast.success(t('settings.settingsSaved')) } catch { toast.error(t('aiSettings.error')) setSettings(initialSettings) } finally { setIsPending(false) } } const handleDemoModeToggle = async (enabled: boolean) => { setSettings(prev => ({ ...prev, demoMode: enabled })) try { setIsPending(true) await updateAISettings({ demoMode: enabled }) } catch { toast.error(t('aiSettings.error')) setSettings(initialSettings) throw new Error() } finally { setIsPending(false) } } const features = [ { key: 'titleSuggestions', icon: Wand2, name: t('aiSettings.titleSuggestions'), description: t('aiSettings.titleSuggestionsDesc'), value: settings.titleSuggestions, }, { key: 'paragraphRefactor', icon: Sparkles, name: t('aiSettings.aiNote'), description: t('aiSettings.aiNoteDesc'), value: settings.paragraphRefactor, }, { key: 'memoryEcho', icon: Brain, name: t('memoryEcho.title'), description: t('memoryEcho.dailyInsight'), value: settings.memoryEcho, }, { key: 'languageDetection', icon: Languages, name: t('aiSettings.languageDetection'), description: t('aiSettings.languageDetectionDesc'), value: settings.languageDetection ?? true, }, { key: 'autoLabeling', icon: Tag, name: t('aiSettings.autoLabeling'), description: t('aiSettings.autoLabelingDesc'), value: settings.autoLabeling ?? true, }, { key: 'noteHistory', icon: History, name: t('aiSettings.noteHistory'), description: t('aiSettings.noteHistoryDesc'), value: settings.noteHistory ?? false, }, ] return ( {isPending && (
{t('aiSettings.saving')}
)}

{t('aiSettings.features')}

{features.map(({ key, icon: Icon, name, description, value }) => (

{name}

{description}

{settings.memoryEcho && (

{t('aiSettings.frequency')}

{t('aiSettings.frequencyDesc')}

{(['daily', 'weekly'] as const).map((opt) => (
)} {settings.noteHistory && (

{t('notes.historyMode')}

{t('aiSettings.noteHistoryDesc')}

{([ { value: 'manual' as const, label: t('notes.historyModeManual'), desc: t('notes.historyModeManualDesc') }, { value: 'auto' as const, label: t('notes.historyModeAuto'), desc: t('notes.historyModeAutoDesc') }, ]).map((opt) => (
)}
) }