-
Notifications
You must be signed in to change notification settings - Fork 926
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(wallet): Edit Visible Assets Network Selector
- Loading branch information
1 parent
bd54554
commit 7a2f9f2
Showing
15 changed files
with
438 additions
and
137 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
70 changes: 70 additions & 0 deletions
70
components/brave_wallet_ui/common/hooks/useTokenRegistry.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,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 |
69 changes: 69 additions & 0 deletions
69
components/brave_wallet_ui/components/desktop/network-filter-with-search/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,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 |
23 changes: 23 additions & 0 deletions
23
components/brave_wallet_ui/components/desktop/network-filter-with-search/style.ts
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,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}; | ||
` |
Oops, something went wrong.