-
Notifications
You must be signed in to change notification settings - Fork 5.5k
fix: account list in SRP reveal flow #39005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
22ed2ed
chore: add card component
gantunesr 1eec32c
chore: add new getWalletIdsByType selector
gantunesr 3e1d539
fix: accounts list in SRP reveal flow
gantunesr b7cfca3
test: update unit test
gantunesr 1d839dc
fix: lint
gantunesr 5e3e2fb
Merge branch 'main' into gar/fix/srp-backup-list
gantunesr 877ffa0
Merge branch 'main' into gar/fix/srp-backup-list
gantunesr 427489d
Merge branch 'main' into gar/fix/srp-backup-list
gantunesr 66fd6ec
Merge branch 'main' into gar/fix/srp-backup-list
gantunesr 8cdf97b
chore: add wallets check
gantunesr eed7be4
Merge branch 'gar/fix/srp-backup-list' of https://github.com/MetaMask…
gantunesr e20c1ad
Merge branch 'main' into gar/fix/srp-backup-list
gantunesr 9b6ab86
test: update checkAccountBelongsToSrp
gantunesr be447e8
test: update unit tests
gantunesr ede3f40
chore: lint
gantunesr 5fd3e63
fix: circular dependency
gantunesr 4709bd7
chore: lint
gantunesr 23e30cf
test: fix E2E import SRP
gantunesr b8fec12
chore: remove only
gantunesr 92f317a
test: revert e2e change
gantunesr d428a5a
fix: typo
gantunesr e09bda0
test: wait for account creation to finish
gantunesr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,6 @@ | |
| &__account-name { | ||
| overflow: hidden; | ||
| white-space: nowrap; | ||
| max-width: 80px; | ||
| max-width: 120px; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export { SrpList } from './srp-list'; | ||
| export { SrpListItem } from './srp-list-item'; |
99 changes: 99 additions & 0 deletions
99
ui/components/multichain/multi-srp/srp-list/srp-card.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import React from 'react'; | ||
| import configureMockStore from 'redux-mock-store'; | ||
| import thunk from 'redux-thunk'; | ||
| import { fireEvent } from '@testing-library/react'; | ||
|
|
||
| import { KeyringTypes } from '@metamask/keyring-controller'; | ||
| import type { AccountGroupId, AccountWalletId } from '@metamask/account-api'; | ||
|
|
||
| import { renderWithProvider } from '../../../../../test/lib/render-helpers-navigate'; | ||
| import mockState from '../../../../../test/data/mock-state.json'; | ||
| import { FirstTimeFlowType } from '../../../../../shared/constants/onboarding'; | ||
| import { SrpCard } from './srp-card'; | ||
|
|
||
| const mockWalletId = 'mock-wallet-id' as AccountWalletId; | ||
| const mockTotalFiatBalance = '$100.00'; | ||
|
|
||
| const mocks = { | ||
| useSingleWalletAccountsBalanceCallback: jest | ||
| .fn() | ||
| .mockReturnValue((_: AccountGroupId) => mockTotalFiatBalance), | ||
| onActionComplete: jest.fn(), | ||
| useWalletInfoCallback: jest.fn().mockReturnValue({ | ||
| multichainAccounts: [ | ||
| { | ||
| id: 'mock-account-id-1' as AccountGroupId, | ||
| metadata: { | ||
| name: 'Mock Account 1', | ||
| }, | ||
| }, | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ], | ||
| keyringId: '01JKAF3DSGM3AB87EM9N0K41AJ', | ||
| isSRPBackedUp: true, | ||
| }), | ||
| }; | ||
|
|
||
| jest.mock('../../../../hooks/multichain-accounts/useWalletBalance', () => ({ | ||
| useSingleWalletAccountsBalanceCallback: (walletId: AccountWalletId) => | ||
| mocks.useSingleWalletAccountsBalanceCallback(walletId), | ||
| })); | ||
|
|
||
| jest.mock('../../../../hooks/multichain-accounts/useWalletInfo', () => ({ | ||
| useWalletInfo: (walletId: AccountWalletId) => | ||
| mocks.useWalletInfoCallback(walletId), | ||
| })); | ||
|
|
||
| const mockSecondHdKeyring = { | ||
| accounts: [], | ||
| type: KeyringTypes.hd, | ||
| metadata: { | ||
| id: '01JN31PKMJ3ANWYFJZM3Z8MYT4', | ||
| name: '', | ||
| }, | ||
| }; | ||
|
|
||
| const render = (shouldTriggerBackup: boolean) => { | ||
| const store = configureMockStore([thunk])({ | ||
| ...mockState, | ||
| metamask: { | ||
| ...mockState.metamask, | ||
| keyrings: [...mockState.metamask.keyrings, mockSecondHdKeyring], | ||
| firstTimeFlowType: FirstTimeFlowType.create, | ||
| seedPhraseBackedUp: false, | ||
| }, | ||
| }); | ||
|
|
||
| return renderWithProvider( | ||
| <SrpCard | ||
| index={0} | ||
| walletId={mockWalletId} | ||
| shouldTriggerBackup={shouldTriggerBackup} | ||
| onActionComplete={mocks.onActionComplete} | ||
| />, | ||
| store, | ||
| ); | ||
| }; | ||
|
|
||
| describe('SrpCard', () => { | ||
| it('renders the secret recovery phrases card', () => { | ||
| const { getByText } = render(false); | ||
| expect(getByText('Secret Recovery Phrase 1')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('shows/hides accounts when clicking show/hide text', () => { | ||
| const { getByText } = render(false); | ||
| const showAccountsButton = getByText('Show 1 account'); | ||
| fireEvent.click(showAccountsButton); | ||
| expect(getByText('Hide 1 account')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('calls onActionComplete when clicking a keyring', () => { | ||
| const { getByTestId } = render(true); | ||
| const firstKeyringId = mockState.metamask.keyrings[0].metadata.id; | ||
|
|
||
| const keyring = getByTestId(`hd-keyring-${firstKeyringId}`); | ||
| fireEvent.click(keyring); | ||
|
|
||
| expect(mocks.onActionComplete).toHaveBeenCalledWith(firstKeyringId, true); | ||
| }); | ||
| }); | ||
174 changes: 174 additions & 0 deletions
174
ui/components/multichain/multi-srp/srp-list/srp-card.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| import React, { useState, useContext, useCallback } from 'react'; | ||
| import type { AccountWalletId } from '@metamask/account-api'; | ||
|
|
||
| import { MetaMetricsContext } from '../../../../contexts/metametrics'; | ||
| import { useWalletInfo } from '../../../../hooks/multichain-accounts/useWalletInfo'; | ||
| import { useI18nContext } from '../../../../hooks/useI18nContext'; | ||
| import { useSingleWalletAccountsBalanceCallback } from '../../../../hooks/multichain-accounts/useWalletBalance'; | ||
| import { | ||
| MetaMetricsEventCategory, | ||
| MetaMetricsEventName, | ||
| } from '../../../../../shared/constants/metametrics'; | ||
|
|
||
| import Card from '../../../ui/card'; | ||
| import { | ||
| Box, | ||
| IconName, | ||
| Icon, | ||
| Text, | ||
| IconSize, | ||
| } from '../../../component-library'; | ||
| import { | ||
| JustifyContent, | ||
| Display, | ||
| TextColor, | ||
| FlexDirection, | ||
| AlignItems, | ||
| BlockSize, | ||
| TextVariant, | ||
| IconColor, | ||
| } from '../../../../helpers/constants/design-system'; | ||
| import { SrpListItem } from './srp-list-item'; | ||
|
|
||
| /** | ||
| * Props for the SrpCard component. | ||
| */ | ||
| type SrpCardProps = { | ||
| index: number; | ||
| walletId: AccountWalletId; | ||
| shouldTriggerBackup: boolean; | ||
| onActionComplete: (id: string, triggerBackup?: boolean) => void; | ||
| isSettingsPage?: boolean; | ||
| hideShowAccounts?: boolean; | ||
| }; | ||
|
|
||
| export const SrpCard = ({ | ||
| index, | ||
| walletId, | ||
| shouldTriggerBackup, | ||
| onActionComplete, | ||
| isSettingsPage = false, | ||
| hideShowAccounts = false, | ||
| }: SrpCardProps) => { | ||
| const t = useI18nContext(); | ||
| const trackEvent = useContext(MetaMetricsContext); | ||
| const { multichainAccounts, keyringId } = useWalletInfo(walletId); | ||
| const [showAccounts, setShowAccounts] = useState<boolean>(false); | ||
gantunesr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const walletAccountBalance = useSingleWalletAccountsBalanceCallback(walletId); | ||
|
|
||
| const showHideText = useCallback( | ||
| (numberOfAccounts: number): string => { | ||
| if (numberOfAccounts > 1) { | ||
| return showAccounts | ||
| ? t('SrpListHideAccounts', [numberOfAccounts]) | ||
| : t('SrpListShowAccounts', [numberOfAccounts]); | ||
| } | ||
| return showAccounts | ||
| ? t('SrpListHideSingleAccount', [numberOfAccounts]) | ||
| : t('SrpListShowSingleAccount', [numberOfAccounts]); | ||
| }, | ||
| [showAccounts, t], | ||
| ); | ||
|
|
||
| return ( | ||
| <Card | ||
| key={`srp-${index}-${keyringId}`} | ||
| data-testid={`hd-keyring-${keyringId}`} | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| onClick={() => { | ||
| trackEvent({ | ||
| category: MetaMetricsEventCategory.Accounts, | ||
| event: MetaMetricsEventName.SecretRecoveryPhrasePickerClicked, | ||
| properties: { | ||
| // TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31860 | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| button_type: 'srp_select', | ||
| }, | ||
| }); | ||
| keyringId && onActionComplete(keyringId, shouldTriggerBackup); | ||
gantunesr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }} | ||
| className="select-srp__container" | ||
| marginBottom={3} | ||
| > | ||
| <Box | ||
| display={Display.Flex} | ||
| flexDirection={FlexDirection.Row} | ||
| alignItems={AlignItems.center} | ||
| justifyContent={JustifyContent.spaceBetween} | ||
| > | ||
| <Box> | ||
| <Text variant={TextVariant.bodyMdMedium}> | ||
| {t('srpListName', [index + 1])} | ||
| </Text> | ||
| {!hideShowAccounts && ( | ||
| <Text | ||
| variant={TextVariant.bodySm} | ||
| color={TextColor.primaryDefault} | ||
| className="srp-list__show-accounts" | ||
| data-testid={`srp-list-show-accounts-${index}`} | ||
| onClick={(event: React.MouseEvent) => { | ||
| event.stopPropagation(); | ||
| trackEvent({ | ||
| category: MetaMetricsEventCategory.Accounts, | ||
| event: MetaMetricsEventName.SecretRecoveryPhrasePickerClicked, | ||
| properties: { | ||
| // TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31860 | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| button_type: 'details', | ||
| }, | ||
| }); | ||
| setShowAccounts((prevState) => !prevState); | ||
| }} | ||
| > | ||
| {showHideText(multichainAccounts.length)} | ||
| </Text> | ||
| )} | ||
| </Box> | ||
| <Box display={Display.Flex} alignItems={AlignItems.center} gap={1}> | ||
| {isSettingsPage && ( | ||
| <Text | ||
| variant={TextVariant.bodyMdMedium} | ||
| color={ | ||
| shouldTriggerBackup | ||
| ? TextColor.errorDefault | ||
| : TextColor.textAlternative | ||
| } | ||
| > | ||
| {shouldTriggerBackup | ||
| ? t('srpListStateNotBackedUp') | ||
| : t('srpListStateBackedUp')} | ||
| </Text> | ||
| )} | ||
| <Icon | ||
| name={IconName.ArrowRight} | ||
| size={IconSize.Sm} | ||
| color={ | ||
| shouldTriggerBackup && isSettingsPage | ||
| ? IconColor.errorDefault | ||
| : IconColor.iconAlternative | ||
| } | ||
| /> | ||
| </Box> | ||
| </Box> | ||
| {showAccounts && ( | ||
| <Box> | ||
| <Box | ||
| width={BlockSize.Full} | ||
| className="srp-list__divider" | ||
| marginTop={2} | ||
| marginBottom={2} | ||
| /> | ||
| {multichainAccounts.map((group) => { | ||
| return ( | ||
| <SrpListItem | ||
| key={`account-${group.id}`} | ||
| accountId={group.id} | ||
| accountName={group.metadata.name} | ||
| balance={walletAccountBalance(group.id) ?? ''} | ||
| /> | ||
| ); | ||
| })} | ||
| </Box> | ||
| )} | ||
| </Card> | ||
| ); | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.