-
Notifications
You must be signed in to change notification settings - Fork 316
fix(dashboard): if show one network only, select it by default #5682
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 all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export * from './useAddressBook' | ||
| export * from './useERC20TokensDetailed' | ||
| export * from './useGasConfig' | ||
| export * from './useIsMatched' | ||
| export * from './useRecentTransactions' |
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,6 @@ | ||
| import { useMatch } from 'react-router-dom' | ||
|
|
||
| export function useIsMatched(...args: Parameters<typeof useMatch>) { | ||
| const match = useMatch(...args) | ||
| return match !== null | ||
| } |
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,21 +1,6 @@ | ||
| import { useEffect, useMemo, useState } from 'react' | ||
| import BigNumber from 'bignumber.js' | ||
| import { useWeb3State } from '@masknet/web3-shared-evm' | ||
| import { StartUp } from './StartUp' | ||
| import { Assets } from './components/Assets' | ||
| import { Route, Routes, useLocation, useMatch, useNavigate } from 'react-router-dom' | ||
| import { Balance } from './components/Balance' | ||
| import { Transfer } from './components/Transfer' | ||
| import { History } from './components/History' | ||
| import { PageFrame } from '../../components/PageFrame' | ||
| import { ReceiveDialog } from './components/ReceiveDialog' | ||
| import { DashboardRoutes } from '@masknet/shared-base' | ||
| import { useRemoteControlledDialog } from '@masknet/shared' | ||
| import { PluginMessages } from '../../API' | ||
| import { WalletStateBar } from './components/WalletStateBar' | ||
| import { useDashboardI18N } from '../../locales' | ||
| import { | ||
| getRegisteredWeb3Networks, | ||
| NetworkPluginID, | ||
| useAccount, | ||
| useChainId, | ||
| useNetworkDescriptor, | ||
|
|
@@ -25,7 +10,24 @@ import { | |
| useWeb3State as useWeb3PluginState, | ||
| Web3Plugin, | ||
| } from '@masknet/plugin-infra' | ||
| import { useRemoteControlledDialog } from '@masknet/shared' | ||
| import { DashboardRoutes } from '@masknet/shared-base' | ||
| import { useWeb3State } from '@masknet/web3-shared-evm' | ||
| import BigNumber from 'bignumber.js' | ||
| import { useEffect, useMemo, useState } from 'react' | ||
| import { Route, Routes, useLocation, useNavigate } from 'react-router-dom' | ||
| import { useAsync } from 'react-use' | ||
| import { PluginMessages } from '../../API' | ||
| import { PageFrame } from '../../components/PageFrame' | ||
| import { useDashboardI18N } from '../../locales' | ||
| import { Assets } from './components/Assets' | ||
| import { Balance } from './components/Balance' | ||
| import { History } from './components/History' | ||
| import { ReceiveDialog } from './components/ReceiveDialog' | ||
| import { Transfer } from './components/Transfer' | ||
| import { WalletStateBar } from './components/WalletStateBar' | ||
| import { useIsMatched } from './hooks' | ||
| import { StartUp } from './StartUp' | ||
| import { getTokenUSDValue } from './utils/getTokenUSDValue' | ||
|
|
||
| function Wallets() { | ||
|
|
@@ -40,9 +42,9 @@ function Wallets() { | |
| const network = useNetworkDescriptor() | ||
|
|
||
| const { pathname } = useLocation() | ||
| const isWalletPath = useMatch(DashboardRoutes.Wallets) | ||
| const isWalletTransferPath = useMatch(DashboardRoutes.WalletsTransfer) | ||
| const isWalletHistoryPath = useMatch(DashboardRoutes.WalletsHistory) | ||
| const isWalletPath = useIsMatched(DashboardRoutes.Wallets) | ||
| const isWalletTransferPath = useIsMatched(DashboardRoutes.WalletsTransfer) | ||
| const isWalletHistoryPath = useIsMatched(DashboardRoutes.WalletsHistory) | ||
|
Comment on lines
+45
to
+47
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. boolean is boolean |
||
|
|
||
| const [receiveOpen, setReceiveOpen] = useState(false) | ||
|
|
||
|
|
@@ -60,29 +62,36 @@ function Wallets() { | |
| async () => Asset?.getFungibleAssets?.(account, portfolioProvider, network!), | ||
| [account, Asset, portfolioProvider, network], | ||
| ) | ||
| const renderNetworks = useMemo(() => { | ||
| return networks.filter((x) => pluginId === x.networkSupporterPluginID && x.isMainnet) | ||
| }, [networks, pluginId]) | ||
|
|
||
| // If show one network only, set it as default network | ||
| const defaultNetwork = useMemo(() => { | ||
| if (renderNetworks.length !== 1) return null | ||
| return renderNetworks[0] | ||
| }, [renderNetworks]) | ||
|
|
||
| useEffect(() => { | ||
| if (isWalletPath) return | ||
| setSelectedNetwork(networkDescriptor ?? null) | ||
| }, [networkDescriptor]) | ||
| setSelectedNetwork(networkDescriptor || defaultNetwork) | ||
| }, [isWalletPath, networkDescriptor, defaultNetwork]) | ||
|
|
||
| useEffect(() => { | ||
| if (isWalletTransferPath || isWalletHistoryPath) { | ||
| setSelectedNetwork(networkDescriptor ?? null) | ||
| setSelectedNetwork(networkDescriptor || defaultNetwork) | ||
| return | ||
| } | ||
| setSelectedNetwork(null) | ||
| }, [pathname]) | ||
| setSelectedNetwork(defaultNetwork) | ||
| }, [pathname, defaultNetwork]) | ||
|
|
||
| const balance = useMemo(() => { | ||
| return BigNumber.sum | ||
| .apply( | ||
| null, | ||
| detailedTokens | ||
| ?.filter((x) => (selectedNetwork ? x.chainId === selectedNetwork.chainId : true)) | ||
| ?.map((y) => getTokenUSDValue(y.value)) ?? [], | ||
| ) | ||
| .toNumber() | ||
| if (!detailedTokens || !detailedTokens.length) return 0 | ||
|
|
||
| const values = detailedTokens | ||
| .filter((x) => (selectedNetwork ? x.chainId === selectedNetwork.chainId : true)) | ||
| .map((y) => getTokenUSDValue(y.value)) | ||
| return BigNumber.sum(...values).toNumber() | ||
| }, [selectedNetwork, detailedTokens]) | ||
|
|
||
| const pateTitle = useMemo(() => { | ||
|
|
@@ -107,10 +116,10 @@ function Wallets() { | |
| onBuy={openBuyDialog} | ||
| onSwap={openSwapDialog} | ||
| onReceive={() => setReceiveOpen(true)} | ||
| networks={networks} | ||
| networks={renderNetworks} | ||
| selectedNetwork={selectedNetwork} | ||
| onSelectNetwork={setSelectedNetwork} | ||
| pluginId={pluginId} | ||
| showOperations={pluginId === NetworkPluginID.PLUGIN_EVM} | ||
| /> | ||
| <Routes> | ||
| <Route path="/" element={<Assets network={selectedNetwork} />} /> | ||
|
|
||
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 |
|---|---|---|
| @@ -1,4 +1,12 @@ | ||
| import { toFixed } from '@masknet/web3-shared-base' | ||
| import type BigNumber from 'bignumber.js' | ||
|
|
||
| export function formatAddress(address: string, size = 0) { | ||
| if (size === 0 || size >= 22) return address | ||
| return `${address.substr(0, 2 + size)}...${address.substr(-size)}` | ||
| } | ||
|
|
||
| export function formatCurrency(value: BigNumber.Value, sign = '') { | ||
| const digits = toFixed(value) | ||
| return `${sign}${digits}` | ||
| } |
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.
Reduce concept, this component has no need to care about pluginId things