Skip to content
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

fix: edit visible assets modal bugs (uplift to 1.62.x) #21942

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion components/brave_wallet_ui/common/async/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,17 @@ handler.on(
WalletActions.setUserAssetVisible.type,
async (store: Store, payload: SetUserAssetVisiblePayloadType) => {
const { braveWalletService } = getAPIProxy()
await braveWalletService.setUserAssetVisible(

const { success } = await braveWalletService.setUserAssetVisible(
payload.token,
payload.isVisible
)

if (!success) {
// token is probably not in the core-side assets list
// try adding it to the user tokens list
store.dispatch(WalletActions.addUserAsset(payload.token))
}
}
)

Expand Down
13 changes: 10 additions & 3 deletions components/brave_wallet_ui/common/async/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ export function refreshVisibleTokenInfo(
const networkList = await getVisibleNetworksList(api)

async function inner(network: BraveWallet.NetworkInfo) {
const nativeAsset = makeNetworkAsset(network)

// Get a list of user tokens for each coinType and network.
const getTokenList = await braveWalletService.getUserAssets(
network.chainId,
Expand All @@ -207,7 +205,16 @@ export function refreshVisibleTokenInfo(
...token,
logo: `chrome://erc-token-images/${token.logo}`
})) as BraveWallet.BlockchainToken[]
return tokenList.length === 0 ? [nativeAsset] : tokenList

if (tokenList.length === 0) {
// user has hidden all tokens for the network
// we should still include the native asset, but as hidden
const nativeAsset = makeNetworkAsset(network)
nativeAsset.visible = false
return [nativeAsset]
}

return tokenList
}

const visibleAssets = targetNetwork
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ export function useAssetManagement() {

const addOrRemoveTokenInLocalStorage = React.useCallback(
(token: BraveWallet.BlockchainToken, addOrRemove: 'add' | 'remove') => {
if (token.contractAddress === '') {
// prevent permanently removing native tokens
return
}

const assetId = getAssetIdKey(token)
const isNFT = token.isNft || token.isErc1155 || token.isErc721
const removedList = isNFT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,20 @@ export const tokenEndpoints = ({
token,
isVisible
)

if (!success) {
// token is probably not in the core-side assets list,
// try adding it to the list
const { success: addTokenSuccess } = await addUserToken({
braveWalletService,
cache,
tokenArg: token
})
if (!addTokenSuccess) {
throw new Error('Token could not be updated or added')
}
}

return { data: success }
} catch (error) {
return handleEndpointError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export const AccountListItem = ({

const reducedAmounts = amounts.reduce(function (a, b) {
return a.plus(b)
})
}, Amount.empty())

return !reducedAmounts.isUndefined() ? reducedAmounts : Amount.empty()
}, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,15 @@ export const EditVisibleAssetsModal = ({ onClose }: Props) => {

// Filtered token list based on user removed tokens
const filteredOutRemovedTokens = React.useMemo(() => {
return tokenList.filter(
(token) =>
!removedTokensList.some(
(t) =>
t.contractAddress.toLowerCase() ===
token.contractAddress.toLowerCase() && t.tokenId === token.tokenId
)
)
return tokenList.filter((token) => {
const tokenContractLower = token.contractAddress.toLowerCase()
return !removedTokensList.some(
(t) =>
t.chainId === token.chainId &&
t.contractAddress.toLowerCase() === tokenContractLower &&
t.tokenId === token.tokenId
)
})
}, [tokenList, removedTokensList])

// Filtered token list based on search value
Expand Down Expand Up @@ -331,10 +332,12 @@ export const EditVisibleAssetsModal = ({ onClose }: Props) => {

const onRemoveAsset = React.useCallback(
(token: BraveWallet.BlockchainToken) => {
const tokenContractLower = token.contractAddress.toLowerCase()
const filterFn = (t: BraveWallet.BlockchainToken) =>
!(
t.contractAddress.toLowerCase() ===
token.contractAddress.toLowerCase() && t.tokenId === token.tokenId
t.chainId === token.chainId &&
t.contractAddress.toLowerCase() === tokenContractLower &&
t.tokenId === token.tokenId
)
const newUserList = updatedTokensList.filter(filterFn)
setUpdatedTokensList(newUserList)
Expand Down
Loading