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
4 changes: 2 additions & 2 deletions src/renderer/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const Sidebar: FC = () => {
settings,
auth,
unreadCount,
hasNotifications,
hasUnreadNotifications,
} = useContext(AppContext);

// We naively assume that the first account is the primary account for the purposes of our sidebar quick links
Expand Down Expand Up @@ -101,7 +101,7 @@ export const Sidebar: FC = () => {
sx={sidebarButtonStyle}
tooltipDirection="e"
unsafeDisableTooltip={false}
variant={hasNotifications ? 'primary' : 'invisible'}
variant={hasUnreadNotifications ? 'primary' : 'invisible'}
/>

{isLoggedIn && (
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/context/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ describe('renderer/context/App.tsx', () => {
.spyOn(tray, 'setTrayIconColorAndTitle')
.mockImplementation(jest.fn());

jest
.spyOn(notifications, 'getNotificationCount')
.mockImplementation(jest.fn());

jest
.spyOn(notifications, 'getUnreadNotificationCount')
.mockImplementation(jest.fn());
Expand Down
30 changes: 23 additions & 7 deletions src/renderer/context/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ import {
setUseAlternateIdleIcon,
setUseUnreadActiveIcon,
} from '../utils/comms';
import { getUnreadNotificationCount } from '../utils/notifications/notifications';
import {
getNotificationCount,
getUnreadNotificationCount,
} from '../utils/notifications/notifications';
import { clearState, loadState, saveState } from '../utils/storage';
import {
DEFAULT_DAY_COLOR_SCHEME,
Expand All @@ -75,6 +78,7 @@ interface AppContextState {

notifications: AccountNotifications[];
unreadCount: number;
hasUnreadNotifications: boolean;
hasNotifications: boolean;
fetchNotifications: () => Promise<void>;
removeAccountNotifications: (account: Account) => Promise<void>;
Expand Down Expand Up @@ -111,9 +115,17 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
unsubscribeNotification,
} = useNotifications();

const unreadCount = getUnreadNotificationCount(notifications);
const notificationCount = getNotificationCount(notifications);
const unreadNotificationCount = getUnreadNotificationCount(notifications);

const hasNotifications = useMemo(() => unreadCount > 0, [unreadCount]);
const hasNotifications = useMemo(
() => notificationCount > 0,
[notificationCount],
);
const hasUnreadNotifications = useMemo(
() => unreadNotificationCount > 0,
[unreadNotificationCount],
);

// biome-ignore lint/correctness/useExhaustiveDependencies: restoreSettings is stable and should run only once
useEffect(() => {
Expand Down Expand Up @@ -175,12 +187,12 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
useEffect(() => {
setUseUnreadActiveIcon(settings.useUnreadActiveIcon);
setUseAlternateIdleIcon(settings.useAlternateIdleIcon);
setTrayIconColorAndTitle(unreadCount, settings);
setTrayIconColorAndTitle(unreadNotificationCount, settings);
}, [
settings.showNotificationsCountInTray,
settings.useUnreadActiveIcon,
settings.useAlternateIdleIcon,
unreadCount,
unreadNotificationCount,
]);

useEffect(() => {
Expand Down Expand Up @@ -359,8 +371,10 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
globalError,

notifications,
unreadCount,
notificationCount,
unreadNotificationCount,
hasNotifications,
hasUnreadNotifications,
fetchNotifications: fetchNotificationsWithAccounts,

markNotificationsAsRead: markNotificationsAsReadWithAccounts,
Expand All @@ -385,8 +399,10 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
globalError,

notifications,
unreadCount,
notificationCount,
unreadNotificationCount,
hasNotifications,
hasUnreadNotifications,
fetchNotificationsWithAccounts,
markNotificationsAsReadWithAccounts,
markNotificationsAsDoneWithAccounts,
Expand Down
7 changes: 7 additions & 0 deletions src/renderer/utils/notifications/notifications.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { Repository } from '../../typesGitHub';
import * as logger from '../../utils/logger';
import {
enrichNotification,
getNotificationCount,
getUnreadNotificationCount,
stabilizeNotificationsOrder,
} from './notifications';
Expand All @@ -36,6 +37,12 @@ describe('renderer/utils/notifications/notifications.ts', () => {
jest.clearAllMocks();
});

it('getNotificationCount', () => {
const result = getNotificationCount(mockSingleAccountNotifications);

expect(result).toBe(1);
});

it('getUnreadNotificationCount', () => {
const result = getUnreadNotificationCount(mockSingleAccountNotifications);

Expand Down
17 changes: 15 additions & 2 deletions src/renderer/utils/notifications/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,22 @@ import { getFlattenedNotificationsByRepo } from './group';
import { createNotificationHandler } from './handlers';

/**
* Get the count of unread notifications.
* Get the count of notifications across all accounts.
*
* @param notifications - The notifications to check.
* @param notifications - The account notifications to check.
* @returns The count of all notifications.
*/
export function getNotificationCount(notifications: AccountNotifications[]) {
return notifications.reduce(
(sum, account) => sum + account.notifications.length,
0,
);
}

/**
* Get the count of notifications across all accounts.
*
* @param notifications - The account notifications to check.
* @returns The count of unread notifications.
*/
export function getUnreadNotificationCount(
Expand Down