'use client' import React, { useState } from 'react' import { useRouter } from 'next/navigation' import { SettingsSection } from '@/components/settings' import { Button } from '@/components/ui/button' import { Loader2, CheckCircle, XCircle, RefreshCw, Database, BrainCircuit } from 'lucide-react' import { cleanupAllOrphans, syncAllEmbeddings } from '@/app/actions/notes' import { toast } from 'sonner' import { useLanguage } from '@/lib/i18n' import Link from 'next/link' import { useLabels } from '@/context/LabelContext' import { useNotebooks } from '@/context/notebooks-context' import { useNoteRefresh } from '@/context/NoteRefreshContext' export default function SettingsPage() { const { t } = useLanguage() const router = useRouter() const { refreshLabels } = useLabels() const { refreshNotebooks } = useNotebooks() const { triggerRefresh } = useNoteRefresh() const [loading, setLoading] = useState(false) const [cleanupLoading, setCleanupLoading] = useState(false) const [syncLoading, setSyncLoading] = useState(false) const [status, setStatus] = useState<'idle' | 'success' | 'error'>('idle') const [result, setResult] = useState(null) const [config, setConfig] = useState(null) const checkConnection = async () => { setLoading(true) setStatus('idle') setResult(null) try { const res = await fetch('/api/ai/test') const data = await res.json() setConfig({ provider: data.provider, status: res.ok ? 'connected' : 'disconnected' }) if (res.ok) { setStatus('success') setResult(data) } else { setStatus('error') setResult(data) } } catch (error: any) { console.error(error) setStatus('error') setResult({ message: error.message, stack: error.stack }) } finally { setLoading(false) } } const handleSync = async () => { setSyncLoading(true) try { const result = await syncAllEmbeddings() if (result.success) { toast.success(t('settings.indexingComplete', { count: result.count ?? 0 })) triggerRefresh() router.refresh() } } catch (error) { console.error(error) toast.error(t('settings.indexingError')) } finally { setSyncLoading(false) } } const handleCleanup = async () => { setCleanupLoading(true) try { const result = await cleanupAllOrphans() if (result.success) { const errCount = Array.isArray(result.errors) ? result.errors.length : 0 if (result.created === 0 && result.deleted === 0 && errCount === 0) { toast.info(t('settings.cleanupNothing')) } else { const base = t('settings.cleanupDone', { created: result.created ?? 0, deleted: result.deleted ?? 0, }) toast.success(errCount > 0 ? `${base} (${t('settings.cleanupWithErrors')})` : base) } await refreshLabels() await refreshNotebooks() triggerRefresh() router.refresh() } else { toast.error(t('settings.cleanupError')) } } catch (error) { console.error(error) toast.error(t('settings.cleanupError')) } finally { setCleanupLoading(false) } } return (

{t('settings.title')}

{t('settings.description')}

{/* Quick Links */}

{t('aiSettings.title')}

{t('aiSettings.description')}

{t('profile.title')}

{t('profile.description')}

{/* AI Diagnostics */} 🔍} description={t('diagnostics.description')} >

{t('diagnostics.configuredProvider')}

{config?.provider || '...'}

{t('diagnostics.apiStatus')}

{status === 'success' && } {status === 'error' && } {status === 'success' ? t('diagnostics.operational') : status === 'error' ? t('diagnostics.errorStatus') : t('diagnostics.checking')}
{result && (

{t('diagnostics.testDetails')}

{JSON.stringify(result, null, 2)}
{status === 'error' && (

{t('diagnostics.troubleshootingTitle')}

  • {t('diagnostics.tip1')}
  • {t('diagnostics.tip2')}
  • {t('diagnostics.tip3')}
  • {t('diagnostics.tip4')}
)}
)}
{/* Maintenance */} 🔧} description={t('settings.maintenanceDescription')} >

{t('settings.cleanTags')}

{t('settings.cleanTagsDescription')}

{t('settings.semanticIndexing')}

{t('settings.semanticIndexingDescription')}

) }