mirror of
https://github.com/maziggy/bambuddy.git
synced 2026-10-21 10:15:04 +02:00
Add Email settings tab to SettingsPage with SMTP configuration
Co-authored-by: cadtoolbox <12723486+cadtoolbox@users.noreply.github.com>
This commit is contained in:
co-authored by
cadtoolbox
parent
726401810c
commit
5462249345
@@ -0,0 +1,364 @@
|
||||
import { useState } from 'react';
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Mail, Send, Lock, Unlock, AlertTriangle, CheckCircle, Loader2 } from 'lucide-react';
|
||||
import { api } from '../api/client';
|
||||
import type { SMTPSettings, TestSMTPRequest } from '../api/client';
|
||||
import { Card, CardContent, CardHeader } from './Card';
|
||||
import { Button } from './Button';
|
||||
import { useToast } from '../contexts/ToastContext';
|
||||
|
||||
export function EmailSettings() {
|
||||
const { t } = useTranslation();
|
||||
const { showToast } = useToast();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const [smtpSettings, setSMTPSettings] = useState<SMTPSettings>({
|
||||
smtp_host: '',
|
||||
smtp_port: 587,
|
||||
smtp_username: '',
|
||||
smtp_password: '',
|
||||
smtp_use_tls: true,
|
||||
smtp_from_email: '',
|
||||
smtp_from_name: 'BamBuddy',
|
||||
});
|
||||
const [testEmail, setTestEmail] = useState('');
|
||||
|
||||
// Fetch SMTP settings
|
||||
const { data: existingSettings, isLoading } = useQuery({
|
||||
queryKey: ['smtpSettings'],
|
||||
queryFn: () => api.getSMTPSettings(),
|
||||
});
|
||||
|
||||
// Fetch advanced auth status
|
||||
const { data: advancedAuthStatus, refetch: refetchAuthStatus } = useQuery({
|
||||
queryKey: ['advancedAuthStatus'],
|
||||
queryFn: () => api.getAdvancedAuthStatus(),
|
||||
});
|
||||
|
||||
// Load existing settings when fetched
|
||||
useState(() => {
|
||||
if (existingSettings) {
|
||||
setSMTPSettings({
|
||||
...existingSettings,
|
||||
smtp_password: '', // Never show password
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Save SMTP settings
|
||||
const saveMutation = useMutation({
|
||||
mutationFn: (settings: SMTPSettings) => api.saveSMTPSettings(settings),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['smtpSettings'] });
|
||||
queryClient.invalidateQueries({ queryKey: ['advancedAuthStatus'] });
|
||||
showToast('SMTP settings saved successfully', 'success');
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
showToast(error.message, 'error');
|
||||
},
|
||||
});
|
||||
|
||||
// Test SMTP connection
|
||||
const testMutation = useMutation({
|
||||
mutationFn: (request: TestSMTPRequest) => api.testSMTP(request),
|
||||
onSuccess: (data) => {
|
||||
showToast(data.message, data.success ? 'success' : 'error');
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
showToast(error.message, 'error');
|
||||
},
|
||||
});
|
||||
|
||||
// Toggle advanced auth
|
||||
const toggleAdvancedAuthMutation = useMutation({
|
||||
mutationFn: (enabled: boolean) =>
|
||||
enabled ? api.enableAdvancedAuth() : api.disableAdvancedAuth(),
|
||||
onSuccess: (data) => {
|
||||
queryClient.invalidateQueries({ queryKey: ['advancedAuthStatus'] });
|
||||
showToast(data.message, 'success');
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
showToast(error.message, 'error');
|
||||
},
|
||||
});
|
||||
|
||||
const handleSave = () => {
|
||||
// Validate required fields
|
||||
if (!smtpSettings.smtp_host || !smtpSettings.smtp_username || !smtpSettings.smtp_from_email) {
|
||||
showToast('Please fill in all required fields', 'error');
|
||||
return;
|
||||
}
|
||||
saveMutation.mutate(smtpSettings);
|
||||
};
|
||||
|
||||
const handleTest = () => {
|
||||
if (!testEmail) {
|
||||
showToast('Please enter a test email address', 'error');
|
||||
return;
|
||||
}
|
||||
if (!smtpSettings.smtp_host || !smtpSettings.smtp_username || !smtpSettings.smtp_password || !smtpSettings.smtp_from_email) {
|
||||
showToast('Please fill in all SMTP settings before testing', 'error');
|
||||
return;
|
||||
}
|
||||
testMutation.mutate({
|
||||
smtp_host: smtpSettings.smtp_host,
|
||||
smtp_port: smtpSettings.smtp_port,
|
||||
smtp_username: smtpSettings.smtp_username,
|
||||
smtp_password: smtpSettings.smtp_password,
|
||||
smtp_use_tls: smtpSettings.smtp_use_tls,
|
||||
smtp_from_email: smtpSettings.smtp_from_email,
|
||||
test_recipient: testEmail,
|
||||
});
|
||||
};
|
||||
|
||||
const handleToggleAdvancedAuth = () => {
|
||||
if (!advancedAuthStatus?.advanced_auth_enabled && !advancedAuthStatus?.smtp_configured) {
|
||||
showToast('Please configure and test SMTP settings first', 'error');
|
||||
return;
|
||||
}
|
||||
toggleAdvancedAuthMutation.mutate(!advancedAuthStatus?.advanced_auth_enabled);
|
||||
};
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="flex items-center justify-center p-12">
|
||||
<Loader2 className="w-8 h-8 animate-spin text-bambu-green" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Advanced Authentication Toggle */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-2">
|
||||
<Mail className="w-5 h-5 text-bambu-green" />
|
||||
<h2 className="text-lg font-semibold text-white">
|
||||
{t('settings.email.advancedAuth') || 'Advanced Authentication'}
|
||||
</h2>
|
||||
</div>
|
||||
<Button
|
||||
onClick={handleToggleAdvancedAuth}
|
||||
disabled={toggleAdvancedAuthMutation.isPending}
|
||||
variant={advancedAuthStatus?.advanced_auth_enabled ? 'danger' : 'primary'}
|
||||
>
|
||||
{advancedAuthStatus?.advanced_auth_enabled ? (
|
||||
<>
|
||||
<Unlock className="w-4 h-4" />
|
||||
{t('settings.email.disable') || 'Disable'}
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Lock className="w-4 h-4" />
|
||||
{t('settings.email.enable') || 'Enable'}
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="space-y-4">
|
||||
{advancedAuthStatus?.advanced_auth_enabled ? (
|
||||
<div className="bg-green-500/10 border border-green-500/30 rounded-lg p-4">
|
||||
<div className="flex items-start gap-3">
|
||||
<CheckCircle className="w-5 h-5 text-green-400 mt-0.5 flex-shrink-0" />
|
||||
<div className="space-y-2">
|
||||
<p className="text-white font-medium">
|
||||
{t('settings.email.advancedAuthEnabled') || 'Advanced Authentication is enabled'}
|
||||
</p>
|
||||
<ul className="text-sm text-green-300 space-y-1 list-disc list-inside">
|
||||
<li>{t('settings.email.feature1') || 'Passwords are auto-generated and emailed to new users'}</li>
|
||||
<li>{t('settings.email.feature2') || 'Users can login with username or email'}</li>
|
||||
<li>{t('settings.email.feature3') || 'Forgot password feature is available'}</li>
|
||||
<li>{t('settings.email.feature4') || 'Admins can reset user passwords via email'}</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="bg-yellow-500/10 border border-yellow-500/30 rounded-lg p-4">
|
||||
<div className="flex items-start gap-3">
|
||||
<AlertTriangle className="w-5 h-5 text-yellow-400 mt-0.5 flex-shrink-0" />
|
||||
<div className="space-y-2">
|
||||
<p className="text-white font-medium">
|
||||
{t('settings.email.advancedAuthDisabled') || 'Advanced Authentication is disabled'}
|
||||
</p>
|
||||
<p className="text-sm text-yellow-300">
|
||||
{t('settings.email.advancedAuthDisabledDesc') || 'Configure and test SMTP settings below, then enable advanced authentication to activate email-based features.'}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* SMTP Configuration */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<h2 className="text-lg font-semibold text-white">
|
||||
{t('settings.email.smtpSettings') || 'SMTP Configuration'}
|
||||
</h2>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="space-y-4">
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-white mb-2">
|
||||
{t('settings.email.smtpHost') || 'SMTP Server'} *
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={smtpSettings.smtp_host}
|
||||
onChange={(e) => setSMTPSettings({ ...smtpSettings, smtp_host: e.target.value })}
|
||||
placeholder="smtp.gmail.com"
|
||||
className="w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-white mb-2">
|
||||
{t('settings.email.smtpPort') || 'Port'} *
|
||||
</label>
|
||||
<input
|
||||
type="number"
|
||||
value={smtpSettings.smtp_port}
|
||||
onChange={(e) => setSMTPSettings({ ...smtpSettings, smtp_port: parseInt(e.target.value) || 587 })}
|
||||
className="w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-white mb-2">
|
||||
{t('settings.email.username') || 'Username'} *
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={smtpSettings.smtp_username}
|
||||
onChange={(e) => setSMTPSettings({ ...smtpSettings, smtp_username: e.target.value })}
|
||||
placeholder="your.email@gmail.com"
|
||||
className="w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-white mb-2">
|
||||
{t('settings.email.password') || 'Password'} *
|
||||
</label>
|
||||
<input
|
||||
type="password"
|
||||
value={smtpSettings.smtp_password}
|
||||
onChange={(e) => setSMTPSettings({ ...smtpSettings, smtp_password: e.target.value })}
|
||||
placeholder={existingSettings ? '••••••••' : 'Enter password'}
|
||||
className="w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-white mb-2">
|
||||
{t('settings.email.fromEmail') || 'From Email'} *
|
||||
</label>
|
||||
<input
|
||||
type="email"
|
||||
value={smtpSettings.smtp_from_email}
|
||||
onChange={(e) => setSMTPSettings({ ...smtpSettings, smtp_from_email: e.target.value })}
|
||||
placeholder="noreply@yourdomain.com"
|
||||
className="w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-white mb-2">
|
||||
{t('settings.email.fromName') || 'From Name'}
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={smtpSettings.smtp_from_name}
|
||||
onChange={(e) => setSMTPSettings({ ...smtpSettings, smtp_from_name: e.target.value })}
|
||||
placeholder="BamBuddy"
|
||||
className="w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<input
|
||||
type="checkbox"
|
||||
id="use_tls"
|
||||
checked={smtpSettings.smtp_use_tls}
|
||||
onChange={(e) => setSMTPSettings({ ...smtpSettings, smtp_use_tls: e.target.checked })}
|
||||
className="w-4 h-4 rounded border-bambu-gray text-bambu-green focus:ring-bambu-green focus:ring-offset-0 bg-bambu-dark"
|
||||
/>
|
||||
<label htmlFor="use_tls" className="text-sm text-white">
|
||||
{t('settings.email.useTLS') || 'Use TLS (recommended)'}
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div className="flex gap-2">
|
||||
<Button
|
||||
onClick={handleSave}
|
||||
disabled={saveMutation.isPending}
|
||||
className="flex-1"
|
||||
>
|
||||
{saveMutation.isPending ? (
|
||||
<>
|
||||
<Loader2 className="w-4 h-4 animate-spin" />
|
||||
{t('settings.email.saving') || 'Saving...'}
|
||||
</>
|
||||
) : (
|
||||
t('settings.email.save') || 'Save Settings'
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Test SMTP */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<h2 className="text-lg font-semibold text-white">
|
||||
{t('settings.email.testConnection') || 'Test SMTP Connection'}
|
||||
</h2>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="space-y-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-white mb-2">
|
||||
{t('settings.email.testRecipient') || 'Test Recipient Email'}
|
||||
</label>
|
||||
<input
|
||||
type="email"
|
||||
value={testEmail}
|
||||
onChange={(e) => setTestEmail(e.target.value)}
|
||||
placeholder="test@example.com"
|
||||
className="w-full px-4 py-3 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray focus:outline-none focus:ring-2 focus:ring-bambu-green/50 focus:border-bambu-green transition-colors"
|
||||
/>
|
||||
</div>
|
||||
<Button
|
||||
onClick={handleTest}
|
||||
disabled={testMutation.isPending}
|
||||
variant="secondary"
|
||||
>
|
||||
{testMutation.isPending ? (
|
||||
<>
|
||||
<Loader2 className="w-4 h-4 animate-spin" />
|
||||
{t('settings.email.sending') || 'Sending...'}
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Send className="w-4 h-4" />
|
||||
{t('settings.email.sendTest') || 'Send Test Email'}
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { Loader2, Plus, Plug, AlertTriangle, RotateCcw, Bell, Download, RefreshCw, ExternalLink, Globe, Droplets, Thermometer, FileText, Edit2, Send, CheckCircle, XCircle, History, Trash2, Zap, TrendingUp, Calendar, DollarSign, Power, PowerOff, Key, Copy, Database, X, Shield, Printer, Cylinder, Wifi, Home, Video, Users, Lock, Unlock, ChevronDown, ChevronRight, Check, Save } from 'lucide-react';
|
||||
import { Loader2, Plus, Plug, AlertTriangle, RotateCcw, Bell, Download, RefreshCw, ExternalLink, Globe, Droplets, Thermometer, FileText, Edit2, Send, CheckCircle, XCircle, History, Trash2, Zap, TrendingUp, Calendar, DollarSign, Power, PowerOff, Key, Copy, Database, X, Shield, Printer, Cylinder, Wifi, Home, Video, Users, Lock, Unlock, ChevronDown, ChevronRight, Check, Save, Mail } from 'lucide-react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useNavigate, useSearchParams } from 'react-router-dom';
|
||||
import { api } from '../api/client';
|
||||
@@ -19,6 +19,7 @@ import { SpoolmanSettings } from '../components/SpoolmanSettings';
|
||||
import { ExternalLinksSettings } from '../components/ExternalLinksSettings';
|
||||
import { VirtualPrinterSettings } from '../components/VirtualPrinterSettings';
|
||||
import { GitHubBackupSettings } from '../components/GitHubBackupSettings';
|
||||
import { EmailSettings } from '../components/EmailSettings';
|
||||
import { APIBrowser } from '../components/APIBrowser';
|
||||
import { virtualPrinterApi } from '../api/client';
|
||||
import { defaultNavItems, getDefaultView, setDefaultView } from '../components/Layout';
|
||||
@@ -28,7 +29,7 @@ import { useTheme, type ThemeStyle, type DarkBackground, type LightBackground, t
|
||||
import { useState, useEffect, useRef, useCallback } from 'react';
|
||||
import { Palette } from 'lucide-react';
|
||||
|
||||
const validTabs = ['general', 'network', 'plugs', 'notifications', 'filament', 'apikeys', 'virtual-printer', 'users', 'backup'] as const;
|
||||
const validTabs = ['general', 'network', 'plugs', 'notifications', 'filament', 'apikeys', 'virtual-printer', 'users', 'email', 'backup'] as const;
|
||||
type TabType = typeof validTabs[number];
|
||||
|
||||
export function SettingsPage() {
|
||||
@@ -1025,6 +1026,17 @@ export function SettingsPage() {
|
||||
<span className="w-2 h-2 rounded-full bg-green-400" />
|
||||
)}
|
||||
</button>
|
||||
<button
|
||||
onClick={() => handleTabChange('email')}
|
||||
className={`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${
|
||||
activeTab === 'email'
|
||||
? 'text-bambu-green border-bambu-green'
|
||||
: 'text-bambu-gray hover:text-gray-900 dark:hover:text-white border-transparent'
|
||||
}`}
|
||||
>
|
||||
<Mail className="w-4 h-4" />
|
||||
{t('settings.tabs.email') || 'Email'}
|
||||
</button>
|
||||
<button
|
||||
onClick={() => handleTabChange('backup')}
|
||||
className={`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px flex items-center gap-2 ${
|
||||
@@ -4179,6 +4191,11 @@ export function SettingsPage() {
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Email Tab */}
|
||||
{activeTab === 'email' && (
|
||||
<EmailSettings />
|
||||
)}
|
||||
|
||||
{/* Backup Tab */}
|
||||
{activeTab === 'backup' && (
|
||||
<GitHubBackupSettings />
|
||||
|
||||
Reference in New Issue
Block a user