Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(settings): disable show accounts when multiple accounts are authenticated #1439

Merged
merged 4 commits into from
Aug 16, 2024
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
10 changes: 8 additions & 2 deletions src/components/settings/AppearanceSettings.test.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -200,7 +204,9 @@ describe('routes/components/settings/AppearanceSettings.tsx', () => {
render(
<AppContext.Provider
value={{
auth: mockAuth,
auth: {
accounts: [mockGitHubAppAccount],
},
settings: mockSettings,
updateSetting,
}}
Expand Down
6 changes: 4 additions & 2 deletions src/components/settings/AppearanceSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -22,7 +23,7 @@ let timeout: NodeJS.Timeout;
const DELAY = 200;

export const AppearanceSettings: FC = () => {
const { settings, updateSetting } = useContext(AppContext);
const { auth, settings, updateSetting } = useContext(AppContext);
const [zoomPercentage, setZoomPercentage] = useState(
zoomLevelToPercentage(webFrame.getZoomLevel()),
);
Expand Down Expand Up @@ -198,7 +199,8 @@ export const AppearanceSettings: FC = () => {
<Checkbox
name="showAccountHeader"
label="Show account header"
checked={settings.showAccountHeader}
checked={settings.showAccountHeader || hasMultipleAccounts(auth)}
disabled={hasMultipleAccounts(auth)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why exactly do we need to disable this on multiple accounts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of this prop

showAccountHostname={
hasMultipleAccounts || settings.showAccountHostname

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, but can't the hostname be the same on some situations?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, we support repeated hostnames now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so single account, feature flag toggles showing the account header
multiple accounts, feature flag does nada

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but does it make sense for it to be disabled on the situations where it is the same for example?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

showAccountHostname is misleading nowdays. It really should be showAccountNameHeader or something like that as it represents the row containing account name, account avatar, account platform type, etc

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed in #1464

onChange={(evt) =>
updateSetting('showAccountHeader', evt.target.checked)
}
Expand Down
3 changes: 2 additions & 1 deletion src/context/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
addAccount,
authGitHub,
getToken,
hasAccounts,
refreshAccount,
removeAccount,
} from '../utils/auth/utils';
Expand Down Expand Up @@ -203,7 +204,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
);

const isLoggedIn = useMemo(() => {
return auth.accounts.length > 0;
return hasAccounts(auth);
}, [auth]);

const loginWithGitHubApp = useCallback(async () => {
Expand Down
3 changes: 3 additions & 0 deletions src/routes/__snapshots__/Settings.test.tsx.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions src/utils/auth/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,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();
});
});
});
8 changes: 8 additions & 0 deletions src/utils/auth/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😅

return auth.accounts.length > 0;
}

export function hasMultipleAccounts(auth: AuthState) {
return auth.accounts.length > 1;
}