Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 27 additions & 31 deletions src/renderer/hooks/notifications/useNotificationEffects.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useRef } from 'react';
import { useCallback, useEffect, useEffectEvent, useRef } from 'react';

import { focusManager, type QueryKey, useQueryClient } from '@tanstack/react-query';

Expand Down Expand Up @@ -75,6 +75,30 @@ export const useNotificationEffects = ({
const showSystemNotifications = useSettingsStore((s) => s.showNotifications);
const notificationVolume = useSettingsStore((s) => s.notificationVolume);

// Read the alert settings at alert time so changing them does not re-run the
// notification diff and does not capture stale values.
const announceNewNotifications = useEffectEvent((notificationsToRaise: GitifyNotification[]) => {
if (playSoundNewNotifications) {
void raiseSoundNotification(notificationVolume);
}

if (showSystemNotifications) {
void Promise.resolve(raiseNativeNotification(notificationsToRaise))
.then((published) => {
for (const payload of published ?? []) {
publishedIdsRef.current.add(payload.id);
}
})
.catch((error) => {
rendererLogError(
'useNotificationEffects',
'Failed to publish native notifications',
toError(error),
);
});
}
});

useEffect(() => {
actionsRef.current = actions;
}, [actions]);
Expand Down Expand Up @@ -213,40 +237,12 @@ export const useNotificationEffects = ({
) as GitifyNotification[];

if (filteredDiffNotifications.length > 0) {
if (playSoundNewNotifications) {
void raiseSoundNotification(notificationVolume);
}

if (showSystemNotifications) {
void Promise.resolve(raiseNativeNotification(filteredDiffNotifications))
.then((published) => {
for (const payload of published ?? []) {
publishedIdsRef.current.add(payload.id);
}
})
.catch((error) => {
rendererLogError(
'useNotificationEffects',
'Failed to publish native notifications',
toError(error),
);
});
}
announceNewNotifications(filteredDiffNotifications);
}
}

previousNotificationsRef.current = unfilteredNotifications;
}, [
enabled,
notifications,
isLoading,
isError,
playSoundNewNotifications,
showSystemNotifications,
notificationVolume,
queryClient,
notificationsQueryKey,
]);
}, [enabled, notifications, isLoading, isError, queryClient, notificationsQueryKey]);

// Remove published notifications when they become read, leave the cache,
// or when no unread notifications remain for the summary.
Expand Down
42 changes: 42 additions & 0 deletions src/renderer/hooks/useNotifications.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,48 @@ describe('renderer/hooks/useNotifications.ts', () => {
expect(raiseNativeNotificationSpy).toHaveBeenCalledTimes(1);
});

it('uses the latest alert settings for the next alert without re-registering the effect', async () => {
useSettingsStore.setState({
notificationVolume: 20 as Percentage,
playSound: true,
showNotifications: false,
});
getAllNotificationsMock.mockResolvedValueOnce(mockSingleAccountNotifications);

const { result } = renderNotificationsHook();
await waitFor(() => expect(result.current.hasNotifications).toBe(true));
await waitFor(() => expect(raiseSoundNotificationSpy).toHaveBeenCalledWith(20));
expect(raiseNativeNotificationSpy).not.toHaveBeenCalled();

act(() => {
useSettingsStore.setState({
notificationVolume: 60 as Percentage,
showNotifications: true,
});
});

getAllNotificationsMock.mockResolvedValueOnce([
{
account: mockGitHubCloudAccount,
error: null,
notifications: [
mockGitifyNotification,
mockGitHubCloudGitifyNotifications[1],
] as AccountNotifications['notifications'],
},
]);

await act(async () => {
await result.current.refetchNotifications();
});

await waitFor(() => {
expect(raiseSoundNotificationSpy).toHaveBeenCalledTimes(2);
expect(raiseSoundNotificationSpy).toHaveBeenLastCalledWith(60);
expect(raiseNativeNotificationSpy).toHaveBeenCalledTimes(1);
});
});

it('does not raise sound or native notifications when disabled', async () => {
useSettingsStore.setState({
playSound: false,
Expand Down
Loading