From 519b666b922327b55090b6aaa9bcd251e52e55a6 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Mon, 8 Apr 2024 06:33:34 -0400 Subject: [PATCH 1/3] refactor(helper): extract isGitHubLoggedIn helper --- src/hooks/useNotifications.ts | 4 ++-- src/utils/helpers.test.ts | 10 ++++++++++ src/utils/helpers.ts | 4 ++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/hooks/useNotifications.ts b/src/hooks/useNotifications.ts index d90bb7699..f6f1c74ed 100644 --- a/src/hooks/useNotifications.ts +++ b/src/hooks/useNotifications.ts @@ -9,6 +9,7 @@ import { generateGitHubAPIUrl, isEnterpriseHost, getTokenForHost, + isGitHubLoggedIn, } from '../utils/helpers'; import { removeNotification } from '../utils/remove-notification'; import { @@ -64,11 +65,10 @@ export const useNotifications = (colors: boolean): NotificationsState => { const fetchNotifications = useCallback( async (accounts: AuthState, settings: SettingsState) => { - const isGitHubLoggedIn = accounts.token !== null; const endpointSuffix = `notifications?participating=${settings.participating}`; function getGitHubNotifications() { - if (!isGitHubLoggedIn) { + if (!isGitHubLoggedIn(accounts)) { return; } const url = `${generateGitHubAPIUrl( diff --git a/src/utils/helpers.test.ts b/src/utils/helpers.test.ts index dfe3f6e77..cc6c76670 100644 --- a/src/utils/helpers.test.ts +++ b/src/utils/helpers.test.ts @@ -8,6 +8,7 @@ import { getHtmlUrl, generateGitHubWebUrl, formatForDisplay, + isGitHubLoggedIn, } from './helpers'; import { mockedGraphQLResponse, @@ -20,6 +21,15 @@ import { mockAccounts } from '../__mocks__/mock-state'; import { SubjectType } from '../typesGithub'; 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 100137a1f..0b48980f5 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 { getWorkflowRunAttributes, getCheckSuiteAttributes } 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 From 4fcddb55741d7858cd951d5585a6bc7584db18a0 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Mon, 8 Apr 2024 06:42:36 -0400 Subject: [PATCH 2/3] refactor(helper): extract isGitHubLoggedIn helper --- src/hooks/useNotifications.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/useNotifications.ts b/src/hooks/useNotifications.ts index f6f1c74ed..e1f7a58f8 100644 --- a/src/hooks/useNotifications.ts +++ b/src/hooks/useNotifications.ts @@ -109,7 +109,7 @@ export const useNotifications = (colors: boolean): NotificationsState => { }; }, ); - const data = isGitHubLoggedIn + const data = isGitHubLoggedIn(accounts) ? [ ...enterpriseNotifications, { From fe7a53ea8fcdcd94f99e8dcb087d649ddd1d2794 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Mon, 8 Apr 2024 08:42:42 -0400 Subject: [PATCH 3/3] fix formatting --- src/utils/helpers.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/helpers.test.ts b/src/utils/helpers.test.ts index 65a9e6c13..b4019e3ff 100644 --- a/src/utils/helpers.test.ts +++ b/src/utils/helpers.test.ts @@ -17,7 +17,7 @@ import { generateNotificationReferrerId, getHtmlUrl, isEnterpriseHost, - isGitHubLoggedIn + isGitHubLoggedIn, } from './helpers'; describe('utils/helpers.ts', () => {