diff --git a/src/renderer/src/components/settings/accounts/account-rename-runner.test.ts b/src/renderer/src/components/settings/accounts/account-rename-runner.test.ts new file mode 100644 index 00000000000..b4722ec0728 --- /dev/null +++ b/src/renderer/src/components/settings/accounts/account-rename-runner.test.ts @@ -0,0 +1,32 @@ +import { describe, expect, it } from 'vitest' +import { withRenamedAccount } from './account-rename-runner' + +describe('withRenamedAccount', () => { + it('applies a late rename reply without undoing a newer selection or removal', () => { + // The rename reply was built while A and B existed and B was selected. + const reply = { + activeAccountId: 'b', + accounts: [ + { id: 'a', email: 'a@example.com', displayName: 'Work' }, + { id: 'b', email: 'b@example.com' } + ] + } + // Meanwhile C was selected and B removed. + const current = { + activeAccountId: 'c', + accounts: [ + { id: 'a', email: 'a@example.com' }, + { id: 'c', email: 'c@example.com' } + ] + } + expect(withRenamedAccount(current, reply, 'a')).toEqual({ + activeAccountId: 'c', + accounts: [ + { id: 'a', email: 'a@example.com', displayName: 'Work' }, + { id: 'c', email: 'c@example.com' } + ] + }) + // A rename of an account removed meanwhile does not bring it back. + expect(withRenamedAccount(current, reply, 'b')).toBe(current) + }) +}) diff --git a/src/renderer/src/components/settings/accounts/account-rename-runner.ts b/src/renderer/src/components/settings/accounts/account-rename-runner.ts index 6b70c788242..1d6f833a8e9 100644 --- a/src/renderer/src/components/settings/accounts/account-rename-runner.ts +++ b/src/renderer/src/components/settings/accounts/account-rename-runner.ts @@ -20,6 +20,29 @@ export function renamingAccountKey(target: Pick( + current: State, + reply: { accounts: readonly { id: string; displayName?: string }[] }, + accountId: string +): State { + const displayName = reply.accounts.find((account) => account.id === accountId)?.displayName + if (!current.accounts.some((account) => account.id === accountId)) { + return current + } + return { + ...current, + accounts: current.accounts.map((account) => { + if (account.id !== accountId) { + return account + } + const { displayName: _previous, ...rest } = account + return displayName ? { ...rest, displayName } : rest + }) + } +} + type AccountRenameContext = { accountOwner: RuntimeClientTarget hostLabel: string @@ -51,21 +74,24 @@ export function createAccountRenameRunner( const rename = { accountId: target.id, displayName } setPending(key, true) try { - if (target.provider === 'claude') { - const next = await renameClaudeProviderAccount(accountOwner, rename, hostLabel) - if (isCurrentAccountOwner()) { - context.setClaudeAccounts(next) - } - } else { - const next = await renameCodexProviderAccount(accountOwner, rename, hostLabel) - if (isCurrentAccountOwner()) { - context.setCodexAccounts(next) - } - } - // Local names live in this desktop's settings, which the status bar reads. + const reply = + target.provider === 'claude' + ? await renameClaudeProviderAccount(accountOwner, rename, hostLabel) + : await renameCodexProviderAccount(accountOwner, rename, hostLabel) + // Local names live in this desktop's settings; read them first so the pane's + // settings overlay already holds the new name when the row updates. if (!context.isRemoteAccountScope) { await context.fetchSettings() } + // Apply only this account's name: a select or remove may have answered after the + // host built this reply, and its whole roster would undo that newer state. + if (isCurrentAccountOwner()) { + if (target.provider === 'claude') { + context.setClaudeAccounts((current) => withRenamedAccount(current, reply, target.id)) + } else { + context.setCodexAccounts((current) => withRenamedAccount(current, reply, target.id)) + } + } } catch (error) { if (target.provider === 'claude') { toast.error(