diff --git a/src/hooks/useNotifications.ts b/src/hooks/useNotifications.ts index eed25e445..802e25128 100644 --- a/src/hooks/useNotifications.ts +++ b/src/hooks/useNotifications.ts @@ -10,6 +10,7 @@ import { getEnterpriseAccountToken, getTokenForHost, isEnterpriseHost, + isGitHubLoggedIn, } from '../utils/helpers'; import { setTrayIconColor, @@ -64,8 +65,6 @@ export const useNotifications = (colors: boolean): NotificationsState => { const fetchNotifications = useCallback( async (accounts: AuthState, settings: SettingsState) => { - const isGitHubLoggedIn = accounts.token !== null; - function getNotifications(hostname: string, token: string): AxiosPromise { const endpointSuffix = `notifications?participating=${settings.participating}`; const url = `${generateGitHubAPIUrl(hostname)}${endpointSuffix}`; @@ -73,7 +72,7 @@ export const useNotifications = (colors: boolean): NotificationsState => { } function getGitHubNotifications() { - if (!isGitHubLoggedIn) { + if (!isGitHubLoggedIn(accounts)) { return; } return getNotifications( @@ -111,7 +110,7 @@ export const useNotifications = (colors: boolean): NotificationsState => { }; }, ); - const data = isGitHubLoggedIn + const data = isGitHubLoggedIn(accounts) ? [ ...enterpriseNotifications, { diff --git a/src/utils/helpers.test.ts b/src/utils/helpers.test.ts index 1f74b1131..b4019e3ff 100644 --- a/src/utils/helpers.test.ts +++ b/src/utils/helpers.test.ts @@ -17,9 +17,19 @@ import { generateNotificationReferrerId, getHtmlUrl, isEnterpriseHost, + isGitHubLoggedIn, } from './helpers'; describe('utils/helpers.ts', () => { + describe('isGitHubLoggedIn', () => { + it('logged in', () => { + expect(isGitHubLoggedIn({ ...mockAccounts, token: '1234' })).toBe(true); + }); + + it('logged out', () => { + expect(isGitHubLoggedIn({ ...mockAccounts, token: null })).toBe(false); + }); + }); describe('isEnterpriseHost', () => { it('should return true for enterprise host', () => { expect(isEnterpriseHost('github.manos.im')).toBe(true); diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index 678489102..cb918752f 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -13,6 +13,10 @@ import { openExternalLink } from '../utils/comms'; import { Constants } from './constants'; import { getCheckSuiteAttributes, getWorkflowRunAttributes } from './subject'; +export function isGitHubLoggedIn(accounts: AuthState): boolean { + return accounts.token != null; +} + export function getTokenForHost(hostname: string, accounts: AuthState): string { const isEnterprise = isEnterpriseHost(hostname); const token = isEnterprise