import React, { ComponentProps, FC, useContext, useState } from 'react' import { useAccount } from 'wagmi' import { useCart } from '@reservoir0x/reservoir-kit-ui' import { Button } from 'components/primitives' import { useConnectModal } from '@rainbow-me/rainbowkit' import { CSS } from '@stitches/react' import { useMarketplaceChain } from 'hooks' import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' import { faMinus, faShoppingCart } from '@fortawesome/free-solid-svg-icons' import { ToastContext } from 'context/ToastContextProvider' import { ConfirmationModal } from 'components/common/ConfirmationModal' type Props = { token?: Parameters['add']>[0][0] orderId?: string buttonCss?: CSS buttonProps?: ComponentProps } const AddToCart: FC = ({ token, orderId = undefined, buttonCss, buttonProps, }) => { const { addToast } = useContext(ToastContext) const { data: items, add, remove, clear } = useCart((cart) => cart.items) const { data: cartChain } = useCart((cart) => cart.chain) const { isConnected } = useAccount() const { openConnectModal } = useConnectModal() const marketplaceChain = useMarketplaceChain() const [confirmationOpen, setConfirmationOpen] = useState(false) const hasOrderId = !!orderId if (hasOrderId) { const orderIsInCart = items.find((item) => item.order?.id === orderId) return ( <> { if (confirmed) { clear() add([{ orderId: orderId }], marketplaceChain.id) } }} /> ) } if (!token || (!('market' in token) && !('id' in token))) { return null } if ( 'market' in token && (token?.market?.floorAsk?.price?.amount === null || token?.market?.floorAsk?.price?.amount === undefined) ) { return null } let tokenKey = '' if ('id' in token) { tokenKey = token.id } else { tokenKey = `${token.token?.collection?.id}:${token.token?.tokenId}` } const isInCart = items.find( (item) => `${item.collection.id}:${item.token.id}` === tokenKey ) return ( <> { if (confirmed) { clear() add([token], marketplaceChain.id) } }} /> ) } export default AddToCart