"use client"; import { useEffect, useState } from "react"; import { SMTPConfig, SMTPConfigResponse } from "@/lib/types/smtp"; export default function SettingsPage() { const [activeTab, setActiveTab] = useState<'smtp'>('smtp'); const [config, setConfig] = useState({ host: '', port: 587, secure: false, auth: { user: '', pass: '' }, from: { email: '', name: 'Location Tracker' }, replyTo: '', timeout: 10000, }); const [source, setSource] = useState<'database' | 'env'>('env'); const [hasPassword, setHasPassword] = useState(false); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [testing, setTesting] = useState(false); const [message, setMessage] = useState<{ type: 'success' | 'error'; text: string } | null>(null); const [testEmail, setTestEmail] = useState(''); const [showTestModal, setShowTestModal] = useState(false); // Fetch current config useEffect(() => { fetchConfig(); }, []); const fetchConfig = async () => { try { const response = await fetch('/api/admin/settings/smtp'); if (!response.ok) throw new Error('Failed to fetch config'); const data: SMTPConfigResponse = await response.json(); if (data.config) { setConfig(data.config); setHasPassword(data.config.auth.pass === '***'); } setSource(data.source); } catch (error) { console.error('Failed to fetch SMTP config:', error); setMessage({ type: 'error', text: 'Failed to load SMTP configuration' }); } finally { setLoading(false); } }; // Save config const handleSave = async (e: React.FormEvent) => { e.preventDefault(); setSaving(true); setMessage(null); try { const response = await fetch('/api/admin/settings/smtp', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ config }), }); if (!response.ok) { const error = await response.json(); throw new Error(error.error || 'Failed to save'); } setMessage({ type: 'success', text: 'SMTP settings saved successfully' }); setHasPassword(true); setSource('database'); // Clear password field for security setConfig({ ...config, auth: { ...config.auth, pass: '' } }); } catch (error: any) { setMessage({ type: 'error', text: error.message || 'Failed to save settings' }); } finally { setSaving(false); } }; // Reset to defaults const handleReset = async () => { if (!confirm('Reset to environment defaults? This will delete database configuration.')) { return; } try { const response = await fetch('/api/admin/settings/smtp', { method: 'DELETE', }); if (!response.ok) throw new Error('Failed to reset'); setMessage({ type: 'success', text: 'Reset to environment defaults' }); await fetchConfig(); } catch (error) { setMessage({ type: 'error', text: 'Failed to reset settings' }); } }; // Test connection const handleTest = async () => { if (!testEmail) { alert('Please enter a test email address'); return; } setTesting(true); setMessage(null); try { const response = await fetch('/api/admin/settings/smtp/test', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ config: hasPassword ? undefined : config, testEmail, }), }); const data = await response.json(); if (!response.ok) { throw new Error(data.error || 'Test failed'); } setMessage({ type: 'success', text: data.message }); setShowTestModal(false); setTestEmail(''); } catch (error: any) { setMessage({ type: 'error', text: error.message || 'Connection test failed' }); } finally { setTesting(false); } }; if (loading) { return (

Loading settings...

); } return (
{/* Hero Section with Gradient */}

Settings

Konfiguriere System-Einstellungen und Integrationen

{/* Tab Navigation */}
{/* Status Message */} {message && (
{message.type === 'success' ? '✓' : '⚠️'} {message.text}
)} {/* Config Source Info */}
📊

Current Source

{source === 'database' ? 'Database (Custom)' : 'Environment (.env)'}

{/* SMTP Form */}
{/* Host */}
setConfig({ ...config, host: e.target.value.trim() })} placeholder="smtp.gmail.com" className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" /> {config.host.includes('gmail') && (

Gmail detected: Use App Password, not your regular password. Generate App Password

)}
{/* Port and Secure */}
setConfig({ ...config, port: parseInt(e.target.value) })} className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" />
{/* Username */}
setConfig({ ...config, auth: { ...config.auth, user: e.target.value.trim() } })} placeholder="your-email@example.com" className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" />
{/* Password */}
setConfig({ ...config, auth: { ...config.auth, pass: e.target.value.trim() } })} placeholder={hasPassword ? '••••••••' : (config.host.includes('gmail') ? 'Gmail App Password (16 chars)' : 'your-password')} className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" /> {config.host.includes('gmail') && (

Do NOT use your Gmail password. Generate an App Password with 2FA enabled.

)}
{/* From Email */}
setConfig({ ...config, from: { ...config.from, email: e.target.value.trim() } })} placeholder="noreply@example.com" className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" />
{/* From Name */}
setConfig({ ...config, from: { ...config.from, name: e.target.value } })} placeholder="Location Tracker" className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" />
{/* Reply-To */}
setConfig({ ...config, replyTo: e.target.value })} placeholder="support@example.com" className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" />
{/* Timeout */}
setConfig({ ...config, timeout: parseInt(e.target.value) })} className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" />
{/* Buttons */}
{source === 'database' && ( )}
{/* Test Email Modal */} {showTestModal && (

Test SMTP Connection

Enter your email address to receive a test email.

setTestEmail(e.target.value)} placeholder="your-email@example.com" className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 mb-4" />
)}
); }