Skip to content

Commit f0bc48b

Browse files
fix(mosaic): hold identifier row order when the primary changes
Email, phone and web3 wallet rows list the primary first when the section loads, then keep their positions when a new primary is set, so only the Primary badge moves. The new primary leads on the next load.
1 parent 3c07da9 commit f0bc48b

6 files changed

Lines changed: 22 additions & 13 deletions

File tree

‎packages/mosaic/src/features/user-profile/user-profile-email-section/__tests__/user-profile-email-section.feature.test.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ async function enterCode(actor: Actor, code: string) {
6868
}
6969

7070
describe('the user profile email section', () => {
71-
it('holds the menu while setting an address as primary, then moves the row first with focus', async () => {
71+
it('holds the menu and row order while setting an address as primary, then returns focus', async () => {
7272
const other = fapiEmailAddress({ id: 'idn_other', email_address: 'bob@example.com', verification: VERIFIED });
7373
const { actor } = await renderSection(signedInWithEmails([PRIMARY, other]));
7474
const primary = holdRequests('post', '/v1/me');
@@ -93,7 +93,7 @@ describe('the user profile email section', () => {
9393
}
9494
expect(screen.getByRole('button', { name: 'Manage bob@example.com' })).not.toHaveAttribute('aria-disabled', 'true');
9595
await waitFor(() => expect(screen.queryByRole('menu')).toBeNull());
96-
expect(emailsListed()).toEqual(['bob@example.com', 'alice@example.com']);
96+
expect(emailsListed()).toEqual(['alice@example.com', 'bob@example.com']);
9797
expect(screen.getByRole('button', { name: 'Manage bob@example.com' })).toHaveFocus();
9898
expect(screen.queryByRole('progressbar', { name: 'Set as primary' })).toBeNull();
9999
});

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import { useRef } from 'react';
1+
import { useRef, useState } from 'react';
22

33
import { useConfirmController } from '../../../blocks/confirm';
44
import { useListRemovalFocus } from '../../../hooks/use-list-removal-focus';
55
import { usePendingAction } from '../../../hooks/use-pending-action';
6+
import { applyOrder } from '../../../utils/apply-order';
67
import type { UserProfileEmail } from '../user-profile-contact.types';
78
import { useUserProfileAddEmailController } from './user-profile-add-email.controller';
89
import type { ReadyEmailSectionModel, UserProfileEmailSectionViewProps } from './user-profile-email-section.types';
910

1011
export function useUserProfileEmailSectionController({
11-
emails,
12+
emails: modelEmails,
1213
username,
1314
onCreateEmail,
1415
getEmailVerifier,
@@ -18,6 +19,8 @@ export function useUserProfileEmailSectionController({
1819
setPrimaryReverification,
1920
createReverification,
2021
}: Omit<ReadyEmailSectionModel, 'status' | 'userId'>): UserProfileEmailSectionViewProps {
22+
const [order] = useState(() => modelEmails.map(email => email.id));
23+
const emails = applyOrder(order, modelEmails, email => email.id);
2124
const verification = useUserProfileAddEmailController({
2225
username,
2326
onCreate: onCreateEmail,

‎packages/mosaic/src/features/user-profile/user-profile-phone-section/__tests__/user-profile-phone-section.feature.test.tsx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ async function enterCode(actor: Actor, code: string) {
9292
}
9393

9494
describe('the user profile phone section', () => {
95-
it('holds the menu while setting a number as primary, then moves the row first', async () => {
95+
it('holds the menu and row order while setting a number as primary', async () => {
9696
const { actor } = await renderSection(signedIn([HOME, WORK]));
9797
const primary = holdRequests('post', '/v1/me');
9898
try {
@@ -117,7 +117,7 @@ describe('the user profile phone section', () => {
117117
}
118118
expect(screen.getByRole('button', { name: `Manage ${WORK_LABEL}` })).not.toHaveAttribute('aria-disabled', 'true');
119119
await waitFor(() => expect(screen.queryByRole('menu')).toBeNull());
120-
expect(listed()).toEqual([WORK_LABEL, HOME_LABEL]);
120+
expect(listed()).toEqual([HOME_LABEL, WORK_LABEL]);
121121
expect(screen.queryByRole('progressbar', { name: 'Set as primary' })).toBeNull();
122122
});
123123

@@ -210,8 +210,8 @@ describe('the user profile phone section', () => {
210210

211211
await manage(actor, WORK_LABEL, 'Set as primary');
212212

213-
await waitFor(() => expect(listed()).toEqual([WORK_LABEL, HOME_LABEL]));
214-
expect(within(row()).getByText(WORK_LABEL).parentElement).toHaveTextContent('Primary');
213+
await waitFor(() => expect(within(row()).getByText(WORK_LABEL).parentElement).toHaveTextContent('Primary'));
214+
expect(listed()).toEqual([HOME_LABEL, WORK_LABEL]);
215215
});
216216

217217
it('removes a number once the removal is confirmed', async () => {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { stringToFormattedPhoneString } from '@clerk/shared/phone';
2-
import { useRef } from 'react';
2+
import { useRef, useState } from 'react';
33

44
import { useConfirmController } from '../../../blocks/confirm';
55
import { useListRemovalFocus } from '../../../hooks/use-list-removal-focus';
66
import { usePendingAction } from '../../../hooks/use-pending-action';
7+
import { applyOrder } from '../../../utils/apply-order';
78
import type { UserProfilePhone } from '../user-profile-contact.types';
89
import { useUserProfileAddPhoneController } from './user-profile-add-phone.controller';
910
import type { ReadyPhoneSectionModel, UserProfilePhoneSectionViewProps } from './user-profile-phone-section.types';
1011

1112
export function useUserProfilePhoneSectionController({
12-
phones,
13+
phones: modelPhones,
1314
defaultPhoneCountry,
1415
onCreatePhone,
1516
getPhoneVerifier,
@@ -19,6 +20,8 @@ export function useUserProfilePhoneSectionController({
1920
setPrimaryReverification,
2021
createReverification,
2122
}: Omit<ReadyPhoneSectionModel, 'status' | 'userId'>): UserProfilePhoneSectionViewProps {
23+
const [order] = useState(() => modelPhones.map(phone => phone.id));
24+
const phones = applyOrder(order, modelPhones, phone => phone.id);
2225
const verification = useUserProfileAddPhoneController({
2326
onCreate: onCreatePhone,
2427
reverification: createReverification,

‎packages/mosaic/src/features/user-profile/user-profile-web3-wallets-section/__tests__/user-profile-web3-wallets-section.feature.test.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ describe('Web3 wallets', () => {
464464
await waitFor(() => expect(screen.getByText('Primary')).toBeInTheDocument());
465465
});
466466

467-
it('holds the wallet menu while setting primary and moves the new primary first', async () => {
467+
it('holds the wallet menu while setting primary and updates the badge in place', async () => {
468468
const fapi = await renderWeb3({
469469
web3_wallets: [
470470
fapiWeb3Wallet({ id: 'wallet_1', web3_wallet: '0x1234567890abcdef' }),
@@ -498,7 +498,7 @@ describe('Web3 wallets', () => {
498498

499499
await waitFor(() => expect(fapi.client.sessions[0]?.user.primary_web3_wallet_id).toBe('wallet_2'));
500500
await waitFor(() => expect(screen.queryByRole('menu')).toBeNull());
501-
expect(screen.getAllByRole('button', { name: 'Manage MetaMask' })[0].closest('.cl-section-row')).toContainElement(
501+
expect(screen.getAllByRole('button', { name: 'Manage MetaMask' })[1].closest('.cl-section-row')).toContainElement(
502502
screen.getByTitle('0xabcdef1234567890'),
503503
);
504504
const selectedRow = screen.getByTitle('0xabcdef1234567890').closest<HTMLElement>('.cl-section-row');

‎packages/mosaic/src/features/user-profile/user-profile-web3-wallets-section/user-profile-web3-wallets-section.controller.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { usePanelTitle } from '../../../components/panel';
55
import { useListRemovalFocus } from '../../../hooks/use-list-removal-focus';
66
import { usePendingAction } from '../../../hooks/use-pending-action';
77
import { useMessages } from '../../../localization';
8+
import { applyOrder } from '../../../utils/apply-order';
89
import type {
910
ReadyWeb3WalletsModel,
1011
UserProfileWeb3Provider,
@@ -23,7 +24,7 @@ type WalletAction =
2324
| { kind: 'verify' | 'primary'; walletId: string };
2425

2526
export function useUserProfileWeb3WalletsController({
26-
wallets,
27+
wallets: modelWallets,
2728
availableProviders,
2829
connect,
2930
verify,
@@ -45,6 +46,8 @@ export function useUserProfileWeb3WalletsController({
4546
| 'createReverification'
4647
>) {
4748
const messages = useMessages('userProfileWeb3Wallets');
49+
const [order] = useState(() => modelWallets.map(wallet => wallet.id));
50+
const wallets = applyOrder(order, modelWallets, wallet => wallet.id);
4851
const action = usePendingAction({ errorFallback: messages.errors.generic });
4952
const [picker, setPicker] = useState<SolanaPicker>({ open: false });
5053
const [owner, setOwner] = useState<WalletAction>();

0 commit comments

Comments
 (0)