Skip to content

Commit 9eb07ce

Browse files
committed
fix add account error
Signed-off-by: Adam Setch <[email protected]>
1 parent 9cd2cf9 commit 9eb07ce

File tree

4 files changed

+52
-44
lines changed

4 files changed

+52
-44
lines changed

src/renderer/components/settings/AppearanceSettings.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ export const AppearanceSettings: FC = () => {
260260
<Checkbox
261261
name="showAccountHeader"
262262
label="Show account header"
263-
checked={settings.showAccountHeader || hasMultipleAccounts(auth)}
264-
disabled={hasMultipleAccounts(auth)}
263+
checked={settings.showAccountHeader}
264+
visible={!hasMultipleAccounts(auth)}
265265
onChange={(evt) =>
266266
updateSetting('showAccountHeader', evt.target.checked)
267267
}

src/renderer/routes/Accounts.tsx

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ export const AccountsRoute: FC = () => {
4949
useContext(AppContext);
5050
const navigate = useNavigate();
5151

52+
const [loadingStates, setLoadingStates] = useState<Record<string, boolean>>(
53+
{},
54+
);
55+
5256
const logoutAccount = useCallback(
5357
(account: Account) => {
5458
logoutFromAccount(account);
@@ -65,6 +69,29 @@ export const AccountsRoute: FC = () => {
6569
navigate('/accounts', { replace: true });
6670
}, []);
6771

72+
const handleRefresh = useCallback(async (account: Account) => {
73+
const accountUUID = getAccountUUID(account);
74+
75+
setLoadingStates((prev) => ({
76+
...prev,
77+
[accountUUID]: true,
78+
}));
79+
80+
await refreshAccount(account);
81+
navigate('/accounts', { replace: true });
82+
83+
/**
84+
* Typically the above refresh API call completes very quickly,
85+
* so we add an brief artificial delay to allow the icon to spin a few times
86+
*/
87+
setTimeout(() => {
88+
setLoadingStates((prev) => ({
89+
...prev,
90+
[accountUUID]: false,
91+
}));
92+
}, 500);
93+
}, []);
94+
6895
const loginWithGitHub = useCallback(async () => {
6996
try {
7097
await loginWithGitHubApp();
@@ -89,11 +116,11 @@ export const AccountsRoute: FC = () => {
89116
{auth.accounts.map((account, i) => {
90117
const AuthMethodIcon = getAuthMethodIcon(account.method);
91118
const PlatformIcon = getPlatformIcon(account.platform);
92-
const [isRefreshingAccount, setIsRefreshingAccount] = useState(false);
119+
const accountUUID = getAccountUUID(account);
93120

94121
return (
95122
<Box
96-
key={getAccountUUID(account)}
123+
key={accountUUID}
97124
className="rounded-md p-2 mb-4 bg-gitify-accounts"
98125
>
99126
<Stack
@@ -191,22 +218,9 @@ export const AccountsRoute: FC = () => {
191218
<IconButton
192219
icon={SyncIcon}
193220
aria-label={`Refresh ${account.user.login}`}
194-
onClick={async () => {
195-
setIsRefreshingAccount(true);
196-
197-
await refreshAccount(account);
198-
navigate('/accounts', { replace: true });
199-
200-
/**
201-
* Typically the above refresh API call completes very quickly,
202-
* so we add an brief artificial delay to allow the icon to spin a few times
203-
*/
204-
setTimeout(() => {
205-
setIsRefreshingAccount(false);
206-
}, 500);
207-
}}
221+
onClick={() => handleRefresh(account)}
208222
size="small"
209-
loading={isRefreshingAccount}
223+
loading={loadingStates[accountUUID] || false}
210224
data-testid="account-refresh"
211225
/>
212226

src/renderer/routes/__snapshots__/Settings.test.tsx.snap

Lines changed: 0 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/renderer/utils/auth/utils.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ export async function addAccount(
108108
token: Token,
109109
hostname: Hostname,
110110
): Promise<AuthState> {
111+
const accountList = auth.accounts;
112+
111113
let newAccount = {
112114
hostname: hostname,
113115
method: method,
@@ -116,9 +118,23 @@ export async function addAccount(
116118
} as Account;
117119

118120
newAccount = await refreshAccount(newAccount);
121+
const newAccountUUID = getAccountUUID(newAccount);
122+
123+
const existingAccount = accountList.find(
124+
(a) => getAccountUUID(a) === newAccountUUID,
125+
);
126+
127+
if (existingAccount) {
128+
logWarn(
129+
'addAccount',
130+
`account for user ${newAccount.user.login} already exists`,
131+
);
132+
} else {
133+
accountList.push(newAccount);
134+
}
119135

120136
return {
121-
accounts: [...auth.accounts, newAccount],
137+
accounts: accountList,
122138
};
123139
}
124140

@@ -136,6 +152,8 @@ export async function refreshAccount(account: Account): Promise<Account> {
136152
try {
137153
const res = await getAuthenticatedUser(account.hostname, account.token);
138154

155+
// console.log('ADAM RESPONSE', JSON.stringify(res, null, 2));
156+
139157
// Refresh user data
140158
account.user = {
141159
id: res.data.id,

0 commit comments

Comments
 (0)