-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: mf-4488 send token * refactor: reuse collectible list * fixup! feat: mf-4488 send token
- Loading branch information
Showing
54 changed files
with
1,205 additions
and
2,238 deletions.
There are no files selected for viewing
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
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
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
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
128 changes: 128 additions & 0 deletions
128
packages/mask/src/extension/popups/components/TokenPicker/TokenItem.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,128 @@ | ||
import { Icons } from '@masknet/icons' | ||
import { ImageIcon, TokenIcon } from '@masknet/shared' | ||
import { makeStyles } from '@masknet/theme' | ||
import type { Web3Helper } from '@masknet/web3-helpers' | ||
import { useWeb3Others } from '@masknet/web3-hooks-base' | ||
import { formatBalance, type NetworkDescriptor } from '@masknet/web3-shared-base' | ||
import type { ChainId, NetworkType } from '@masknet/web3-shared-evm' | ||
import { Box, ListItem, ListItemIcon, ListItemText, Typography, type ListItemProps, Link } from '@mui/material' | ||
import { memo, useEffect, useMemo, useRef } from 'react' | ||
|
||
const useStyles = makeStyles()((theme) => { | ||
return { | ||
item: { | ||
padding: theme.spacing(1, 1.5), | ||
borderRadius: 8, | ||
border: `1px solid ${theme.palette.maskColor.line}`, | ||
marginBottom: theme.spacing(1), | ||
}, | ||
selected: { | ||
borderColor: theme.palette.maskColor.highlight, | ||
}, | ||
tokenIcon: { | ||
width: 36, | ||
height: 36, | ||
}, | ||
badgeIcon: { | ||
position: 'absolute', | ||
right: -6, | ||
bottom: -4, | ||
border: `1px solid ${theme.palette.common.white}`, | ||
borderRadius: '50%', | ||
}, | ||
text: { | ||
fontSize: 16, | ||
fontWeight: 700, | ||
color: theme.palette.maskColor.main, | ||
}, | ||
name: { | ||
fontSize: 14, | ||
color: theme.palette.maskColor.second, | ||
display: 'flex', | ||
alignItems: 'center', | ||
}, | ||
balance: { | ||
fontSize: 16, | ||
fontWeight: 700, | ||
color: theme.palette.maskColor.second, | ||
}, | ||
link: { | ||
color: theme.palette.maskColor.second, | ||
display: 'inline-flex', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
marginLeft: 4, | ||
}, | ||
} | ||
}) | ||
|
||
export interface TokenItemProps extends Omit<ListItemProps, 'onSelect'> { | ||
asset: Web3Helper.FungibleAssetAll | ||
network: NetworkDescriptor<ChainId, NetworkType> | undefined | ||
selected?: boolean | ||
onSelect?(asset: Web3Helper.FungibleAssetAll): void | ||
} | ||
|
||
export const TokenItem = memo(function TokenItem({ | ||
className, | ||
asset, | ||
network, | ||
onSelect, | ||
selected, | ||
...rest | ||
}: TokenItemProps) { | ||
const { classes, cx } = useStyles() | ||
|
||
const Others = useWeb3Others() | ||
const explorerLink = useMemo(() => { | ||
return Others.explorerResolver.fungibleTokenLink(asset.chainId, asset.address) | ||
}, [asset.address, asset.chainId, Others.explorerResolver.fungibleTokenLink]) | ||
|
||
const liRef = useRef<HTMLLIElement>(null) | ||
useEffect(() => { | ||
if (!selected) return | ||
liRef.current?.scrollIntoView() | ||
}, [selected, liRef.current]) | ||
|
||
return ( | ||
<ListItem | ||
secondaryAction={ | ||
<Typography className={classes.balance}> | ||
{formatBalance(asset.balance, asset.decimals, 0, false, true, 5)} | ||
</Typography> | ||
} | ||
className={cx(classes.item, className, selected ? classes.selected : null)} | ||
onClick={() => onSelect?.(asset)} | ||
ref={liRef} | ||
{...rest}> | ||
<ListItemIcon> | ||
{/* TODO utility TokenIcon with badge */} | ||
<Box position="relative"> | ||
<TokenIcon | ||
className={classes.tokenIcon} | ||
chainId={asset.chainId} | ||
address={asset.address} | ||
size={36} | ||
/> | ||
<ImageIcon className={classes.badgeIcon} size={16} icon={network?.icon} /> | ||
</Box> | ||
</ListItemIcon> | ||
<ListItemText | ||
secondary={ | ||
<Typography className={classes.name}> | ||
{asset.name} | ||
<Link | ||
onClick={(event) => event.stopPropagation()} | ||
href={explorerLink} | ||
className={classes.link} | ||
target="_blank" | ||
rel="noopener noreferrer"> | ||
<Icons.LinkOut size={18} /> | ||
</Link> | ||
</Typography> | ||
}> | ||
<Typography className={classes.text}>{asset.symbol}</Typography> | ||
</ListItemText> | ||
</ListItem> | ||
) | ||
}) |
80 changes: 80 additions & 0 deletions
80
packages/mask/src/extension/popups/components/TokenPicker/index.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,80 @@ | ||
import { SelectNetworkSidebar } from '@masknet/shared' | ||
import { EMPTY_LIST, NetworkPluginID } from '@masknet/shared-base' | ||
import type { Web3Helper } from '@masknet/web3-helpers' | ||
import { useFungibleAssets, useNetworkDescriptors } from '@masknet/web3-hooks-base' | ||
import type { ChainId } from '@masknet/web3-shared-evm' | ||
import { Box, List, type BoxProps } from '@mui/material' | ||
import { memo, useMemo, useState } from 'react' | ||
import { TokenItem, type TokenItemProps } from './TokenItem.js' | ||
import { makeStyles } from '@masknet/theme' | ||
import { isSameAddress } from '@masknet/web3-shared-base' | ||
|
||
const useStyles = makeStyles()((theme) => { | ||
return { | ||
picker: { | ||
display: 'flex', | ||
flexDirection: 'row', | ||
overflow: 'auto', | ||
}, | ||
list: { | ||
overflow: 'auto', | ||
width: '100%', | ||
}, | ||
} | ||
}) | ||
|
||
export interface TokenPickerProps extends Omit<BoxProps, 'onSelect'>, Pick<TokenItemProps, 'onSelect'> { | ||
defaultChainId?: ChainId | ||
chainId?: ChainId | ||
address?: string | ||
} | ||
|
||
export const TokenPicker = memo(function TokenPicker({ | ||
defaultChainId, | ||
chainId, | ||
address, | ||
className, | ||
onSelect, | ||
...rest | ||
}: TokenPickerProps) { | ||
const { classes, cx } = useStyles() | ||
const { data: assets = EMPTY_LIST } = useFungibleAssets(NetworkPluginID.PLUGIN_EVM, undefined, { | ||
chainId, | ||
}) | ||
const [sidebarChainId, setSidebarChainId] = useState<Web3Helper.ChainIdAll | undefined>(defaultChainId) | ||
const availableAssets = useMemo(() => { | ||
if (!sidebarChainId) return assets | ||
return assets.filter((x) => x.chainId === sidebarChainId) | ||
}, [assets, sidebarChainId]) | ||
|
||
const allNetworks = useNetworkDescriptors(NetworkPluginID.PLUGIN_EVM) | ||
const networks = useMemo(() => allNetworks.filter((x) => x.isMainnet), [allNetworks]) | ||
|
||
return ( | ||
<Box className={cx(classes.picker, className)} {...rest}> | ||
<SelectNetworkSidebar | ||
networks={networks} | ||
pluginID={NetworkPluginID.PLUGIN_EVM} | ||
chainId={sidebarChainId} | ||
hiddenAllButton | ||
onChainChange={setSidebarChainId} | ||
/> | ||
<List className={classes.list} data-hide-scrollbar> | ||
{availableAssets.map((asset) => { | ||
const network = allNetworks.find((x) => x.chainId === asset.chainId) | ||
const selected = asset.chainId === chainId && isSameAddress(asset.address, address) | ||
|
||
return ( | ||
<TokenItem | ||
key={`${asset.chainId}.${asset.address}`} | ||
asset={asset} | ||
network={network} | ||
selected={selected} | ||
onSelect={onSelect} | ||
/> | ||
) | ||
})} | ||
</List> | ||
</Box> | ||
) | ||
}) |
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
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './useParamTab.js' | ||
export * from './useTitle.js' | ||
export * from './useTokenParams.js' |
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,21 @@ | ||
import { useCallback } from 'react' | ||
import { useSearchParams } from 'react-router-dom' | ||
|
||
export function useParamTab<T extends string>(defaultTab: T) { | ||
const [params, setParams] = useSearchParams() | ||
const tab = (params.get('tab') || defaultTab) as T | ||
const handleTabChange = useCallback( | ||
(_: unknown, tab: T) => { | ||
setParams( | ||
(params) => { | ||
params.set('tab', tab) | ||
return params.toString() | ||
}, | ||
{ replace: true }, | ||
) | ||
}, | ||
[setParams], | ||
) | ||
|
||
return [tab, handleTabChange] as const | ||
} |
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
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
Oops, something went wrong.