Skip to content

Commit 0c60764

Browse files
feat(ui): confirm connected account removal from its row
1 parent 5ee24ff commit 0c60764

10 files changed

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

‎packages/swingset/src/stories/user-profile-connected-accounts-section.stories.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { UserProfileConnectedAccountsSectionView } from '@clerk/ui/mosaic/user-profile/user-profile-connected-accounts-section.view';
2+
import { useState } from 'react';
23

34
import type { StoryMeta } from '@/lib/types';
45

@@ -14,15 +15,16 @@ export const meta: StoryMeta = {
1415
};
1516

1617
export function Default() {
18+
const [connected, setConnected] = useState(true);
1719
return (
1820
<UserProfileConnectedAccountsSectionView
1921
accounts={[
2022
{
2123
id: 'google',
2224
provider: 'Google',
23-
identifier: '[email protected]',
25+
identifier: connected ? '[email protected]' : undefined,
2426
iconUrl: 'https://img.clerk.com/static/google.svg',
25-
connected: true,
27+
connected,
2628
},
2729
{
2830
id: 'apple',
@@ -32,7 +34,7 @@ export function Default() {
3234
},
3335
]}
3436
onConnect={() => undefined}
35-
onManage={() => undefined}
37+
onRemove={() => setConnected(false)}
3638
/>
3739
);
3840
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { render, screen, waitFor, within } from '@testing-library/react';
2+
import userEvent from '@testing-library/user-event';
3+
import { useState } from 'react';
4+
import { describe, expect, it, vi } from 'vitest';
5+
6+
import { MosaicProvider } from '../../MosaicProvider';
7+
import { UserProfileConnectedAccountsSectionView } from '../user-profile-connected-accounts-section.view';
8+
9+
const account = { id: 'github', provider: 'GitHub', identifier: 'test' };
10+
11+
function renderAccounts(onRemove: (id: string) => void | Promise<void>) {
12+
return render(
13+
<MosaicProvider>
14+
<UserProfileConnectedAccountsSectionView
15+
accounts={[account]}
16+
onRemove={onRemove}
17+
/>
18+
</MosaicProvider>,
19+
);
20+
}
21+
22+
async function openRemoval(user: ReturnType<typeof userEvent.setup>) {
23+
await user.click(screen.getByRole('button', { name: 'Manage GitHub' }));
24+
await user.click(screen.getByRole('menuitem', { name: 'Remove' }));
25+
}
26+
27+
describe('connected account removal', () => {
28+
it('returns keyboard focus to the menu when canceled', async () => {
29+
const user = userEvent.setup();
30+
const onRemove = vi.fn();
31+
renderAccounts(onRemove);
32+
const trigger = screen.getByRole('button', { name: 'Manage GitHub' });
33+
trigger.focus();
34+
await user.keyboard('{Enter}{Enter}');
35+
expect(screen.getByRole('alertdialog')).toBeInTheDocument();
36+
await user.keyboard('{Escape}');
37+
await waitFor(() => expect(trigger).toHaveFocus());
38+
expect(onRemove).not.toHaveBeenCalled();
39+
});
40+
41+
it('keeps focus in the section when a removed account disappears', async () => {
42+
const user = userEvent.setup();
43+
function Example() {
44+
const [accounts, setAccounts] = useState([account]);
45+
return (
46+
<MosaicProvider>
47+
<UserProfileConnectedAccountsSectionView
48+
accounts={accounts}
49+
onRemove={id => setAccounts(current => current.filter(item => item.id !== id))}
50+
/>
51+
</MosaicProvider>
52+
);
53+
}
54+
render(<Example />);
55+
await openRemoval(user);
56+
await user.click(within(screen.getByRole('alertdialog')).getByRole('button', { name: 'Remove' }));
57+
await waitFor(() => expect(screen.queryByRole('button', { name: 'Manage GitHub' })).not.toBeInTheDocument());
58+
await waitFor(() => expect(screen.getByRole('region', { name: 'Connected accounts' })).toHaveFocus());
59+
});
60+
61+
it('shows a removal failure and allows retrying', async () => {
62+
const user = userEvent.setup();
63+
const onRemove = vi.fn().mockRejectedValueOnce(new Error('Unable to disconnect')).mockResolvedValueOnce(undefined);
64+
renderAccounts(onRemove);
65+
await openRemoval(user);
66+
await user.click(within(screen.getByRole('alertdialog')).getByRole('button', { name: 'Remove' }));
67+
expect(await screen.findByRole('alert')).toHaveTextContent('Unable to disconnect');
68+
await openRemoval(user);
69+
await user.click(within(screen.getByRole('alertdialog')).getByRole('button', { name: 'Remove' }));
70+
expect(onRemove).toHaveBeenCalledTimes(2);
71+
expect(screen.queryByRole('alert')).not.toBeInTheDocument();
72+
});
73+
});

‎packages/ui/src/mosaic/user-profile/__tests__/user-profile-profile-panel.view.test.tsx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,12 @@ describe('UserProfileProfilePanelView', () => {
430430
const removeConnectedAccount = screen.getByRole('menuitem', { name: 'Remove' });
431431
expect(removeConnectedAccount).toHaveAttribute('data-color', 'negative');
432432
await user.click(removeConnectedAccount);
433+
expect(onRemoveConnectedAccount).not.toHaveBeenCalled();
434+
await user.click(
435+
within(screen.getByRole('alertdialog', { name: 'Remove connected account?' })).getByRole('button', {
436+
name: 'Remove',
437+
}),
438+
);
433439
expect(onRemoveConnectedAccount).toHaveBeenCalledWith('github');
434440
});
435441

‎packages/ui/src/mosaic/user-profile/user-profile-action-menu.tsx‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { ReactNode } from 'react';
2+
13
import { Icon } from '../components/icon';
24
import { Menu } from '../components/menu';
35
import type { IconName } from '../icons/registry';
@@ -9,7 +11,15 @@ export interface UserProfileMenuAction {
911
onClick: () => void;
1012
}
1113

12-
export function UserProfileActionMenu({ label, actions }: { label: string; actions: UserProfileMenuAction[] }) {
14+
export function UserProfileActionMenu({
15+
label,
16+
actions,
17+
children,
18+
}: {
19+
label: string;
20+
actions: UserProfileMenuAction[];
21+
children?: ReactNode;
22+
}) {
1323
if (actions.length === 0) {
1424
return null;
1525
}
@@ -34,6 +44,7 @@ export function UserProfileActionMenu({ label, actions }: { label: string; actio
3444
</Menu.Item>
3545
))}
3646
</Menu.Popup>
47+
{children}
3748
</Menu.Root>
3849
);
3950
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import type { RefObject } from 'react';
2+
import { useRef, useState } from 'react';
3+
4+
import { Button } from '../components/button';
5+
import { Icon } from '../components/icon';
6+
import { Section } from '../components/section';
7+
import type { UserProfileMenuAction } from './user-profile-action-menu';
8+
import { UserProfileActionMenu } from './user-profile-action-menu';
9+
import { userProfileConnectedAccountsMessages as m } from './user-profile-connected-accounts.messages';
10+
import type {
11+
UserProfileConnectedAccount,
12+
UserProfileConnectedAccountsSectionViewProps,
13+
} from './user-profile-connected-accounts-section.view';
14+
import { UserProfileProviderIcon } from './user-profile-provider-icon';
15+
import { UserProfileRemoveConnectedAccountDialog } from './user-profile-remove-connected-account.dialog';
16+
17+
interface UserProfileConnectedAccountRowViewProps extends Pick<
18+
UserProfileConnectedAccountsSectionViewProps,
19+
'onConnect' | 'onManage' | 'onRemove'
20+
> {
21+
account: UserProfileConnectedAccount;
22+
removalFocusRef: RefObject<HTMLElement | null>;
23+
}
24+
25+
export function UserProfileConnectedAccountRowView({
26+
account,
27+
onConnect,
28+
onManage,
29+
onRemove,
30+
removalFocusRef,
31+
}: UserProfileConnectedAccountRowViewProps) {
32+
const [open, setOpen] = useState(false);
33+
const [removeError, setRemoveError] = useState<string>();
34+
const removing = useRef(false);
35+
const confirmedRemoval = useRef(false);
36+
const connectRef = useRef<HTMLButtonElement>(null);
37+
const connected = account.connected ?? Boolean(account.identifier);
38+
const actions: UserProfileMenuAction[] = [];
39+
40+
const confirmRemove = async () => {
41+
if (!onRemove || account.canRemove === false || removing.current) {
42+
return;
43+
}
44+
removing.current = true;
45+
confirmedRemoval.current = true;
46+
setOpen(false);
47+
try {
48+
await onRemove(account.id);
49+
} catch (error) {
50+
setRemoveError(error instanceof Error ? error.message : m.removeError);
51+
} finally {
52+
removing.current = false;
53+
}
54+
};
55+
56+
if (onRemove && account.canRemove !== false) {
57+
actions.push({
58+
label: m.remove,
59+
color: 'negative',
60+
onClick: () => {
61+
if (removing.current) {
62+
return;
63+
}
64+
confirmedRemoval.current = false;
65+
setRemoveError(undefined);
66+
setOpen(true);
67+
},
68+
});
69+
} else if (!onRemove && onManage) {
70+
actions.push({ label: m.manage, onClick: () => onManage(account.id) });
71+
}
72+
73+
return (
74+
<Section.Row>
75+
<Section.Item>
76+
{account.iconUrl ? <UserProfileProviderIcon iconUrl={account.iconUrl} /> : null}
77+
<Section.Content>
78+
<Section.Label>{account.provider}</Section.Label>
79+
{account.identifier ? <Section.Description>{account.identifier}</Section.Description> : null}
80+
{removeError ? <Section.Error role='alert'>{removeError}</Section.Error> : null}
81+
</Section.Content>
82+
<Section.Actions>
83+
{connected ? (
84+
<UserProfileActionMenu
85+
actions={actions}
86+
label={`${m.manage} ${account.provider}`}
87+
>
88+
{onRemove && account.canRemove !== false ? (
89+
<UserProfileRemoveConnectedAccountDialog
90+
provider={account.provider}
91+
open={open}
92+
onOpenChange={setOpen}
93+
onConfirm={() => void confirmRemove()}
94+
finalFocus={() =>
95+
confirmedRemoval.current ? (connectRef.current ?? removalFocusRef.current) : undefined
96+
}
97+
/>
98+
) : null}
99+
</UserProfileActionMenu>
100+
) : null}
101+
{!connected && onConnect ? (
102+
<Button
103+
ref={connectRef}
104+
color='neutral'
105+
size='sm'
106+
variant='outline'
107+
onClick={() => onConnect(account.id)}
108+
>
109+
{m.connect}
110+
<Icon
111+
name='arrow-right-top'
112+
placement='inline-end'
113+
size='sm'
114+
/>
115+
</Button>
116+
) : null}
117+
</Section.Actions>
118+
</Section.Item>
119+
</Section.Row>
120+
);
121+
}

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

Lines changed: 21 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { Button } from '../components/button';
2-
import { Icon } from '../components/icon';
1+
import { useRef } from 'react';
2+
33
import { Section } from '../components/section';
4-
import type { UserProfileMenuAction } from './user-profile-action-menu';
5-
import { UserProfileActionMenu } from './user-profile-action-menu';
6-
import { UserProfileProviderIcon } from './user-profile-provider-icon';
4+
import { UserProfileConnectedAccountRowView } from './user-profile-connected-account-row.view';
5+
import { userProfileConnectedAccountsMessages as m } from './user-profile-connected-accounts.messages';
76

87
export interface UserProfileConnectedAccount {
98
id: string;
@@ -18,7 +17,7 @@ export interface UserProfileConnectedAccountsSectionViewProps {
1817
accounts: UserProfileConnectedAccount[];
1918
onConnect?: (id: string) => void;
2019
onManage?: (id: string) => void;
21-
onRemove?: (id: string) => void;
20+
onRemove?: (id: string) => void | Promise<void>;
2221
}
2322

2423
export function UserProfileConnectedAccountsSectionView({
@@ -27,54 +26,24 @@ export function UserProfileConnectedAccountsSectionView({
2726
onManage,
2827
onRemove,
2928
}: UserProfileConnectedAccountsSectionViewProps) {
29+
const sectionRef = useRef<HTMLElement>(null);
3030
return (
31-
<Section.Root>
32-
<Section.Title>Connected accounts</Section.Title>
31+
<Section.Root
32+
ref={sectionRef}
33+
tabIndex={-1}
34+
>
35+
<Section.Title>{m.title}</Section.Title>
3336
<Section.Group>
34-
{accounts.map(account => {
35-
const connected = account.connected ?? Boolean(account.identifier);
36-
const actions: UserProfileMenuAction[] = [];
37-
if (onRemove && account.canRemove !== false) {
38-
actions.push({ label: 'Remove', color: 'negative', onClick: () => onRemove(account.id) });
39-
} else if (!onRemove && onManage) {
40-
actions.push({ label: 'Manage', onClick: () => onManage(account.id) });
41-
}
42-
43-
return (
44-
<Section.Row key={account.id}>
45-
<Section.Item>
46-
{account.iconUrl ? <UserProfileProviderIcon iconUrl={account.iconUrl} /> : null}
47-
<Section.Content>
48-
<Section.Label>{account.provider}</Section.Label>
49-
{account.identifier ? <Section.Description>{account.identifier}</Section.Description> : null}
50-
</Section.Content>
51-
<Section.Actions>
52-
{connected ? (
53-
<UserProfileActionMenu
54-
actions={actions}
55-
label={`Manage ${account.provider}`}
56-
/>
57-
) : null}
58-
{!connected && onConnect ? (
59-
<Button
60-
color='neutral'
61-
size='sm'
62-
variant='outline'
63-
onClick={() => onConnect(account.id)}
64-
>
65-
Connect
66-
<Icon
67-
name='arrow-right-top'
68-
placement='inline-end'
69-
size='sm'
70-
/>
71-
</Button>
72-
) : null}
73-
</Section.Actions>
74-
</Section.Item>
75-
</Section.Row>
76-
);
77-
})}
37+
{accounts.map(account => (
38+
<UserProfileConnectedAccountRowView
39+
key={account.id}
40+
account={account}
41+
onConnect={onConnect}
42+
onManage={onManage}
43+
onRemove={onRemove}
44+
removalFocusRef={sectionRef}
45+
/>
46+
))}
7847
</Section.Group>
7948
</Section.Root>
8049
);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const userProfileConnectedAccountsMessages = {
2+
title: 'Connected accounts',
3+
connect: 'Connect',
4+
manage: 'Manage',
5+
remove: 'Remove',
6+
removeError: 'Unable to remove connected account. Please try again.',
7+
removeDialog: {
8+
title: 'Remove connected account?',
9+
description: 'Are you sure you want to remove your {provider} account?',
10+
confirm: 'Remove',
11+
cancel: 'Cancel',
12+
},
13+
};

‎packages/ui/src/mosaic/user-profile/user-profile-profile-panel.view.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface UserProfileProfilePanelViewProps extends UserProfileAccountSect
2828
web3Wallets?: UserProfileWeb3Wallet[];
2929
onConnectAccount?: (id: string) => void;
3030
onManageConnectedAccount?: (id: string) => void;
31-
onRemoveConnectedAccount?: (id: string) => void;
31+
onRemoveConnectedAccount?: (id: string) => void | Promise<void>;
3232
onConnectWeb3Wallet?: (id: string) => void;
3333
onManageWeb3Wallet?: (id: string) => void;
3434
onSetPrimaryWeb3Wallet?: (id: string) => void;

0 commit comments

Comments
 (0)