import { useState, useEffect } from 'react' import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query' import { useTranslation } from 'react-i18next' import { Card, CardHeader, CardTitle, CardDescription, CardContent, CardFooter } from '../components/ui/Card' import { Button } from '../components/ui/Button' import { Input } from '../components/ui/Input' import { Label } from '../components/ui/Label' import { Alert } from '../components/ui/Alert' import { Badge } from '../components/ui/Badge' import { Skeleton } from '../components/ui/Skeleton' import { Select, SelectTrigger, SelectContent, SelectItem, SelectValue } from '../components/ui/Select' import { toast } from '../utils/toast' import { getSMTPConfig, updateSMTPConfig, testSMTPConnection, sendTestEmail } from '../api/smtp' import type { SMTPConfigRequest } from '../api/smtp' import { Mail, Send, CheckCircle2, XCircle } from 'lucide-react' export default function SMTPSettings() { const { t } = useTranslation() const queryClient = useQueryClient() const [host, setHost] = useState('') const [port, setPort] = useState(587) const [username, setUsername] = useState('') const [password, setPassword] = useState('') const [fromAddress, setFromAddress] = useState('') const [encryption, setEncryption] = useState<'none' | 'ssl' | 'starttls'>('starttls') const [testEmail, setTestEmail] = useState('') const { data: smtpConfig, isLoading } = useQuery({ queryKey: ['smtp-config'], queryFn: getSMTPConfig, }) useEffect(() => { if (smtpConfig) { setHost(smtpConfig.host || '') setPort(smtpConfig.port || 587) setUsername(smtpConfig.username || '') setPassword(smtpConfig.password || '') setFromAddress(smtpConfig.from_address || '') setEncryption(smtpConfig.encryption || 'starttls') } }, [smtpConfig]) const saveMutation = useMutation({ mutationFn: async () => { const config: SMTPConfigRequest = { host, port, username, password, from_address: fromAddress, encryption, } return updateSMTPConfig(config) }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['smtp-config'] }) toast.success(t('smtp.settingsSaved')) }, onError: (error: unknown) => { const err = error as { response?: { data?: { error?: string } } } toast.error(err.response?.data?.error || t('smtp.saveFailed')) }, }) const testConnectionMutation = useMutation({ mutationFn: testSMTPConnection, onSuccess: (data) => { if (data.success) { toast.success(data.message || t('smtp.connectionSuccess')) } else { toast.error(data.error || t('smtp.connectionFailed')) } }, onError: (error: unknown) => { const err = error as { response?: { data?: { error?: string } } } toast.error(err.response?.data?.error || t('smtp.testFailed')) }, }) const sendTestEmailMutation = useMutation({ mutationFn: async () => sendTestEmail({ to: testEmail }), onSuccess: (data) => { if (data.success) { toast.success(data.message || t('smtp.testEmailSent')) setTestEmail('') } else { toast.error(data.error || t('smtp.testEmailFailed')) } }, onError: (error: unknown) => { const err = error as { response?: { data?: { error?: string } } } toast.error(err.response?.data?.error || t('smtp.testEmailFailed')) }, }) if (isLoading) { return (
) } return (

{t('smtp.title')}

{t('smtp.description')}

{/* SMTP Configuration Form */} {t('smtp.configuration')} {t('smtp.configDescription')}
setHost(e.target.value)} placeholder="smtp.gmail.com" />
setPort(parseInt(e.target.value) || 587)} placeholder="587" />
setUsername(e.target.value)} placeholder="your@email.com" autoComplete="username" />
setPassword(e.target.value)} placeholder="••••••••" helperText={t('smtp.passwordHelper')} autoComplete="current-password" />
setFromAddress(e.target.value)} placeholder="Charon " />
{/* Status Indicator */}
{smtpConfig?.configured ? ( <> {t('smtp.configured')} {t('smtp.active')} ) : ( <> {t('smtp.notConfigured')} {t('smtp.inactive')} )}
{/* Test Email */} {smtpConfig?.configured && ( {t('smtp.sendTestEmail')} {t('smtp.testEmailDescription')}
setTestEmail(e.target.value)} placeholder="recipient@example.com" />
)} {/* Help Alert */} {t('smtp.helpText')}
) }