Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(settings): notification settings show an error when they cannot load

Maurice12962480d515
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(
+ <>
+ <ToastContainer />
+ <NotificationsTab />
+ </>
+ );
+ 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(<NotificationsTab />);
// 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 <MatrixSkeleton label={t('common.loading')} />
+ if (!matrix) return loadFailed ? <SettingsHint>{t('common.error')}</SettingsHint> : <MatrixSkeleton label={t('common.loading')} />
if (visibleChannels.length === 0) {
return <SettingsHint>{t('settings.notificationPreferences.noChannels')}</SettingsHint>
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<void>((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<PreferencesMatrix | null>(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<string | null>(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<string, unknown> }) => {
+ 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 (
<MSetCard title={t('settings.notifications')} icon={Bell}>
- {!matrix && <p className="font-geist text-[0.6875rem] italic text-m-faint">{t('common.loading')}</p>}
+ {!matrix && (
+ <p className="font-geist text-[0.6875rem] italic text-m-faint">{loadFailed ? t('common.error') : t('common.loading')}</p>
+ )}
{matrix && visibleChannels.length === 0 && (
<p className="font-geist text-[0.6875rem] italic text-m-faint">{t('settings.notificationPreferences.noChannels')}</p>
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(<MSettingsNotifications />);
- await waitFor(() => expect(screen.getByText('Loading...')).toBeInTheDocument());
+ expect(await screen.findByText('Error')).toBeInTheDocument();
+ expect(screen.queryByText('Loading...')).not.toBeInTheDocument();
expect(screen.getByText('Notifications')).toBeInTheDocument();
});