Repository: gitify-app/gitify
Pull request #982 (2024-04-08 10:33:54)

refactor(helper): extract isGitHubLoggedIn helper


Patch:
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 00000000..af717190
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,4 @@
+# Snapshots are taken by our test suite and used to determine regressions.
+# We don't need to review their code in PRs, so treat them as auto-generated.
+# https://docs.github.com/en/repositories/working-with-files/managing-files/customizing-how-changed-files-appear-on-github
+*.snap linguist-generated=true
diff --git a/src/hooks/useNotifications.ts b/src/hooks/useNotifications.ts
index eed25e44..802e2512 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/routes/Settings.tsx b/src/routes/Settings.tsx
index 310e03f6..52701657 100644
--- a/src/routes/Settings.tsx
+++ b/src/routes/Settings.tsx
@@ -250,7 +250,7 @@ export const SettingsRoute: FC = () => {
           >
             <SignOutIcon
               size={18}
-              aria-label={`Logout ${accounts.user.login}`}
+              aria-label={`Logout from ${accounts.user.login}`}
             />
           </button>
 
diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts
index 67848910..cb918752 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


Test patch:
diff --git a/src/routes/__snapshots__/Settings.test.tsx.snap b/src/routes/__snapshots__/Settings.test.tsx.snap
index 34cf42d0..f6916ff5 100644
--- a/src/routes/__snapshots__/Settings.test.tsx.snap
+++ b/src/routes/__snapshots__/Settings.test.tsx.snap
@@ -420,7 +420,7 @@ exports[`routes/Settings.tsx should render itself & its children 1`] = `
         type="button"
       >
         <svg
-          aria-label="Logout octocat"
+          aria-label="Logout from octocat"
           class="octicon octicon-sign-out"
           fill="currentColor"
           focusable="false"
diff --git a/src/utils/helpers.test.ts b/src/utils/helpers.test.ts
index 1f74b113..b4019e3f 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);