import React, { useEffect } from 'react'; import PropTypes from 'prop-types'; import { useDispatch, useSelector } from 'react-redux'; import { useHistory } from 'react-router-dom'; import { getTokenTrackerLink } from '@metamask/etherscan-link'; import { isEqual } from 'lodash'; import Box from '../../ui/box'; import Card from '../../ui/card'; import Typography from '../../ui/typography/typography'; import { COLORS, TYPOGRAPHY, FONT_WEIGHT, JUSTIFY_CONTENT, FLEX_DIRECTION, OVERFLOW_WRAP, DISPLAY, BLOCK_SIZES, } from '../../../helpers/constants/design-system'; import { useI18nContext } from '../../../hooks/useI18nContext'; import { getAssetImageURL, isEqualCaseInsensitive, shortenAddress, } from '../../../helpers/utils/util'; import { getCurrentChainId, getIpfsGateway, getRpcPrefsForCurrentProvider, getSelectedIdentity, } from '../../../selectors'; import AssetNavigation from '../../../pages/asset/components/asset-navigation'; import { getCollectibleContracts } from '../../../ducks/metamask/metamask'; import { DEFAULT_ROUTE, SEND_ROUTE } from '../../../helpers/constants/routes'; import { checkAndUpdateSingleCollectibleOwnershipStatus, removeAndIgnoreCollectible, } from '../../../store/actions'; import { GOERLI_CHAIN_ID, KOVAN_CHAIN_ID, MAINNET_CHAIN_ID, POLYGON_CHAIN_ID, RINKEBY_CHAIN_ID, ROPSTEN_CHAIN_ID, } from '../../../../shared/constants/network'; import { getEnvironmentType } from '../../../../app/scripts/lib/util'; import { ENVIRONMENT_TYPE_POPUP } from '../../../../shared/constants/app'; import CollectibleOptions from '../collectible-options/collectible-options'; import Button from '../../ui/button'; import { ASSET_TYPES, updateSendAsset } from '../../../ducks/send'; import InfoTooltip from '../../ui/info-tooltip'; import { ERC721 } from '../../../helpers/constants/common'; import { usePrevious } from '../../../hooks/usePrevious'; export default function CollectibleDetails({ collectible }) { const { image, name, description, address, tokenId, standard, isCurrentlyOwned, } = collectible; const t = useI18nContext(); const history = useHistory(); const dispatch = useDispatch(); const rpcPrefs = useSelector(getRpcPrefsForCurrentProvider); const ipfsGateway = useSelector(getIpfsGateway); const collectibleContracts = useSelector(getCollectibleContracts); const currentNetwork = useSelector(getCurrentChainId); const collectibleContractName = collectibleContracts.find( ({ address: contractAddress }) => isEqualCaseInsensitive(contractAddress, address), )?.name; const selectedAccountName = useSelector( (state) => getSelectedIdentity(state).name, ); const collectibleImageURL = getAssetImageURL(image, ipfsGateway); const onRemove = () => { dispatch(removeAndIgnoreCollectible(address, tokenId)); history.push(DEFAULT_ROUTE); }; const prevCollectible = usePrevious(collectible); useEffect(() => { if (!isEqual(prevCollectible, collectible)) { checkAndUpdateSingleCollectibleOwnershipStatus(collectible); } }, [collectible, prevCollectible]); const getOpenSeaLink = () => { switch (currentNetwork) { case MAINNET_CHAIN_ID: return `https://opensea.io/assets/${address}/${tokenId}`; case POLYGON_CHAIN_ID: return `https://opensea.io/assets/matic/${address}/${tokenId}`; case GOERLI_CHAIN_ID: case KOVAN_CHAIN_ID: case ROPSTEN_CHAIN_ID: case RINKEBY_CHAIN_ID: return `https://testnets.opensea.io/assets/${address}/${tokenId}`; default: return null; } }; const openSeaLink = getOpenSeaLink(); const sendDisabled = standard !== ERC721; const inPopUp = getEnvironmentType() === ENVIRONMENT_TYPE_POPUP; const onSend = async () => { await dispatch( updateSendAsset({ type: ASSET_TYPES.COLLECTIBLE, details: collectible, }), ); history.push(SEND_ROUTE); }; const renderSendButton = () => { if (isCurrentlyOwned === false) { return
; } return ( {sendDisabled ? ( ) : null} ); }; return ( <> history.push(DEFAULT_ROUTE)} optionsButton={ global.platform.openTab({ url: openSeaLink }) : null } onRemove={onRemove} /> } />
{name} #{tokenId}
{t('description')} {description}
{inPopUp ? null : renderSendButton()}
{t('source')} {image} {t('contractAddress')} {getEnvironmentType() === ENVIRONMENT_TYPE_POPUP ? shortenAddress(address) : address} {inPopUp ? renderSendButton() : null}
); } CollectibleDetails.propTypes = { collectible: PropTypes.shape({ address: PropTypes.string.isRequired, tokenId: PropTypes.string.isRequired, isCurrentlyOwned: PropTypes.bool, name: PropTypes.string, description: PropTypes.string, image: PropTypes.string, standard: PropTypes.string, imageThumbnail: PropTypes.string, imagePreview: PropTypes.string, creator: PropTypes.shape({ address: PropTypes.string, config: PropTypes.string, profile_img_url: PropTypes.string, }), }), };