From a46fc983d555397f21eade9ae6273326d35daa19 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Tue, 6 Aug 2024 01:57:18 -0400 Subject: [PATCH 1/2] feat(settings): disable show accounts option if multiple accounts authenticated Signed-off-by: Adam Setch --- src/components/settings/AppearanceSettings.test.tsx | 10 ++++++++-- src/components/settings/AppearanceSettings.tsx | 5 +++-- src/routes/__snapshots__/Settings.test.tsx.snap | 3 +++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/components/settings/AppearanceSettings.test.tsx b/src/components/settings/AppearanceSettings.test.tsx index 843b39d05..50d721387 100644 --- a/src/components/settings/AppearanceSettings.test.tsx +++ b/src/components/settings/AppearanceSettings.test.tsx @@ -1,7 +1,11 @@ import { act, fireEvent, render, screen } from '@testing-library/react'; import { webFrame } from 'electron'; import { MemoryRouter } from 'react-router-dom'; -import { mockAuth, mockSettings } from '../../__mocks__/state-mocks'; +import { + mockAuth, + mockGitHubAppAccount, + mockSettings, +} from '../../__mocks__/state-mocks'; import { AppContext } from '../../context/App'; import { AppearanceSettings } from './AppearanceSettings'; @@ -200,7 +204,9 @@ describe('routes/components/settings/AppearanceSettings.tsx', () => { render( { - const { settings, updateSetting } = useContext(AppContext); + const { auth, settings, updateSetting } = useContext(AppContext); const [zoomPercentage, setZoomPercentage] = useState( zoomLevelToPercentage(webFrame.getZoomLevel()), ); @@ -198,7 +198,8 @@ export const AppearanceSettings: FC = () => { 1} + disabled={auth.accounts.length > 1} onChange={(evt) => updateSetting('showAccountHostname', evt.target.checked) } diff --git a/src/routes/__snapshots__/Settings.test.tsx.snap b/src/routes/__snapshots__/Settings.test.tsx.snap index 54e98764e..667c619f1 100644 --- a/src/routes/__snapshots__/Settings.test.tsx.snap +++ b/src/routes/__snapshots__/Settings.test.tsx.snap @@ -327,7 +327,9 @@ exports[`routes/Settings.tsx should render itself & its children 1`] = ` class="flex items-center" > @@ -337,6 +339,7 @@ exports[`routes/Settings.tsx should render itself & its children 1`] = ` From c7211948d83ee35ee452f5b4bc0b23cd55e816ae Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Fri, 9 Aug 2024 11:03:36 -0400 Subject: [PATCH 2/2] refactor: extract to utils Signed-off-by: Adam Setch --- .../settings/AppearanceSettings.tsx | 5 +-- src/context/App.tsx | 3 +- src/utils/auth/utils.test.ts | 33 +++++++++++++++++++ src/utils/auth/utils.ts | 8 +++++ 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/components/settings/AppearanceSettings.tsx b/src/components/settings/AppearanceSettings.tsx index 1c0cbfd73..8f5bcdbc3 100644 --- a/src/components/settings/AppearanceSettings.tsx +++ b/src/components/settings/AppearanceSettings.tsx @@ -11,6 +11,7 @@ import { ipcRenderer, webFrame } from 'electron'; import { type FC, useContext, useEffect, useState } from 'react'; import { AppContext } from '../../context/App'; import { Size, Theme } from '../../types'; +import { hasMultipleAccounts } from '../../utils/auth/utils'; import { setTheme } from '../../utils/theme'; import { zoomLevelToPercentage, zoomPercentageToLevel } from '../../utils/zoom'; import { Button } from '../buttons/Button'; @@ -198,8 +199,8 @@ export const AppearanceSettings: FC = () => { 1} - disabled={auth.accounts.length > 1} + checked={settings.showAccountHostname || hasMultipleAccounts(auth)} + disabled={hasMultipleAccounts(auth)} onChange={(evt) => updateSetting('showAccountHostname', evt.target.checked) } diff --git a/src/context/App.tsx b/src/context/App.tsx index 67857be5e..084e302e9 100644 --- a/src/context/App.tsx +++ b/src/context/App.tsx @@ -33,6 +33,7 @@ import { authGitHub, getToken, getUserData, + hasAccounts, removeAccount, } from '../utils/auth/utils'; import { @@ -194,7 +195,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => { ); const isLoggedIn = useMemo(() => { - return auth.accounts.length > 0; + return hasAccounts(auth); }, [auth]); const loginWithGitHubApp = useCallback(async () => { diff --git a/src/utils/auth/utils.test.ts b/src/utils/auth/utils.test.ts index bb8f22642..9a6a4d832 100644 --- a/src/utils/auth/utils.test.ts +++ b/src/utils/auth/utils.test.ts @@ -338,4 +338,37 @@ describe('utils/auth/utils.ts', () => { expect(auth.isValidToken('1234567890asdfg' as Token)).toBeFalsy(); }); }); + + describe('hasAccounts', () => { + it('should return true', () => { + expect(auth.hasAccounts(mockAuth)).toBeTruthy(); + }); + + it('should validate false', () => { + expect( + auth.hasAccounts({ + accounts: [], + }), + ).toBeFalsy(); + }); + }); + + describe('hasMultipleAccounts', () => { + it('should return true', () => { + expect(auth.hasMultipleAccounts(mockAuth)).toBeTruthy(); + }); + + it('should validate false', () => { + expect( + auth.hasMultipleAccounts({ + accounts: [], + }), + ).toBeFalsy(); + expect( + auth.hasMultipleAccounts({ + accounts: [mockGitHubCloudAccount], + }), + ).toBeFalsy(); + }); + }); }); diff --git a/src/utils/auth/utils.ts b/src/utils/auth/utils.ts index b15b84557..d90a3ef71 100644 --- a/src/utils/auth/utils.ts +++ b/src/utils/auth/utils.ts @@ -218,3 +218,11 @@ export function isValidToken(token: Token) { export function getAccountUUID(account: Account): string { return btoa(`${account.hostname}-${account.user.id}-${account.method}`); } + +export function hasAccounts(auth: AuthState) { + return auth.accounts.length > 0; +} + +export function hasMultipleAccounts(auth: AuthState) { + return auth.accounts.length > 1; +}