Skip to content

Commit b2115f5

Browse files
refactor(ui): Use shared confirmations in user profile (#9779)
1 parent ae66de9 commit b2115f5

45 files changed

Lines changed: 941 additions & 939 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

‎.changeset/honest-chairs-cheat.md‎

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

‎packages/swingset/src/lib/registry.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,14 @@ import {
207207
Default as UserProfileAccountSectionDefault,
208208
EmailLinkResendFails as UserProfileAccountSectionEmailLinkResendFails,
209209
EmailLinkVerification as UserProfileAccountSectionEmailLinkVerification,
210+
EmailRemovalError as UserProfileAccountSectionEmailRemovalError,
211+
EmailRemovalPending as UserProfileAccountSectionEmailRemovalPending,
210212
EmailSsoConnectFails as UserProfileAccountSectionEmailSsoConnectFails,
211213
EmailSsoVerification as UserProfileAccountSectionEmailSsoVerification,
212214
meta as userProfileAccountSectionMeta,
213215
MultipleAccounts as UserProfileAccountSectionMultipleAccounts,
216+
PhoneRemovalError as UserProfileAccountSectionPhoneRemovalError,
217+
PhoneRemovalPending as UserProfileAccountSectionPhoneRemovalPending,
214218
} from '../stories/user-profile-account-section.stories';
215219
import {
216220
Default as UserProfileActiveDevicesSectionDefault,
@@ -529,6 +533,10 @@ const userProfileAccountSectionModule: StoryModule = {
529533
EmailLinkResendFails: UserProfileAccountSectionEmailLinkResendFails,
530534
EmailSsoVerification: UserProfileAccountSectionEmailSsoVerification,
531535
EmailSsoConnectFails: UserProfileAccountSectionEmailSsoConnectFails,
536+
EmailRemovalPending: UserProfileAccountSectionEmailRemovalPending,
537+
EmailRemovalError: UserProfileAccountSectionEmailRemovalError,
538+
PhoneRemovalPending: UserProfileAccountSectionPhoneRemovalPending,
539+
PhoneRemovalError: UserProfileAccountSectionPhoneRemovalError,
532540
};
533541
const userProfileProfilePanelModule: StoryModule = {
534542
meta: userProfileProfilePanelMeta,

‎packages/swingset/src/stories/fixtures/user-profile-connected-accounts.ts‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export function useConnectedAccountsFixture({
2424
providers?: UserProfileConnectionProvider[];
2525
removalState?: 'pending' | 'error';
2626
} = {}) {
27+
const [hasRemovalFailed, setHasRemovalFailed] = useState(false);
2728
const [accounts, setAccounts] = useState(initialAccounts);
2829
const availableProviders = providers.filter(provider => !accounts.some(account => account.id === provider.id));
2930

@@ -47,18 +48,15 @@ export function useConnectedAccountsFixture({
4748
: account,
4849
),
4950
),
50-
onRemove: (id: string) =>
51-
setAccounts(current => {
52-
const account = current.find(item => item.id === id);
53-
if (removalState === 'pending') {
54-
return current.map(item => (item.id === id ? { ...item, isRemoving: true } : item));
55-
}
56-
if (removalState === 'error' && account && !account.removalError) {
57-
return current.map(item =>
58-
item.id === id ? { ...item, removalError: 'Unable to remove this account. Please try again.' } : item,
59-
);
60-
}
61-
return current.filter(item => item.id !== id);
62-
}),
51+
onRemove: async (id: string) => {
52+
if (removalState === 'pending') {
53+
await new Promise(resolve => setTimeout(resolve, 1500));
54+
}
55+
if (removalState === 'error' && !hasRemovalFailed) {
56+
setHasRemovalFailed(true);
57+
throw new Error('Unable to remove this account. Please try again.');
58+
}
59+
setAccounts(current => current.filter(item => item.id !== id));
60+
},
6361
};
6462
}

‎packages/swingset/src/stories/fixtures/user-profile-web3-wallets.ts‎

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export function useWeb3WalletsFixture({
4545
} = {}) {
4646
const [wallets, setWallets] = useState(initialWallets);
4747
const [connectionProviders, setConnectionProviders] = useState(availableProviders);
48+
const [removalFailed, setRemovalFailed] = useState(false);
4849
const [primaryFailed, setPrimaryFailed] = useState(false);
4950

5051
return {
@@ -94,22 +95,15 @@ export function useWeb3WalletsFixture({
9495
current.map(wallet => ({ ...wallet, isPrimary: wallet.id === id, primaryError: undefined })),
9596
);
9697
},
97-
onRemove: (id: string) => {
98+
onRemove: async (id: string) => {
9899
if (removalState === 'pending') {
99-
setWallets(current => current.map(wallet => (wallet.id === id ? { ...wallet, isRemoving: true } : wallet)));
100-
setTimeout(() => {
101-
setWallets(current => current.filter(wallet => wallet.id !== id));
102-
}, 1500);
103-
return;
100+
await new Promise(resolve => setTimeout(resolve, 1500));
104101
}
105-
setWallets(current => {
106-
if (removalState === 'error' && !current.find(wallet => wallet.id === id)?.removalError) {
107-
return current.map(wallet =>
108-
wallet.id === id ? { ...wallet, removalError: 'Unable to remove wallet. Please try again.' } : wallet,
109-
);
110-
}
111-
return current.filter(wallet => wallet.id !== id);
112-
});
102+
if (removalState === 'error' && !removalFailed) {
103+
setRemovalFailed(true);
104+
throw new Error('Unable to remove wallet. Please try again.');
105+
}
106+
setWallets(current => current.filter(wallet => wallet.id !== id));
113107
},
114108
};
115109
}

‎packages/swingset/src/stories/fixtures/user-profile.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { useConnectedAccountsFixture } from './user-profile-connected-accounts';
2222
import { useUserProfileEditNameFixture } from './user-profile-edit-name';
2323
import { useUserProfileEditPasswordFixture } from './user-profile-edit-password';
2424
import { useUserProfileEditUsernameFixture } from './user-profile-edit-username';
25+
import { useWeb3WalletsFixture } from './user-profile-web3-wallets';
2526

2627
export interface UserProfileFixtureOptions {
2728
/** Replaces the default OTP flow, e.g. for a custom dialog example. */
@@ -52,6 +53,7 @@ const initialAPIKeys: UserProfileAPIKey[] = [
5253
*/
5354
export function useUserProfileFixture({ onAddEmail }: UserProfileFixtureOptions = {}) {
5455
const connections = useConnectedAccountsFixture();
56+
const wallets = useWeb3WalletsFixture();
5557
const editName = useUserProfileEditNameFixture();
5658
const editUsername = useUserProfileEditUsernameFixture();
5759
const editPassword = useUserProfileEditPasswordFixture();
@@ -132,6 +134,11 @@ export function useUserProfileFixture({ onAddEmail }: UserProfileFixtureOptions
132134
onConnectAccount: connections.onConnect,
133135
onReconnectAccount: connections.onReconnect,
134136
onRemoveConnectedAccount: connections.onRemove,
137+
web3Wallets: wallets.wallets,
138+
availableWeb3Providers: wallets.availableProviders,
139+
onConnectWeb3Wallet: wallets.onConnect,
140+
onSetPrimaryWeb3Wallet: wallets.onSetPrimary,
141+
onRemoveWeb3Wallet: wallets.onRemove,
135142
allowMultipleAccounts: true,
136143
hasImage: Boolean(imageUrl),
137144
imageUrl,

‎packages/swingset/src/stories/user-profile-account-section.mdx‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,27 @@ simulates completion and closes the dialog.
9999
Connect to see the supplied error message. The user can retry or cancel.
100100

101101
<Story name='EmailSsoConnectFails' storyModule={Stories} />
102+
103+
## Email removal pending
104+
105+
Confirm removal to see a short pending state before the email disappears.
106+
107+
<Story name='EmailRemovalPending' storyModule={Stories} />
108+
109+
## Email removal error
110+
111+
The first attempt fails inside the confirmation. Retry succeeds.
112+
113+
<Story name='EmailRemovalError' storyModule={Stories} />
114+
115+
## Phone removal pending
116+
117+
Remove a phone number to see the confirmation remain open until removal completes.
118+
119+
<Story name='PhoneRemovalPending' storyModule={Stories} />
120+
121+
## Phone removal error
122+
123+
The first removal fails. Retry in the confirmation to remove the phone number.
124+
125+
<Story name='PhoneRemovalError' storyModule={Stories} />

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

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,19 @@ function AccountSection({
3838
failWith,
3939
usernameFailWith,
4040
failEmailVerification = false,
41+
emailRemovalState,
42+
phoneRemovalState,
4143
}: {
4244
allowMultipleAccounts: boolean;
4345
failAt?: UserProfileAddPhoneDialogProps['step'];
4446
failWith?: UserProfileFormError;
4547
usernameFailWith?: UserProfileFormError;
4648
failEmailVerification?: boolean;
49+
emailRemovalState?: 'pending' | 'error';
50+
phoneRemovalState?: 'pending' | 'error';
4751
}) {
52+
const [phoneRemovalFailed, setPhoneRemovalFailed] = useState(false);
53+
const [emailRemovalFailed, setEmailRemovalFailed] = useState(false);
4854
const editName = useUserProfileEditNameFixture({ failWith });
4955
const editUsername = useUserProfileEditUsernameFixture({ failWith: usernameFailWith });
5056
const [emails, setEmails] = useState<UserProfileEmail[]>(
@@ -84,9 +90,27 @@ function AccountSection({
8490
onRemoveProfilePicture={clearImage}
8591
onManageEmail={() => undefined}
8692
onManagePhone={() => undefined}
87-
onRemoveEmail={id => setEmails(current => current.filter(email => email.id !== id))}
93+
onRemoveEmail={async id => {
94+
if (emailRemovalState === 'pending') {
95+
await new Promise(resolve => setTimeout(resolve, 1500));
96+
}
97+
if (emailRemovalState === 'error' && !emailRemovalFailed) {
98+
setEmailRemovalFailed(true);
99+
throw new Error('Unable to remove this email address. Try again.');
100+
}
101+
setEmails(current => current.filter(email => email.id !== id));
102+
}}
88103
onSetPrimaryEmail={id => setEmails(current => current.map(email => ({ ...email, isDefault: email.id === id })))}
89-
onRemovePhone={id => setPhones(current => current.filter(phone => phone.id !== id))}
104+
onRemovePhone={async id => {
105+
if (phoneRemovalState === 'pending') {
106+
await new Promise(resolve => setTimeout(resolve, 1500));
107+
}
108+
if (phoneRemovalState === 'error' && !phoneRemovalFailed) {
109+
setPhoneRemovalFailed(true);
110+
throw new Error('Unable to remove this phone number. Try again.');
111+
}
112+
setPhones(current => current.filter(phone => phone.id !== id));
113+
}}
90114
onSetPrimaryPhone={id => setPhones(current => current.map(phone => ({ ...phone, isDefault: phone.id === id })))}
91115
/>
92116
);
@@ -210,3 +234,39 @@ export function AddPhoneFails() {
210234
/>
211235
);
212236
}
237+
238+
export function EmailRemovalPending() {
239+
return (
240+
<AccountSection
241+
allowMultipleAccounts
242+
emailRemovalState='pending'
243+
/>
244+
);
245+
}
246+
247+
export function EmailRemovalError() {
248+
return (
249+
<AccountSection
250+
allowMultipleAccounts
251+
emailRemovalState='error'
252+
/>
253+
);
254+
}
255+
256+
export function PhoneRemovalPending() {
257+
return (
258+
<AccountSection
259+
allowMultipleAccounts
260+
phoneRemovalState='pending'
261+
/>
262+
);
263+
}
264+
265+
export function PhoneRemovalError() {
266+
return (
267+
<AccountSection
268+
allowMultipleAccounts
269+
phoneRemovalState='error'
270+
/>
271+
);
272+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Stories from './user-profile-connected-accounts-section.stories';
22

33
# UserProfileConnectedAccountsSection
44

5-
Linked accounts and available providers are supplied separately. These examples simulate state through props; they do not start OAuth or mutate a user. Reload to reset interactive examples.
5+
Linked accounts and available providers are supplied separately. Removal uses one shared confirmation per section, which awaits the removal callback and displays failures. These examples simulate actions; they do not start OAuth or mutate a user. Reload to reset interactive examples.
66

77
<Story name='Default' storyModule={Stories} />
88

@@ -30,7 +30,7 @@ Recoverable verification errors and additional scope requirements show a Disconn
3030

3131
## Removal Pending
3232

33-
Choose Remove from the row menu, then confirm to show the pending state.
33+
Choose Remove from the row menu, then confirm. The dialog shows pending for a short delay, then the account becomes available to connect again.
3434

3535
<Story name='RemovalPending' storyModule={Stories} />
3636

‎packages/swingset/src/stories/user-profile-profile-panel.stories.tsx‎

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import { createUserProfileAddPhoneFixture } from './fixtures/user-profile-add-ph
1313
import { useConnectedAccountsFixture } from './fixtures/user-profile-connected-accounts';
1414
import { useUserProfileEditNameFixture } from './fixtures/user-profile-edit-name';
1515
import { useUserProfileEditUsernameFixture } from './fixtures/user-profile-edit-username';
16+
import { useWeb3WalletsFixture } from './fixtures/user-profile-web3-wallets';
1617

17-
const providerIconUrl = (provider: string) => `https://img.clerk.com/static/${provider}.svg`;
1818
const profileImageUrl = 'https://avatars.githubusercontent.com/u/51144033?v=4';
1919

2020
export { default as __source } from './user-profile-profile-panel.stories?raw';
@@ -38,6 +38,7 @@ export function Default(_args: Record<string, unknown>) {
3838
]);
3939
const { imageUrl, showFile, clearImage } = usePreviewImage(profileImageUrl);
4040
const connections = useConnectedAccountsFixture();
41+
const wallets = useWeb3WalletsFixture();
4142
const editName = useUserProfileEditNameFixture();
4243
const editUsername = useUserProfileEditUsernameFixture();
4344
const emailFlow = createUserProfileAddEmailFixture({
@@ -54,19 +55,8 @@ export function Default(_args: Record<string, unknown>) {
5455
connectedAccounts={connections.accounts}
5556
availableConnectionProviders={connections.availableProviders}
5657
onReconnectAccount={connections.onReconnect}
57-
web3Wallets={[
58-
{
59-
id: 'metamask',
60-
address: '0x71C7656EC7ab88b098defB751B7401B5f6d8976F',
61-
provider: 'MetaMask',
62-
iconUrl: providerIconUrl('metamask'),
63-
isPrimary: true,
64-
isVerified: true,
65-
},
66-
]}
67-
availableWeb3Providers={[
68-
{ id: 'coinbase-wallet', provider: 'Coinbase Wallet', iconUrl: providerIconUrl('coinbase_wallet') },
69-
]}
58+
web3Wallets={wallets.wallets}
59+
availableWeb3Providers={wallets.availableProviders}
7060
hasImage={Boolean(imageUrl)}
7161
imageUrl={imageUrl}
7262
phones={phones}
@@ -82,9 +72,9 @@ export function Default(_args: Record<string, unknown>) {
8272
onRemoveProfilePicture={clearImage}
8373
onRemoveEmail={id => setEmails(current => current.filter(email => email.id !== id))}
8474
onRemovePhone={id => setPhones(current => current.filter(phone => phone.id !== id))}
85-
onConnectWeb3Wallet={() => undefined}
86-
onRemoveWeb3Wallet={() => undefined}
87-
onSetPrimaryWeb3Wallet={() => undefined}
75+
onConnectWeb3Wallet={wallets.onConnect}
76+
onRemoveWeb3Wallet={wallets.onRemove}
77+
onSetPrimaryWeb3Wallet={wallets.onSetPrimary}
8878
onSetPrimaryEmail={id => setEmails(current => current.map(email => ({ ...email, isDefault: email.id === id })))}
8979
onSetPrimaryPhone={id => setPhones(current => current.map(phone => ({ ...phone, isDefault: phone.id === id })))}
9080
onVerifyEmail={() => undefined}

0 commit comments

Comments
 (0)