Skip to content

Commit

Permalink
feat(wallet): Edit Visible Assets Network Selector
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglashdaniel committed Oct 20, 2022
1 parent bd54554 commit 7a2f9f2
Show file tree
Hide file tree
Showing 15 changed files with 438 additions and 137 deletions.
27 changes: 16 additions & 11 deletions components/brave_wallet_ui/common/async/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ export async function getBuyAssetUrl (args: {
return url
}

export async function getTokenList (network: BraveWallet.NetworkInfo) {
const { blockchainRegistry } = getAPIProxy()
return (blockchainRegistry.getAllTokens(network.chainId, network.coin))
}

export async function getBuyAssets (onRampProvider: BraveWallet.OnRampProvider, chainId: string) {
const { blockchainRegistry } = getAPIProxy()
return (await blockchainRegistry.getBuyTokens(
Expand Down Expand Up @@ -514,14 +519,14 @@ export function refreshPrices () {

const price = token.balance > 0 && !token.token.isErc721
? await assetRatioService.getPrice(
[getTokenParam(token.token)],
[defaultFiatCurrency],
selectedPortfolioTimeline
)
[getTokenParam(token.token)],
[defaultFiatCurrency],
selectedPortfolioTimeline
)
: {
values: [{ ...emptyPrice, price: '0' }],
success: true
}
values: [{ ...emptyPrice, price: '0' }],
success: true
}

const tokenPrice = {
...price.values[0],
Expand Down Expand Up @@ -711,8 +716,8 @@ export function refreshKeyringInfo () {
const defaultAccounts = await Promise.all(SupportedCoinTypes.map(async (coin: BraveWallet.CoinType) => {
const chainId = await jsonRpcService.getChainId(coin)
const defaultAccount = coin === BraveWallet.CoinType.FIL
? await keyringService.getFilecoinSelectedAccount(chainId.chainId)
: await keyringService.getSelectedAccount(coin)
? await keyringService.getFilecoinSelectedAccount(chainId.chainId)
: await keyringService.getSelectedAccount(coin)
const defaultAccountAddress = defaultAccount.address
return walletInfo.accountInfos.find((account) => account.address.toLowerCase() === defaultAccountAddress?.toLowerCase()) ?? {} as BraveWallet.AccountInfo
}))
Expand All @@ -722,8 +727,8 @@ export function refreshKeyringInfo () {

// Get selectedAccountAddress
const getSelectedAccount = selectedCoin === BraveWallet.CoinType.FIL
? await keyringService.getFilecoinSelectedAccount(coinsChainId.chainId)
: await keyringService.getSelectedAccount(selectedCoin)
? await keyringService.getFilecoinSelectedAccount(coinsChainId.chainId)
: await keyringService.getSelectedAccount(selectedCoin)
const selectedAddress = getSelectedAccount.address

// Fallback account address if selectedAccount returns null
Expand Down
4 changes: 3 additions & 1 deletion components/brave_wallet_ui/common/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import useAssetManagement from './assets-management'
import useHasAccount from './has-account'
import usePrevNetwork from './previous-network'
import useIsMounted from './useIsMounted'
import useTokenRegistry from './useTokenRegistry'
import { useLib } from './useLib'

export {
Expand All @@ -35,5 +36,6 @@ export {
useSwap,
useTokenInfo,
useTransactionFeesParser,
useTransactionParser
useTransactionParser,
useTokenRegistry
}
70 changes: 70 additions & 0 deletions components/brave_wallet_ui/common/hooks/useTokenRegistry.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) 2022 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.
import * as React from 'react'

// Redux
import { useUnsafeWalletSelector } from './use-safe-selector'
import { WalletSelectors } from '../selectors'

// Types
import { TokenRegistry, BraveWallet } from '../../constants/types'

// Hooks
import { useLib } from './'

// Utils
import { addLogoToToken } from '../../utils/asset-utils'

export function useTokenRegistry () {
// Hooks
const { getTokenList } = useLib()

// Redux
const networkList = useUnsafeWalletSelector(WalletSelectors.networkList)

// Hook State
const [tokenRegistry, setTokenRegistry] = React.useState<TokenRegistry>({})
const [isLoading, setIsLoading] = React.useState<boolean>(true)

React.useEffect(() => {
let subscribed = true
let registry = tokenRegistry
Promise.all(networkList.map(async (network) => {
await getTokenList(network).then(
(result) => {
const formattedListWithIcons = result.tokens.map((token) => {
return addLogoToToken(token)
})
registry[network.chainId] = formattedListWithIcons
}
).catch((error) => {
if (!subscribed) {
return
}
console.log(error)
setIsLoading(false)
})
})).then(() => {
if (!subscribed) {
return
}
setTokenRegistry(registry)
setIsLoading(false)
})
// cleanup
return () => {
subscribed = false
}
}, [tokenRegistry, networkList, getTokenList])

// Creates a flat list of all tokens in the tokenRegistry
const fullTokenListAllChains: BraveWallet.BlockchainToken[] = React.useMemo(() => {
return Object.keys(tokenRegistry).length === 0 ? [] : networkList.map((network) => tokenRegistry[network.chainId]).flat(1)
}, [tokenRegistry, networkList, Object.keys(tokenRegistry).length])

return { tokenRegistry, fullTokenListAllChains, isLoading }
}

export default useTokenRegistry
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) 2022 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.

import * as React from 'react'

// Types
import { BraveWallet } from '../../../constants/types'

// Options
import { AllNetworksOption } from '../../../options/network-filter-options'

// Components
import SelectNetworkDropdown from '../select-network-dropdown'
import SearchBar from '../../shared/search-bar'

// Styled Components
import {
StyledWrapper,
HorizontalDivider
} from './style'

interface Props {
selectedNetwork: BraveWallet.NetworkInfo
showNetworkDropDown: boolean
onClick: () => void
onSelectNetwork?: (network: BraveWallet.NetworkInfo) => void
searchPlaceholder: string
searchAction?: (event: any) => void | undefined
searchAutoFocus?: boolean
searchValue?: string
}

export const NetworkFilterWithSearch = (props: Props) => {
const {
selectedNetwork,
onClick,
showNetworkDropDown,
onSelectNetwork,
searchPlaceholder,
searchAction,
searchAutoFocus,
searchValue
} = props

return (
<StyledWrapper>
<SearchBar
placeholder={searchPlaceholder}
action={searchAction}
autoFocus={searchAutoFocus}
value={searchValue}
useWithFilter={true}
/>
<HorizontalDivider />
<SelectNetworkDropdown
onSelectCustomNetwork={onSelectNetwork}
selectedNetwork={selectedNetwork}
onClick={onClick}
showNetworkDropDown={showNetworkDropDown}
useWithSearch={true}
customNetwork={AllNetworksOption}
/>
</StyledWrapper>
)
}

export default NetworkFilterWithSearch
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2022 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.

import styled from 'styled-components'

export const StyledWrapper = styled.div`
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
width: 100%;
border: ${(p) => `1px solid ${p.theme.color.interactive08}`};
border-radius: 4px;
`

export const HorizontalDivider = styled.div`
display: flex;
width: 1px;
height: 24px;
background-color: ${(p) => p.theme.color.interactive08};
`
Loading

0 comments on commit 7a2f9f2

Please sign in to comment.