refactor(admin): share the notification settings saves and tests between desktop and phone

useAdminNotificationSettings now holds the SMTP save and test, the trip reminder switch and the admin webhook and ntfy save, test and token clear. The desktop tab drops its inline handlers and both views keep their markup.
This commit is contained in:
Maurice
2026-10-09 10:31:15 +02:00
parent ab3a69e18c
commit 163b4c54e0
4 changed files with 378 additions and 218 deletions
@@ -1,7 +1,7 @@
import { authApi, notificationsApi } from '../../../api/client'
import type { TranslationFn } from '../../../types'
import type { useAdmin } from '../../../pages/admin/useAdmin'
import { SWITCH_ONLY_CHANNELS, useNotificationChannels } from '../../../components/Admin/useNotificationChannels'
import { useAdminNotificationSettings } from '../../../components/Admin/useAdminNotificationSettings'
import MToggle from '../../components/MToggle'
import MAdminNotifyMatrix from './MAdminNotifyMatrix'
import { MAdminButton, MAdminCard, MAdminCardHead, MAdminField, MAdminInput, MAdminRow } from './MAdminUi'
@@ -24,129 +24,14 @@ const SMTP_FIELDS = [
// per-event preference matrix: the desktop notifications tab in mobile cards.
// The channel switches come from useNotificationChannels, shared with desktop.
export default function MAdminNotificationsSection({ admin, t }: MAdminNotificationsSectionProps) {
const { toast, smtpValues, setSmtpValues, smtpLoaded, setTripRemindersEnabled } = admin
const { toast, smtpValues, setSmtpValues, smtpLoaded } = admin
const channels = useNotificationChannels(admin, t)
const emailActive = channels.isActive('email')
const tripRemindersActive = smtpValues.notify_trip_reminder !== 'false'
const smtpConfigured = !!smtpValues.smtp_host?.trim()
const saveSmtp = async () => {
// Saves credentials only — channel activation is auto-saved by the toggle.
const notifKeys = ['smtp_host', 'smtp_port', 'smtp_user', 'smtp_pass', 'smtp_from', 'smtp_skip_tls_verify']
const payload: Record<string, string> = {}
for (const k of notifKeys) {
if (smtpValues[k] !== undefined) payload[k] = smtpValues[k]
}
try {
await authApi.updateAppSettings(payload)
toast.success(t('admin.notifications.saved'))
authApi
.getAppConfig()
.then((c: { trip_reminders_enabled?: boolean }) => {
if (c?.trip_reminders_enabled !== undefined) setTripRemindersEnabled(c.trip_reminders_enabled)
})
.catch(() => {})
} catch {
toast.error(t('common.error'))
}
}
const testSmtp = async () => {
const smtpKeys = ['smtp_host', 'smtp_port', 'smtp_user', 'smtp_pass', 'smtp_from', 'smtp_skip_tls_verify']
const payload: Record<string, string> = {}
for (const k of smtpKeys) {
if (smtpValues[k] !== undefined) payload[k] = smtpValues[k]
}
await authApi.updateAppSettings(payload).catch(() => {})
try {
const result = await notificationsApi.testSmtp()
if (result.success) toast.success(t('admin.smtp.testSuccess'))
else toast.error(result.error || t('admin.smtp.testFailed'))
} catch {
toast.error(t('admin.smtp.testFailed'))
}
}
const toggleTripReminders = async () => {
const next = !tripRemindersActive
setSmtpValues((prev) => ({ ...prev, notify_trip_reminder: next ? 'true' : 'false' }))
try {
await authApi.updateAppSettings({ notify_trip_reminder: next ? 'true' : 'false' })
toast.success(
next ? t('admin.notifications.tripReminders.enabled') : t('admin.notifications.tripReminders.disabled'),
)
authApi
.getAppConfig()
.then((c: { trip_reminders_enabled?: boolean }) => {
if (c?.trip_reminders_enabled !== undefined) setTripRemindersEnabled(c.trip_reminders_enabled)
})
.catch(() => {})
} catch {
setSmtpValues((prev) => ({ ...prev, notify_trip_reminder: tripRemindersActive ? 'true' : 'false' }))
toast.error(t('common.error'))
}
}
const saveAdminWebhook = async () => {
try {
await authApi.updateAppSettings({ admin_webhook_url: smtpValues.admin_webhook_url || '' })
toast.success(t('admin.notifications.adminWebhookPanel.saved'))
} catch {
toast.error(t('common.error'))
}
}
const testAdminWebhook = async () => {
const url = smtpValues.admin_webhook_url === '••••••••' ? undefined : smtpValues.admin_webhook_url
try {
if (url) await authApi.updateAppSettings({ admin_webhook_url: url }).catch(() => {})
const result = await notificationsApi.testWebhook(url)
if (result.success) toast.success(t('admin.notifications.adminWebhookPanel.testSuccess'))
else toast.error(result.error || t('admin.notifications.adminWebhookPanel.testFailed'))
} catch {
toast.error(t('admin.notifications.adminWebhookPanel.testFailed'))
}
}
const saveAdminNtfy = async () => {
try {
await authApi.updateAppSettings({
admin_ntfy_server: smtpValues.admin_ntfy_server || '',
admin_ntfy_topic: smtpValues.admin_ntfy_topic || '',
...(smtpValues.admin_ntfy_token && smtpValues.admin_ntfy_token !== '••••••••'
? { admin_ntfy_token: smtpValues.admin_ntfy_token }
: {}),
})
toast.success(t('admin.notifications.adminNtfyPanel.saved'))
} catch {
toast.error(t('common.error'))
}
}
const testAdminNtfy = async () => {
const topic = smtpValues.admin_ntfy_topic?.trim()
if (!topic) return
try {
const token =
smtpValues.admin_ntfy_token && smtpValues.admin_ntfy_token !== '••••••••' ? smtpValues.admin_ntfy_token : null
const result = await notificationsApi.testNtfy({ topic, server: smtpValues.admin_ntfy_server || null, token })
if (result.success) toast.success(t('admin.notifications.adminNtfyPanel.testSuccess'))
else toast.error(result.error || t('admin.notifications.adminNtfyPanel.testFailed'))
} catch {
toast.error(t('admin.notifications.adminNtfyPanel.testFailed'))
}
}
const clearNtfyToken = async () => {
try {
await authApi.updateAppSettings({ admin_ntfy_token: '' })
setSmtpValues((prev) => ({ ...prev, admin_ntfy_token: '' }))
toast.success(t('admin.notifications.adminNtfyPanel.tokenCleared'))
} catch {
toast.error(t('common.error'))
}
}
const {
tripRemindersActive, smtpConfigured, saveSmtp, testSmtp, toggleTripReminders, saveAdminWebhook, testAdminWebhook,
clearNtfyToken, saveAdminNtfy, testAdminNtfy,
} = useAdminNotificationSettings(admin, t)
return (
<div className="space-y-3">