Skip to content

Commit 000cc8b

Browse files
fix(mosaic): let account section controllers own row pending state
Connected accounts now disables account menus while a connection starts, so removal can no longer begin mid-connect, and removal shares the section lock. Enterprise accounts moves onto the shared pending action, so a connect error clears when any connection starts.
1 parent 9c1c63c commit 000cc8b

11 files changed

Lines changed: 63 additions & 66 deletions

‎.changeset/goofy-parents-trade.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

‎packages/mosaic/src/features/user-profile/user-profile-connected-accounts-section/__tests__/user-profile-connected-accounts.connect.feature.test.tsx‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,27 @@ describe('connected accounts', () => {
158158
request.fail();
159159
});
160160

161+
it('holds connected account menus while a connection starts', async () => {
162+
await renderSection([google]);
163+
const request = holdRequests('post', '/v1/me/external_accounts');
164+
const user = userEvent.setup();
165+
166+
try {
167+
await user.click(screen.getByRole('button', { name: 'Connect GitHub' }));
168+
await waitFor(() => expect(request.requests).toHaveLength(1));
169+
const manage = screen.getByRole('button', { name: 'Manage Google' });
170+
expect(manage).toHaveAttribute('aria-disabled', 'true');
171+
await user.click(manage);
172+
expect(screen.queryByRole('menu')).toBeNull();
173+
} finally {
174+
request.fail();
175+
}
176+
177+
await waitFor(() =>
178+
expect(screen.getByRole('button', { name: 'Manage Google' })).not.toHaveAttribute('aria-disabled', 'true'),
179+
);
180+
});
181+
161182
it('verifies the session before connecting a provider, then continues to it', async () => {
162183
serveFapi(signedIn([], { verification: { secrets: { password: 'hunter2' } } }));
163184
const view = await renderWithClerk(<UserProfileConnectedAccountsSection />);

‎packages/mosaic/src/features/user-profile/user-profile-connected-accounts-section/user-profile-connected-account-row.view.tsx‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ import type {
1919

2020
export function UserProfileConnectProviderRowView({
2121
provider,
22-
isPending,
2322
isDisabled,
2423
onConnect,
2524
triggerRef,
2625
}: {
2726
provider: UserProfileConnectionProvider;
28-
isPending: boolean;
2927
isDisabled: boolean;
3028
onConnect: (id: string) => void;
3129
triggerRef?: Ref<HTMLButtonElement>;
@@ -58,8 +56,8 @@ export function UserProfileConnectProviderRowView({
5856
variant='outline'
5957
color='neutral'
6058
aria-label={fill(m.connectLabel, { provider: provider.provider })}
61-
isPending={isPending}
62-
disabled={isDisabled && !isPending}
59+
isPending={provider.isPending}
60+
disabled={isDisabled && !provider.isPending}
6361
onClick={() => onConnect(provider.id)}
6462
>
6563
{m.connect}
@@ -91,7 +89,7 @@ export function UserProfileConnectedAccountRowView({
9189
}) {
9290
const m = useMessages('userProfileConnectedAccounts');
9391
const actions: ActionMenuAction[] = [];
94-
if ((account.status === 'reconnect' || account.status === 'error') && onReconnect && !isDisabled) {
92+
if ((account.status === 'reconnect' || account.status === 'error') && onReconnect) {
9593
actions.push({
9694
label: account.status === 'reconnect' ? m.reconnect : m.tryAgain,
9795
onClick: () => onReconnect(account.id),
@@ -131,6 +129,8 @@ export function UserProfileConnectedAccountRowView({
131129
{actions.length > 0 ? (
132130
<Section.Actions>
133131
<ActionMenu
132+
disabled={isDisabled}
133+
focusableWhenDisabled
134134
triggerRef={triggerRef}
135135
label={fill(m.manageLabel, { provider: account.provider })}
136136
actions={actions}

‎packages/mosaic/src/features/user-profile/user-profile-connected-accounts-section/user-profile-connected-accounts-section.controller.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface UserProfileConnectedAccountsControllerOptions {
2929
export interface UserProfileConnectedAccountsController {
3030
accounts: UserProfileConnectedAccount[];
3131
availableProviders: UserProfileConnectionProvider[];
32-
pendingId: string | undefined;
32+
isDisabled: boolean;
3333
onConnect: (id: string) => void;
3434
onReconnect: (id: string) => void;
3535
sectionRef: Ref<HTMLDivElement>;
@@ -82,15 +82,17 @@ export function useUserProfileConnectedAccountsController({
8282
accounts: accounts.map(account =>
8383
actions.errorKey === account.id ? { ...account, reconnectError: actions.error } : account,
8484
),
85-
availableProviders: availableProviders.map(provider =>
86-
actions.errorKey === provider.id ? { ...provider, connectError: actions.error } : provider,
87-
),
88-
pendingId: actions.pendingKey,
85+
availableProviders: availableProviders.map(provider => ({
86+
...provider,
87+
isPending: actions.pendingKey === provider.id,
88+
connectError: actions.errorKey === provider.id ? actions.error : undefined,
89+
})),
90+
isDisabled: actions.isPending,
8991
onConnect: id => run(id, onConnect),
9092
onReconnect: id => run(id, onReconnect),
9193
sectionRef,
9294
registerTrigger: removalFocus.registerTrigger,
93-
remove: onRemove ? remove : undefined,
95+
remove: onRemove ? actions.guard(remove) : undefined,
9496
finalFocus: removalFocus.finalFocus,
9597
reconnectReverification,
9698
reconnectFinalFocus: () => (actions.lastKey ? removalFocus.trigger(actions.lastKey) : null),

‎packages/mosaic/src/features/user-profile/user-profile-connected-accounts-section/user-profile-connected-accounts-section.types.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface ConnectedAccountProviderDisplay {
1616
export interface UserProfileConnectionProvider extends ConnectedAccountProviderDisplay {
1717
id: string;
1818
connectError?: string;
19+
isPending?: boolean;
1920
}
2021

2122
export interface UserProfileConnectedAccount extends ConnectedAccountProviderDisplay {
@@ -30,7 +31,7 @@ export interface UserProfileConnectedAccountsSectionViewProps {
3031
accounts: UserProfileConnectedAccount[];
3132
sectionRef?: Ref<HTMLDivElement>;
3233
availableProviders?: UserProfileConnectionProvider[];
33-
pendingId?: string;
34+
isDisabled?: boolean;
3435
onConnect?: (id: string) => void;
3536
onReconnect?: (id: string) => void;
3637
registerTrigger: (id: string) => Ref<HTMLButtonElement>;

‎packages/mosaic/src/features/user-profile/user-profile-connected-accounts-section/user-profile-connected-accounts-section.view.tsx‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function UserProfileConnectedAccountsSectionView({
1818
accounts,
1919
sectionRef,
2020
availableProviders = [],
21-
pendingId,
21+
isDisabled = false,
2222
onConnect,
2323
onReconnect,
2424
registerTrigger,
@@ -31,7 +31,6 @@ export function UserProfileConnectedAccountsSectionView({
3131
}: UserProfileConnectedAccountsSectionViewProps) {
3232
const m = useMessages('userProfileConnectedAccounts');
3333
const hasRows = accounts.length > 0 || (availableProviders.length > 0 && Boolean(onConnect));
34-
const isBusy = pendingId !== undefined;
3534

3635
return (
3736
<>
@@ -52,7 +51,7 @@ export function UserProfileConnectedAccountsSectionView({
5251
key={account.id}
5352
account={account}
5453
triggerRef={registerTrigger(account.id)}
55-
isDisabled={isBusy}
54+
isDisabled={isDisabled}
5655
onReconnect={onReconnect}
5756
onRemove={remove ? account => remove.open(account) : undefined}
5857
/>
@@ -63,8 +62,7 @@ export function UserProfileConnectedAccountsSectionView({
6362
key={provider.id}
6463
provider={provider}
6564
triggerRef={registerTrigger(provider.id)}
66-
isPending={pendingId === provider.id}
67-
isDisabled={isBusy}
65+
isDisabled={isDisabled}
6866
onConnect={onConnect}
6967
/>
7068
))

‎packages/mosaic/src/features/user-profile/user-profile-enterprise-accounts-section/user-profile-enterprise-account-row.view.tsx‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ import type { UserProfileEnterpriseAccount } from './user-profile-enterprise-acc
1414
export function UserProfileEnterpriseAccountRowView({
1515
account,
1616
onConnect,
17-
isPending,
1817
disabled,
1918
}: {
2019
account: UserProfileEnterpriseAccount;
2120
onConnect?: (id: string) => void;
22-
isPending?: boolean;
2321
disabled?: boolean;
2422
}) {
2523
const m = useMessages('userProfileEnterpriseAccountsSection');
@@ -58,7 +56,7 @@ export function UserProfileEnterpriseAccountRowView({
5856
size='sm'
5957
variant='outline'
6058
aria-label={fill(m.connectProvider, { provider: account.name })}
61-
isPending={isPending}
59+
isPending={account.isPending}
6260
pendingLabel={m.connecting}
6361
disabled={disabled}
6462
onClick={() => onConnect(account.id)}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import { isReverificationCancelledError } from '@clerk/shared/error';
2-
import { useRef, useState } from 'react';
3-
4-
import type { ErrorDescription } from '../../../localization';
5-
import { useErrorText } from '../../../localization';
6-
import { toLocalizableError } from '../../../utils/errors';
1+
import { usePendingAction } from '../../../hooks/use-pending-action';
72
import type { ReverificationController } from '../../reverification';
83
import type {
94
EnterpriseAccountActionResult,
@@ -22,7 +17,7 @@ export interface UserProfileEnterpriseAccountsControllerOptions {
2217
export interface UserProfileEnterpriseAccountsController {
2318
accounts: UserProfileEnterpriseAccount[];
2419
connections: UserProfileEnterpriseConnection[];
25-
pendingId: string | undefined;
20+
isDisabled: boolean;
2621
onConnect: (connectionId: string) => void;
2722
connectReverification: ReverificationController;
2823
}
@@ -34,42 +29,22 @@ export function useUserProfileEnterpriseAccountsController({
3429
connectReverification,
3530
errorFallback,
3631
}: UserProfileEnterpriseAccountsControllerOptions): UserProfileEnterpriseAccountsController {
37-
const [pendingId, setPendingId] = useState<string>();
38-
const errorText = useErrorText();
39-
const [connectErrors, setConnectErrors] = useState<Record<string, ErrorDescription>>({});
40-
const connecting = useRef(false);
41-
42-
const run = async (id: string) => {
43-
if (connecting.current) {
44-
return;
45-
}
46-
connecting.current = true;
47-
setPendingId(id);
48-
setConnectErrors(({ [id]: _cleared, ...rest }) => rest);
49-
50-
try {
51-
if ((await onConnect(id)) === 'redirecting') {
52-
await new Promise(resolve => setTimeout(resolve, 2000));
53-
}
54-
} catch (error) {
55-
if (isReverificationCancelledError(error)) {
56-
return;
57-
}
58-
setConnectErrors(current => ({ ...current, [id]: toLocalizableError(error) }));
59-
} finally {
60-
connecting.current = false;
61-
setPendingId(undefined);
62-
}
63-
};
32+
const actions = usePendingAction({ errorFallback });
6433

6534
return {
6635
accounts,
67-
connections: connections.map(connection => {
68-
const error = connectErrors[connection.id];
69-
return error ? { ...connection, connectError: errorText(error, errorFallback) } : connection;
70-
}),
71-
pendingId,
72-
onConnect: id => void run(id),
36+
connections: connections.map(connection => ({
37+
...connection,
38+
isPending: actions.pendingKey === connection.id,
39+
connectError: actions.errorKey === connection.id ? actions.error : undefined,
40+
})),
41+
isDisabled: actions.isPending,
42+
onConnect: id =>
43+
void actions.run(id, async () => {
44+
if ((await onConnect(id)) === 'redirecting') {
45+
await new Promise(resolve => setTimeout(resolve, 2000));
46+
}
47+
}),
7348
connectReverification,
7449
};
7550
}

‎packages/mosaic/src/features/user-profile/user-profile-enterprise-accounts-section/user-profile-enterprise-accounts-section.types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface UserProfileEnterpriseConnection {
44
iconUrl?: string;
55
monochromeIcon?: boolean;
66
connectError?: string;
7+
isPending?: boolean;
78
}
89

910
export interface UserProfileEnterpriseAccount extends UserProfileEnterpriseConnection {

‎packages/mosaic/src/features/user-profile/user-profile-enterprise-accounts-section/user-profile-enterprise-accounts-section.view.tsx‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import type {
1111
export interface UserProfileEnterpriseAccountsSectionViewProps {
1212
accounts: UserProfileEnterpriseAccount[];
1313
connections?: UserProfileEnterpriseConnection[];
14-
pendingId?: string;
14+
isDisabled?: boolean;
1515
onConnect?: (id: string) => void;
1616
connectReverification?: ReverificationController;
1717
}
1818

1919
export function UserProfileEnterpriseAccountsSectionView({
2020
accounts,
2121
connections = [],
22-
pendingId,
22+
isDisabled = false,
2323
onConnect,
2424
connectReverification,
2525
}: UserProfileEnterpriseAccountsSectionViewProps) {
@@ -49,8 +49,7 @@ export function UserProfileEnterpriseAccountsSectionView({
4949
key={connection.id}
5050
account={connection}
5151
onConnect={onConnect}
52-
isPending={pendingId === connection.id}
53-
disabled={pendingId !== undefined}
52+
disabled={isDisabled}
5453
/>
5554
))
5655
: null}

0 commit comments

Comments
 (0)