|
| 1 | +// Copyright (c) 2022 The Brave Authors. All rights reserved. |
| 2 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | +// License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 4 | +// you can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | +import * as React from 'react' |
| 6 | + |
| 7 | +// types |
| 8 | +import { TabNavTypes } from '../../../constants/types' |
| 9 | + |
| 10 | +// options |
| 11 | +import { CUSTOM_ASSET_NAV_OPTIONS } from '../../../options/add-custom-asset-nav-options' |
| 12 | + |
| 13 | +// components |
| 14 | +import { TopTabNav } from '../index' |
| 15 | +import { AddCustomTokenForm } from '../../shared/add-custom-token-form/add-custom-token-form' |
| 16 | +import { AddNftForm } from '../../shared/add-custom-token-form/add-nft-form' |
| 17 | + |
| 18 | +// styles |
| 19 | +import { AddAssetWrapper } from './add-asset.styles' |
| 20 | + |
| 21 | +interface Props { |
| 22 | + contractAddress: string | undefined |
| 23 | + onHideForm: () => void |
| 24 | +} |
| 25 | + |
| 26 | +export const AddAsset = (props: Props) => { |
| 27 | + const { |
| 28 | + contractAddress, |
| 29 | + onHideForm |
| 30 | + } = props |
| 31 | + const [tokenContractAddress, setTokenContractAddress] = React.useState<string>(contractAddress || '') |
| 32 | + const [selectedTab, setSelectedTab] = React.useState<TabNavTypes>('token') |
| 33 | + |
| 34 | + const onSelectTab = React.useCallback((id: TabNavTypes) => { |
| 35 | + // Reset contractAddress when a user switches tabs |
| 36 | + // This will reset the form to avoid the tabs being auto selected based |
| 37 | + // on found token type |
| 38 | + if (tokenContractAddress !== '') setTokenContractAddress('') |
| 39 | + setSelectedTab(id) |
| 40 | + }, [tokenContractAddress]) |
| 41 | + |
| 42 | + const onNftAssetFound = React.useCallback((contractAddress: string) => { |
| 43 | + setTokenContractAddress(contractAddress) |
| 44 | + setSelectedTab('nft') |
| 45 | + }, []) |
| 46 | + |
| 47 | + const onTokenFound = React.useCallback((contractAddress: string) => { |
| 48 | + setTokenContractAddress(contractAddress) |
| 49 | + setSelectedTab('token') |
| 50 | + }, []) |
| 51 | + |
| 52 | + const onChangeContractAddress = React.useCallback((contractAddress: string) => { |
| 53 | + setTokenContractAddress(contractAddress) |
| 54 | + }, []) |
| 55 | + |
| 56 | + return ( |
| 57 | + <AddAssetWrapper> |
| 58 | + <TopTabNav |
| 59 | + tabList={CUSTOM_ASSET_NAV_OPTIONS} |
| 60 | + selectedTab={selectedTab} |
| 61 | + onSelectTab={onSelectTab} |
| 62 | + /> |
| 63 | + |
| 64 | + {selectedTab === 'token' |
| 65 | + ? <AddCustomTokenForm |
| 66 | + contractAddress={tokenContractAddress} |
| 67 | + onHideForm={onHideForm} |
| 68 | + onNftAssetFound={onNftAssetFound} |
| 69 | + onChangeContractAddress={onChangeContractAddress} |
| 70 | + /> |
| 71 | + : <AddNftForm |
| 72 | + contractAddress={tokenContractAddress} |
| 73 | + onHideForm={onHideForm} |
| 74 | + onTokenFound={onTokenFound} |
| 75 | + onChangeContractAddress={onChangeContractAddress} |
| 76 | + /> |
| 77 | + } |
| 78 | + </AddAssetWrapper> |
| 79 | + ) |
| 80 | +} |
0 commit comments