mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 09:54:14 +00:00
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.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user