-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
feat: MMS-1872 destination account picker for sol-evm (standalone component and story) #30282
Merged
ghgoodreau
merged 14 commits into
main
from
MMS-1872-destination-account-picker-standalone-component
Feb 14, 2025
+597
−0
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
31f0d53
feat: account picker component and stories
ghgoodreau 5392447
feat: fix some styles
ghgoodreau ea420d9
feat: resolve or expect errors
ghgoodreau c10abf4
feat: box shadow design token
ghgoodreau d0e7d70
Merge branch 'main' into MMS-1872-destination-account-picker-standalo…
ghgoodreau 4397845
feat: address comments
ghgoodreau 41e6c24
Merge branch 'main' into MMS-1872-destination-account-picker-standalo…
ghgoodreau 2b8e6e3
chore: fix linting errors in bridge dir
ghgoodreau 4c86d20
Merge branch 'main' into MMS-1872-destination-account-picker-standalo…
ghgoodreau 4c3e8ec
fix: fix linting
ghgoodreau d02fc73
fix: linting again
ghgoodreau 38fab12
fix: revert
ghgoodreau 8732b64
fix: use SolAccountType type for comparison
ghgoodreau 366986a
Merge branch 'main' into MMS-1872-destination-account-picker-standalo…
ghgoodreau 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
228 changes: 228 additions & 0 deletions
228
ui/pages/bridge/prepare/components/destination-account-list-item.tsx
This file contains 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,228 @@ | ||
import React from 'react'; | ||
import classnames from 'classnames'; | ||
import { useSelector } from 'react-redux'; | ||
import { InternalAccount } from '@metamask/keyring-internal-api'; | ||
import { shortenAddress } from '../../../../helpers/utils/util'; | ||
|
||
import { | ||
AvatarAccount, | ||
AvatarAccountSize, | ||
AvatarAccountVariant, | ||
Box, | ||
Text, | ||
AvatarToken, | ||
AvatarTokenSize, | ||
} from '../../../../components/component-library'; | ||
|
||
import { | ||
AlignItems, | ||
BackgroundColor, | ||
BorderColor, | ||
Display, | ||
TextColor, | ||
TextVariant, | ||
JustifyContent, | ||
TextAlign, | ||
FlexDirection, | ||
} from '../../../../helpers/constants/design-system'; | ||
|
||
import { | ||
getUseBlockie, | ||
getShouldHideZeroBalanceTokens, | ||
getIsTokenNetworkFilterEqualCurrentNetwork, | ||
getChainIdsToPoll, | ||
getShowFiatInTestnets, | ||
} from '../../../../selectors'; | ||
// eslint-disable-next-line import/no-restricted-paths | ||
import { normalizeSafeAddress } from '../../../../../app/scripts/lib/multichain/address'; | ||
import { useMultichainAccountTotalFiatBalance } from '../../../../hooks/useMultichainAccountTotalFiatBalance'; | ||
import { useGetFormattedTokensPerChain } from '../../../../hooks/useGetFormattedTokensPerChain'; | ||
import { useAccountTotalCrossChainFiatBalance } from '../../../../hooks/useAccountTotalCrossChainFiatBalance'; | ||
import UserPreferencedCurrencyDisplay from '../../../../components/app/user-preferenced-currency-display/user-preferenced-currency-display.component'; | ||
import { PRIMARY } from '../../../../helpers/constants/common'; | ||
import { useMultichainSelector } from '../../../../hooks/useMultichainSelector'; | ||
import { | ||
getMultichainNetwork, | ||
getMultichainIsTestnet, | ||
getMultichainShouldShowFiat, | ||
getMultichainNativeCurrency, | ||
getMultichainNativeCurrencyImage, | ||
} from '../../../../selectors/multichain'; | ||
|
||
const MAXIMUM_CURRENCY_DECIMALS = 3; | ||
|
||
type DestinationAccountListItemProps = { | ||
account: InternalAccount; | ||
selected: boolean; | ||
onClick?: () => void; | ||
}; | ||
|
||
const DestinationAccountListItem: React.FC<DestinationAccountListItemProps> = ({ | ||
account, | ||
selected, | ||
onClick, | ||
}) => { | ||
const useBlockie = useSelector(getUseBlockie); | ||
const shouldHideZeroBalanceTokens = useSelector( | ||
getShouldHideZeroBalanceTokens, | ||
); | ||
const isTokenNetworkFilterEqualCurrentNetwork = useSelector( | ||
getIsTokenNetworkFilterEqualCurrentNetwork, | ||
); | ||
const allChainIDs = useSelector(getChainIdsToPoll); | ||
|
||
const { isEvmNetwork } = useMultichainSelector(getMultichainNetwork, account); | ||
const isTestnet = useMultichainSelector(getMultichainIsTestnet, account); | ||
const isMainnet = !isTestnet; | ||
const shouldShowFiat = useMultichainSelector( | ||
getMultichainShouldShowFiat, | ||
account, | ||
); | ||
const showFiatInTestnets = useSelector(getShowFiatInTestnets); | ||
const showFiat = | ||
shouldShowFiat && (isMainnet || (isTestnet && showFiatInTestnets)); | ||
|
||
const primaryTokenImage = useMultichainSelector( | ||
getMultichainNativeCurrencyImage, | ||
account, | ||
); | ||
const nativeCurrency = useMultichainSelector( | ||
getMultichainNativeCurrency, | ||
account, | ||
); | ||
|
||
const accountTotalFiatBalances = | ||
useMultichainAccountTotalFiatBalance(account); | ||
|
||
const { formattedTokensWithBalancesPerChain } = useGetFormattedTokensPerChain( | ||
account, | ||
shouldHideZeroBalanceTokens, | ||
isTokenNetworkFilterEqualCurrentNetwork, | ||
allChainIDs, | ||
); | ||
|
||
const { totalFiatBalance } = useAccountTotalCrossChainFiatBalance( | ||
account, | ||
formattedTokensWithBalancesPerChain, | ||
); | ||
|
||
// TODO: not working - troubleshoot. | ||
// const chainAvatars = Object.values(formattedTokensWithBalancesPerChain).map( | ||
// (chainData) => ({ | ||
// avatarValue: chainData.networkImage, | ||
// }), | ||
// ); | ||
|
||
let balanceToTranslate; | ||
if (isEvmNetwork) { | ||
balanceToTranslate = | ||
!shouldShowFiat || isTestnet || !process.env.PORTFOLIO_VIEW | ||
? // @ts-expect-error: balance is not typed. | ||
account.balance | ||
: totalFiatBalance; | ||
} else { | ||
balanceToTranslate = accountTotalFiatBalances.totalBalance; | ||
} | ||
|
||
return ( | ||
<Box | ||
display={Display.Flex} | ||
padding={4} | ||
backgroundColor={ | ||
selected ? BackgroundColor.primaryMuted : BackgroundColor.transparent | ||
} | ||
className={classnames('multichain-account-list-item', { | ||
'multichain-account-list-item--selected': selected, | ||
})} | ||
onClick={onClick} | ||
alignItems={AlignItems.center} | ||
> | ||
<AvatarAccount | ||
borderColor={BorderColor.transparent} | ||
size={AvatarAccountSize.Md} | ||
address={account.address} | ||
variant={ | ||
useBlockie | ||
? AvatarAccountVariant.Blockies | ||
: AvatarAccountVariant.Jazzicon | ||
} | ||
marginInlineEnd={2} | ||
/> | ||
|
||
<Box | ||
display={Display.Flex} | ||
flexDirection={FlexDirection.Column} | ||
style={{ flex: 1 }} | ||
> | ||
<Box | ||
display={Display.Flex} | ||
justifyContent={JustifyContent.spaceBetween} | ||
> | ||
<Text variant={TextVariant.bodyMdMedium}> | ||
{account.metadata.name} | ||
</Text> | ||
<Box | ||
display={Display.Flex} | ||
alignItems={AlignItems.center} | ||
justifyContent={JustifyContent.flexEnd} | ||
gap={1} | ||
> | ||
{/* <AvatarToken | ||
src={primaryTokenImage} | ||
name={nativeCurrency} | ||
size={AvatarTokenSize.Xs} | ||
borderColor={BorderColor.borderDefault} | ||
/> */} | ||
<Text | ||
as="div" | ||
display={Display.Flex} | ||
flexDirection={FlexDirection.Row} | ||
alignItems={AlignItems.center} | ||
justifyContent={JustifyContent.flexEnd} | ||
ellipsis | ||
textAlign={TextAlign.End} | ||
> | ||
<UserPreferencedCurrencyDisplay | ||
ethNumberOfDecimals={MAXIMUM_CURRENCY_DECIMALS} | ||
value={balanceToTranslate} | ||
type={PRIMARY} | ||
showFiat={showFiat} | ||
isAggregatedFiatOverviewBalance={showFiat} | ||
hideLabel={true} | ||
data-testid="first-currency-display" | ||
/> | ||
</Text> | ||
</Box> | ||
</Box> | ||
|
||
<Box | ||
display={Display.Flex} | ||
justifyContent={JustifyContent.spaceBetween} | ||
alignItems={AlignItems.center} | ||
> | ||
<Text | ||
variant={TextVariant.bodySm} | ||
color={TextColor.textAlternative} | ||
data-testid="account-list-address" | ||
> | ||
{shortenAddress(normalizeSafeAddress(account.address))} | ||
</Text> | ||
<Box display={Display.Flex} gap={2}> | ||
{/* // TODO: not working - troubleshoot. */} | ||
{/* {chainAvatars.length > 0 && ( | ||
<AvatarGroup members={chainAvatars} limit={4} /> | ||
)} */} | ||
<AvatarToken | ||
src={primaryTokenImage} | ||
name={nativeCurrency} | ||
size={AvatarTokenSize.Xs} | ||
borderColor={BorderColor.borderDefault} | ||
/> | ||
</Box> | ||
</Box> | ||
</Box> | ||
</Box> | ||
); | ||
}; | ||
|
||
export default React.memo(DestinationAccountListItem); |
57 changes: 57 additions & 0 deletions
57
ui/pages/bridge/prepare/components/destination-account-picker.stories.tsx
This file contains 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,57 @@ | ||
import React from 'react'; | ||
import { StoryFn } from '@storybook/react'; | ||
import { DestinationAccountPicker } from './destination-account-picker'; | ||
import { createMockInternalAccount } from '../../../../../test/jest/mocks'; | ||
import { InternalAccount } from '@metamask/keyring-internal-api'; | ||
|
||
export default { | ||
title: 'Pages/Bridge/Prepare/Components/DestinationAccountPicker', | ||
component: DestinationAccountPicker, | ||
}; | ||
|
||
const mockAccounts = [ | ||
createMockInternalAccount({ | ||
name: 'EVM Account 1', | ||
keyringType: 'HD Key Tree', | ||
}), | ||
createMockInternalAccount({ | ||
name: 'Solana Account 1', | ||
keyringType: 'Solana', | ||
}), | ||
createMockInternalAccount({ | ||
name: 'EVM Account 2', | ||
keyringType: 'HD Key Tree', | ||
}), | ||
createMockInternalAccount({ | ||
name: 'Solana Account 2', | ||
keyringType: 'Solana', | ||
}), | ||
] as InternalAccount[]; | ||
|
||
const Template: StoryFn<typeof DestinationAccountPicker> = (args) => ( | ||
<div style={{ width: '400px', height: '300px' }}> | ||
<DestinationAccountPicker {...args} /> | ||
</div> | ||
); | ||
|
||
export const EVMAccounts = Template.bind({}); | ||
EVMAccounts.args = { | ||
accounts: mockAccounts, | ||
chainType: 'evm', | ||
onAccountSelect: (account) => console.log('Selected:', account), | ||
}; | ||
|
||
export const SolanaAccounts = Template.bind({}); | ||
SolanaAccounts.args = { | ||
accounts: mockAccounts, | ||
chainType: 'solana', | ||
onAccountSelect: (account) => console.log('Selected:', account), | ||
}; | ||
|
||
export const WithSelectedAccount = Template.bind({}); | ||
WithSelectedAccount.args = { | ||
accounts: mockAccounts, | ||
chainType: 'evm', | ||
selectedSwapToAccount: mockAccounts[0], | ||
onAccountSelect: (account) => console.log('Selected:', account), | ||
}; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would
getImageForChainId
work here? Just merged my PR for adding support for multichain images