Skip to content

Commit 3cfadd3

Browse files
committed
fix: correctly set count when using delayNotificationState
Signed-off-by: Adam Setch <[email protected]>
1 parent 250b650 commit 3cfadd3

File tree

9 files changed

+54
-84
lines changed

9 files changed

+54
-84
lines changed

src/renderer/components/Sidebar.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ import {
2323
openGitHubPulls,
2424
} from '../utils/links';
2525
import { hasActiveFilters } from '../utils/notifications/filters/filter';
26-
import { getUnreadNotificationCount } from '../utils/notifications/notifications';
2726
import { LogoIcon } from './icons/LogoIcon';
2827

2928
export const Sidebar: FC = () => {
3029
const navigate = useNavigate();
3130
const location = useLocation();
3231

3332
const {
34-
notifications,
3533
fetchNotifications,
3634
isLoggedIn,
3735
status,
3836
settings,
3937
auth,
38+
unreadCount,
39+
hasNotifications,
4040
} = useContext(AppContext);
4141

4242
// We naively assume that the first account is the primary account for the purposes of our sidebar quick links
@@ -65,8 +65,6 @@ export const Sidebar: FC = () => {
6565
fetchNotifications();
6666
};
6767

68-
const notificationsCount = getUnreadNotificationCount(notifications);
69-
7068
const sidebarButtonStyle = { color: 'white' };
7169

7270
return (
@@ -96,14 +94,14 @@ export const Sidebar: FC = () => {
9694
<IconButton
9795
aria-label="Notifications"
9896
data-testid="sidebar-notifications"
99-
description={`${notificationsCount} unread notifications ↗`}
97+
description={`${unreadCount} unread notifications ↗`}
10098
icon={BellIcon}
10199
onClick={() => openGitHubNotifications(primaryAccountHostname)}
102100
size="small"
103101
sx={sidebarButtonStyle}
104102
tooltipDirection="e"
105103
unsafeDisableTooltip={false}
106-
variant={notificationsCount > 0 ? 'primary' : 'invisible'}
104+
variant={hasNotifications ? 'primary' : 'invisible'}
107105
/>
108106

109107
{isLoggedIn && (

src/renderer/components/__snapshots__/Sidebar.test.tsx.snap

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/renderer/components/layout/__snapshots__/AppLayout.test.tsx.snap

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/renderer/context/App.test.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ describe('renderer/context/App.tsx', () => {
4242
.spyOn(notifications, 'setTrayIconColorAndTitle')
4343
.mockImplementation(jest.fn());
4444

45+
jest
46+
.spyOn(notifications, 'getUnreadNotificationCount')
47+
.mockImplementation(jest.fn());
48+
4549
const fetchNotificationsMock = jest.fn();
4650
const markNotificationsAsReadMock = jest.fn();
4751
const markNotificationsAsDoneMock = jest.fn();
@@ -70,8 +74,6 @@ describe('renderer/context/App.tsx', () => {
7074
it('fetch notifications every minute', async () => {
7175
customRender(null);
7276

73-
// Wait for the useEffects, for settings.participating and accounts, to run.
74-
// Those aren't what we're testing
7577
await waitFor(() =>
7678
expect(fetchNotificationsMock).toHaveBeenCalledTimes(1),
7779
);
@@ -80,23 +82,20 @@ describe('renderer/context/App.tsx', () => {
8082
jest.advanceTimersByTime(
8183
Constants.DEFAULT_FETCH_NOTIFICATIONS_INTERVAL_MS,
8284
);
83-
return;
8485
});
8586
expect(fetchNotificationsMock).toHaveBeenCalledTimes(2);
8687

8788
act(() => {
8889
jest.advanceTimersByTime(
8990
Constants.DEFAULT_FETCH_NOTIFICATIONS_INTERVAL_MS,
9091
);
91-
return;
9292
});
9393
expect(fetchNotificationsMock).toHaveBeenCalledTimes(3);
9494

9595
act(() => {
9696
jest.advanceTimersByTime(
9797
Constants.DEFAULT_FETCH_NOTIFICATIONS_INTERVAL_MS,
9898
);
99-
return;
10099
});
101100
expect(fetchNotificationsMock).toHaveBeenCalledTimes(4);
102101
});

src/renderer/context/App.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ import {
4848
setUseAlternateIdleIcon,
4949
setUseUnreadActiveIcon,
5050
} from '../utils/comms';
51-
import { setTrayIconColorAndTitle } from '../utils/notifications/notifications';
51+
import {
52+
getUnreadNotificationCount,
53+
setTrayIconColorAndTitle,
54+
} from '../utils/notifications/notifications';
5255
import { clearState, loadState, saveState } from '../utils/storage';
5356
import {
5457
DEFAULT_DAY_COLOR_SCHEME,
@@ -73,6 +76,8 @@ interface AppContextState {
7376
globalError: GitifyError;
7477

7578
notifications: AccountNotifications[];
79+
unreadCount: number;
80+
hasNotifications: boolean;
7681
fetchNotifications: () => Promise<void>;
7782
removeAccountNotifications: (account: Account) => Promise<void>;
7883

@@ -108,6 +113,10 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
108113
unsubscribeNotification,
109114
} = useNotifications();
110115

116+
const unreadCount = getUnreadNotificationCount(notifications);
117+
118+
const hasNotifications = useMemo(() => unreadCount > 0, [unreadCount]);
119+
111120
// biome-ignore lint/correctness/useExhaustiveDependencies: restoreSettings is stable and should run only once
112121
useEffect(() => {
113122
restoreSettings();
@@ -346,6 +355,8 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
346355
globalError,
347356

348357
notifications,
358+
unreadCount,
359+
hasNotifications,
349360
fetchNotifications: fetchNotificationsWithAccounts,
350361

351362
markNotificationsAsRead: markNotificationsAsReadWithAccounts,
@@ -370,6 +381,8 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
370381
globalError,
371382

372383
notifications,
384+
unreadCount,
385+
hasNotifications,
373386
fetchNotificationsWithAccounts,
374387
markNotificationsAsReadWithAccounts,
375388
markNotificationsAsDoneWithAccounts,

src/renderer/hooks/useNotifications.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export const useNotifications = (): NotificationsState => {
8383
setStatus('loading');
8484
setGlobalError(null);
8585

86+
const previousNotifications = notifications;
8687
const fetchedNotifications = await getAllNotifications(state);
8788

8889
// Set Global Error if all accounts have the same error
@@ -109,8 +110,13 @@ export const useNotifications = (): NotificationsState => {
109110
return;
110111
}
111112

113+
triggerNativeNotifications(
114+
previousNotifications,
115+
fetchedNotifications,
116+
state,
117+
);
118+
112119
setNotifications(fetchedNotifications);
113-
triggerNativeNotifications(notifications, fetchedNotifications, state);
114120

115121
setStatus('success');
116122
},

src/renderer/routes/Notifications.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import { AccountNotifications } from '../components/notifications/AccountNotific
77
import { Oops } from '../components/Oops';
88
import { AppContext } from '../context/App';
99
import { getAccountUUID } from '../utils/auth/utils';
10-
import { getUnreadNotificationCount } from '../utils/notifications/notifications';
1110

1211
export const NotificationsRoute: FC = () => {
13-
const { notifications, status, globalError, settings } =
12+
const { notifications, status, globalError, settings, hasNotifications } =
1413
useContext(AppContext);
1514

1615
const hasMultipleAccounts = useMemo(
@@ -23,11 +22,6 @@ export const NotificationsRoute: FC = () => {
2322
[notifications],
2423
);
2524

26-
const hasNotifications = useMemo(
27-
() => getUnreadNotificationCount(notifications) > 0,
28-
[notifications],
29-
);
30-
3125
if (status === 'error') {
3226
return <Oops error={globalError} />;
3327
}

src/renderer/routes/__snapshots__/Notifications.test.tsx.snap

Lines changed: 12 additions & 54 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/renderer/utils/notifications/native.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe('renderer/utils/notifications/native.ts', () => {
2828
auth: mockAuth,
2929
settings,
3030
});
31+
3132
// wait for async native handling (generateGitHubWebUrl) to complete
3233
await waitFor(() =>
3334
expect(window.gitify.raiseNativeNotification).toHaveBeenCalledTimes(1),
@@ -64,6 +65,7 @@ describe('renderer/utils/notifications/native.ts', () => {
6465
auth: mockAuth,
6566
settings,
6667
});
68+
6769
await waitFor(() =>
6870
expect(window.gitify.raiseNativeNotification).toHaveBeenCalledTimes(1),
6971
);

0 commit comments

Comments
 (0)