From 12962480d515a3ab6c4a5718c9931ef0996bc767 Mon Sep 17 00:00:00 2001 From: Maurice Date: Fri, 9 Oct 2026 17:32:31 +0200 Subject: [PATCH] fix(settings): notification settings show an error when they cannot load When the notification preferences failed to load, the matrix on both the desktop tab and the phone screen stayed on its loading line for good and nothing told the user anything went wrong. A failed load of the webhook and ntfy settings was just as silent and left empty fields that looked like nothing was saved. Both loads in useNotificationSettings ended in .catch(() => {}) and had no cancelled flag. Each failure now shows the common error toast, a failed preferences load sets loadFailed so both views replace the loading line with the error text, and both loads ignore a response that arrives after the screen is gone. Regression test FE-COMP-NOTIFSETTINGS-010 (useNotificationSettings.test.ts) was red on the old code: "expected "vi.fn()" to be called 2 times, but got 0 times". FE-COMP-NOTIFSETTINGS-011 covers the cancelled flag and FE-COMP-NOTIFICATIONS-032 the desktop view. Changed assertions: - MSettingsNotifications.test.tsx FE-MOB-SETNOTIF-002: a failed load used to expect the "Loading..." line to stay; it now expects "Error" and no loading line. --- .../Settings/NotificationsTab.test.tsx | 12 +++++++ .../components/Settings/NotificationsTab.tsx | 4 +-- .../Settings/useNotificationSettings.test.ts | 34 ++++++++++++++++++ .../Settings/useNotificationSettings.ts | 35 ++++++++++++++++--- .../settings/MSettingsNotifications.tsx | 6 ++-- .../settings/MSettingsNotifications.test.tsx | 5 +-- 6 files changed, 86 insertions(+), 10 deletions(-) diff --git a/client/src/components/Settings/NotificationsTab.test.tsx b/client/src/components/Settings/NotificationsTab.test.tsx index 54a9876aa..9fd52ec7b 100644 --- a/client/src/components/Settings/NotificationsTab.test.tsx +++ b/client/src/components/Settings/NotificationsTab.test.tsx @@ -57,6 +57,18 @@ describe('NotificationsTab', () => { expect(screen.getByText('Loading...')).toBeInTheDocument(); }); + it('FE-COMP-NOTIFICATIONS-032: a failed preferences load shows an error instead of loading for good', async () => { + server.use(http.get('/api/notifications/preferences', () => HttpResponse.json({ error: 'boom' }, { status: 500 }))); + render( + <> + + + + ); + await waitFor(() => expect(screen.queryByText('Loading...')).not.toBeInTheDocument()); + expect(screen.getAllByText('Error').length).toBeGreaterThan(0); + }); + it('FE-COMP-NOTIFICATIONS-002: renders the matrix after preferences load', async () => { render(); // The event label is translated; fallback is the key itself diff --git a/client/src/components/Settings/NotificationsTab.tsx b/client/src/components/Settings/NotificationsTab.tsx index f7c04866a..bc36483a0 100644 --- a/client/src/components/Settings/NotificationsTab.tsx +++ b/client/src/components/Settings/NotificationsTab.tsx @@ -16,14 +16,14 @@ import { SettingsCard, SettingsHint, StatusPill, SETTINGS_BUTTON, SETTINGS_BUTTO export default function NotificationsTab(): React.ReactElement { const { t } = useTranslation() const { - matrix, saving, visibleChannels, hasChannel, pluginChannels, toggle, testChannel, channelTesting, + matrix, loadFailed, saving, visibleChannels, hasChannel, pluginChannels, toggle, testChannel, channelTesting, webhookUrl, setWebhookUrl, webhookIsSet, webhookSaving, webhookTesting, saveWebhookUrl, testWebhookUrl, ntfyTopic, setNtfyTopic, ntfyServer, setNtfyServer, ntfyToken, setNtfyToken, ntfyTokenIsSet, ntfySaving, ntfyTesting, saveNtfySettings, clearNtfyToken, testNtfySettings, } = useNotificationSettings() const renderMatrix = () => { - if (!matrix) return + if (!matrix) return loadFailed ? {t('common.error')} : if (visibleChannels.length === 0) { return {t('settings.notificationPreferences.noChannels')} diff --git a/client/src/components/Settings/useNotificationSettings.test.ts b/client/src/components/Settings/useNotificationSettings.test.ts index e4d5bca62..ea88781d3 100644 --- a/client/src/components/Settings/useNotificationSettings.test.ts +++ b/client/src/components/Settings/useNotificationSettings.test.ts @@ -191,4 +191,38 @@ describe('useNotificationSettings', () => { expect(toast.error).toHaveBeenCalledWith('settings.notificationPreferences.testFailed'); expect(result.current.channelTesting).toBeNull(); }); + + it('FE-COMP-NOTIFSETTINGS-010: a failed load flags the matrix as failed and toasts once, however many requests failed', async () => { + vi.spyOn(notificationsApi, 'getPreferences').mockRejectedValue(new Error('x')); + vi.spyOn(settingsApi, 'get').mockRejectedValue(new Error('y')); + const { result } = renderHook(() => useNotificationSettings()); + await waitFor(() => expect(result.current.loadFailed).toBe(true)); + // Let both rejections settle before counting toasts. + await act(() => new Promise((resolve) => setTimeout(resolve, 0))); + expect(toast.error).toHaveBeenCalledTimes(1); + expect(toast.error).toHaveBeenCalledWith('common.error'); + expect(result.current.matrix).toBeNull(); + }); + + it('FE-COMP-NOTIFSETTINGS-011: a load that settles after unmount touches nothing', async () => { + let rejectPrefs!: (e: Error) => void; + let rejectSettings!: (e: Error) => void; + vi.spyOn(notificationsApi, 'getPreferences').mockReturnValue( + new Promise((_, reject) => { + rejectPrefs = reject; + }) + ); + vi.spyOn(settingsApi, 'get').mockReturnValue( + new Promise((_, reject) => { + rejectSettings = reject; + }) + ); + const { unmount } = renderHook(() => useNotificationSettings()); + unmount(); + await act(async () => { + rejectPrefs(new Error('x')); + rejectSettings(new Error('y')); + }); + expect(toast.error).not.toHaveBeenCalled(); + }); }); diff --git a/client/src/components/Settings/useNotificationSettings.ts b/client/src/components/Settings/useNotificationSettings.ts index 62b811851..d621c1a80 100644 --- a/client/src/components/Settings/useNotificationSettings.ts +++ b/client/src/components/Settings/useNotificationSettings.ts @@ -1,4 +1,4 @@ -import { useEffect, useState } from 'react'; +import { useEffect, useEffectEvent, useState } from 'react'; import { notificationsApi, settingsApi } from '../../api/client'; import { useTranslation } from '../../i18n'; @@ -44,6 +44,7 @@ export function useNotificationSettings({ skipMaskedToken = false }: Notificatio const { t } = useTranslation(); const toast = useToast(); const [matrix, setMatrix] = useState(null); + const [loadFailed, setLoadFailed] = useState(false); const [saving, setSaving] = useState(false); const [webhookUrl, setWebhookUrl] = useState(''); const [webhookIsSet, setWebhookIsSet] = useState(false); @@ -57,14 +58,34 @@ export function useNotificationSettings({ skipMaskedToken = false }: Notificatio const [ntfyTesting, setNtfyTesting] = useState(false); const [channelTesting, setChannelTesting] = useState(null); + // The loader runs once; the error toast reads the latest t and toast. + const loadError = useEffectEvent(() => toast.error(t('common.error'))); + useEffect(() => { + let cancelled = false; + // Both loads fail together when the server is down; one toast is enough, + // the matrix shows its own error line. + let toasted = false; + const failed = () => { + if (toasted) return; + toasted = true; + loadError(); + }; notificationsApi .getPreferences() - .then((data: PreferencesMatrix) => setMatrix(data)) - .catch(() => {}); + .then((data: PreferencesMatrix) => { + if (!cancelled) setMatrix(data); + }) + .catch(() => { + // Without this the matrix would sit on its loading line for good. + if (cancelled) return; + setLoadFailed(true); + failed(); + }); settingsApi .get() .then((data: { settings: Record }) => { + if (cancelled) return; const val = (data.settings?.webhook_url as string) || ''; if (val === MASKED) { setWebhookIsSet(true); @@ -82,7 +103,12 @@ export function useNotificationSettings({ skipMaskedToken = false }: Notificatio setNtfyToken(rawToken); } }) - .catch(() => {}); + .catch(() => { + if (!cancelled) failed(); + }); + return () => { + cancelled = true; + }; }, []); // Columns are whatever the server says exists and the admin turned on, so a @@ -227,6 +253,7 @@ export function useNotificationSettings({ skipMaskedToken = false }: Notificatio return { matrix, + loadFailed, saving, visibleChannels, hasChannel, diff --git a/client/src/mobile/screens/settings/MSettingsNotifications.tsx b/client/src/mobile/screens/settings/MSettingsNotifications.tsx index 8d171ae8a..4b417df7b 100644 --- a/client/src/mobile/screens/settings/MSettingsNotifications.tsx +++ b/client/src/mobile/screens/settings/MSettingsNotifications.tsx @@ -17,7 +17,7 @@ export default function MSettingsNotifications() { const { t } = useTranslation() const navigate = useNavigate() const { - matrix, saving, visibleChannels, hasChannel, pluginChannels, toggle, testChannel, channelTesting, + matrix, loadFailed, saving, visibleChannels, hasChannel, pluginChannels, toggle, testChannel, channelTesting, webhookUrl, setWebhookUrl, webhookIsSet, webhookSaving, webhookTesting, saveWebhookUrl, testWebhookUrl, ntfyTopic, setNtfyTopic, ntfyServer, setNtfyServer, ntfyToken, setNtfyToken, ntfyTokenIsSet, ntfySaving, ntfyTesting, saveNtfySettings, clearNtfyToken, testNtfySettings, @@ -25,7 +25,9 @@ export default function MSettingsNotifications() { return ( - {!matrix &&

{t('common.loading')}

} + {!matrix && ( +

{loadFailed ? t('common.error') : t('common.loading')}

+ )} {matrix && visibleChannels.length === 0 && (

{t('settings.notificationPreferences.noChannels')}

diff --git a/client/tests/unit/mobile/settings/MSettingsNotifications.test.tsx b/client/tests/unit/mobile/settings/MSettingsNotifications.test.tsx index 4299149cb..df6d3289b 100644 --- a/client/tests/unit/mobile/settings/MSettingsNotifications.test.tsx +++ b/client/tests/unit/mobile/settings/MSettingsNotifications.test.tsx @@ -77,13 +77,14 @@ describe('MSettingsNotifications', () => { expect(screen.getByText('Loading...')).toBeInTheDocument(); }); - it('FE-MOB-SETNOTIF-002: a failing preferences load keeps the loading line instead of crashing', async () => { + it('FE-MOB-SETNOTIF-002: a failing preferences load shows an error instead of the loading line', async () => { server.use( http.get('/api/notifications/preferences', () => HttpResponse.json({ error: 'boom' }, { status: 500 })), http.get('/api/settings', () => HttpResponse.json({ error: 'boom' }, { status: 500 })), ); render(); - await waitFor(() => expect(screen.getByText('Loading...')).toBeInTheDocument()); + expect(await screen.findByText('Error')).toBeInTheDocument(); + expect(screen.queryByText('Loading...')).not.toBeInTheDocument(); expect(screen.getByText('Notifications')).toBeInTheDocument(); });