'use client'; import { useState, useEffect } from 'react'; import { motion } from 'framer-motion'; import { Settings, Shield, Image as ImageIcon, History, Trash2, Save, Loader2, LogOut } from 'lucide-react'; import api from '@/lib/api'; interface SettingsData { theme: string; grid_size: string; trash_auto_purge_days: string; } interface SettingsPageProps { onLogout: () => void; } export default function SettingsPage({ onLogout }: SettingsPageProps) { const [settings, setSettings] = useState(null); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [error, setError] = useState(null); const [success, setSuccess] = useState(false); const [oldPassword, setOldPassword] = useState(''); const [newPassword, setNewPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [pwdSaving, setPwdSaving] = useState(false); const [pwdError, setPwdError] = useState(null); const [pwdSuccess, setPwdSuccess] = useState(false); useEffect(() => { loadSettings(); }, []); function applyTheme(theme: string) { if (theme === 'light') { document.documentElement.setAttribute('data-theme', 'light'); document.documentElement.classList.remove('dark'); document.documentElement.style.setProperty('color-scheme', 'light'); } else { document.documentElement.setAttribute('data-theme', 'dark'); document.documentElement.classList.add('dark'); document.documentElement.style.setProperty('color-scheme', 'dark'); } localStorage.setItem('theme', theme); } async function loadSettings() { try { const data = await api.getSettings(); const s = data.settings || data; const loaded = { theme: s.theme || 'dark', grid_size: s.grid_size || '200', trash_auto_purge_days: s.trash_auto_purge_days || '14', }; setSettings(loaded); // Apply the persisted theme on load applyTheme(loaded.theme); } catch (err: any) { setError(err.message || 'Failed to load settings'); } finally { setLoading(false); } } async function handleSave() { if (!settings) return; setSaving(true); setError(null); setSuccess(false); try { const existing = await api.getSettings(); const merged = { ...(existing?.settings || existing || {}), theme: settings.theme, grid_size: settings.grid_size, trash_auto_purge_days: settings.trash_auto_purge_days, }; await api.updateSettings(merged); setSuccess(true); setTimeout(() => setSuccess(false), 3000); applyTheme(settings.theme); } catch (err: any) { setError(err.message || 'Failed to save settings'); } finally { setSaving(false); } } async function handlePasswordChange(e: React.FormEvent) { e.preventDefault(); if (newPassword !== confirmPassword) { setPwdError("New passwords do not match."); return; } if (newPassword.length < 8) { setPwdError("Password must be at least 8 characters."); return; } setPwdSaving(true); setPwdError(null); setPwdSuccess(false); try { await api.changePassword(oldPassword, newPassword); setPwdSuccess(true); setOldPassword(''); setNewPassword(''); setConfirmPassword(''); setTimeout(() => setPwdSuccess(false), 3000); } catch (err: any) { setPwdError(err.message || 'Failed to change password'); } finally { setPwdSaving(false); } } if (loading) { return (
); } if (!settings) return null; const sections = [ { id: 'appearance', title: 'Appearance', icon: , content: (
setSettings({ ...settings, grid_size: e.target.value })} className="w-full max-w-xs accent-[var(--color-accent)]" />
) }, { id: 'security', title: 'Security', icon: , content: (
setOldPassword(e.target.value)} required className="w-full px-3 py-2 rounded-lg text-sm outline-none" style={{ backgroundColor: 'var(--color-bg-primary)', border: '1px solid var(--color-border)', color: 'var(--color-text-primary)' }} />
setNewPassword(e.target.value)} required minLength={8} className="w-full px-3 py-2 rounded-lg text-sm outline-none" style={{ backgroundColor: 'var(--color-bg-primary)', border: '1px solid var(--color-border)', color: 'var(--color-text-primary)' }} />
setConfirmPassword(e.target.value)} required minLength={8} className="w-full px-3 py-2 rounded-lg text-sm outline-none" style={{ backgroundColor: 'var(--color-bg-primary)', border: '1px solid var(--color-border)', color: 'var(--color-text-primary)' }} />
{pwdError &&

{pwdError}

} {pwdSuccess &&

Password changed successfully!

}
) }, { id: 'trash', title: 'Trash Auto-Purge', icon: , content: (

Files in the trash will be permanently deleted after this many days. Set to 0 to disable auto-purge.

setSettings({ ...settings, trash_auto_purge_days: e.target.value })} className="w-full max-w-xs px-3 py-2 rounded-lg text-sm outline-none" style={{ backgroundColor: 'var(--color-bg-primary)', border: '1px solid var(--color-border)', color: 'var(--color-text-primary)' }} />
) } ]; return (

Settings

Manage your Drive preferences and configurations.

{error && (
{error}
)} {success && (
Settings saved successfully.
)}
{sections.map((section, idx) => (
{section.icon}

{section.title}

{section.content}
))}
); }