mirror of
https://github.com/luxfi/market.git
synced 2026-09-24 00:25:57 +00:00
101 lines
2.7 KiB
TypeScript
101 lines
2.7 KiB
TypeScript
import { BidModal } from '@reservoir0x/reservoir-kit-ui'
|
|
import { Button } from 'components/primitives'
|
|
import { cloneElement, ComponentProps, FC, useContext } from 'react'
|
|
import { CSS } from '@stitches/react'
|
|
import { SWRResponse } from 'swr'
|
|
import { useAccount, useNetwork, useSigner, useSwitchNetwork } from 'wagmi'
|
|
import { useConnectModal } from '@rainbow-me/rainbowkit'
|
|
import { ToastContext } from 'context/ToastContextProvider'
|
|
import { useMarketplaceChain } from 'hooks'
|
|
|
|
type Props = {
|
|
tokenId?: string | undefined
|
|
collectionId?: string | undefined
|
|
disabled?: boolean
|
|
openState?: [boolean, React.Dispatch<React.SetStateAction<boolean>>]
|
|
buttonCss?: CSS
|
|
buttonProps?: ComponentProps<typeof Button>
|
|
mutate?: SWRResponse['mutate']
|
|
}
|
|
|
|
const Bid: FC<Props> = ({
|
|
tokenId,
|
|
collectionId,
|
|
disabled,
|
|
openState,
|
|
buttonCss,
|
|
buttonProps,
|
|
mutate,
|
|
}) => {
|
|
const { isDisconnected } = useAccount()
|
|
const { openConnectModal } = useConnectModal()
|
|
const { addToast } = useContext(ToastContext)
|
|
const marketplaceChain = useMarketplaceChain()
|
|
const { switchNetworkAsync } = useSwitchNetwork({
|
|
chainId: marketplaceChain.id,
|
|
})
|
|
|
|
const { data: signer } = useSigner()
|
|
const { chain: activeChain } = useNetwork()
|
|
|
|
const isInTheWrongNetwork = Boolean(
|
|
signer && marketplaceChain.id !== activeChain?.id
|
|
)
|
|
|
|
const trigger = (
|
|
<Button css={buttonCss} disabled={disabled} {...buttonProps} color="gray3">
|
|
Make Offer
|
|
</Button>
|
|
)
|
|
|
|
if (isDisconnected || isInTheWrongNetwork) {
|
|
return cloneElement(trigger, {
|
|
onClick: async () => {
|
|
if (switchNetworkAsync && activeChain) {
|
|
const chain = await switchNetworkAsync(marketplaceChain.id)
|
|
if (chain.id !== marketplaceChain.id) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
if (!signer) {
|
|
openConnectModal?.()
|
|
}
|
|
},
|
|
})
|
|
} else
|
|
return (
|
|
<BidModal
|
|
tokenId={tokenId}
|
|
collectionId={collectionId}
|
|
trigger={trigger}
|
|
openState={openState}
|
|
onBidComplete={() => {
|
|
if (mutate) {
|
|
mutate()
|
|
}
|
|
}}
|
|
onBidError={(error) => {
|
|
if (error) {
|
|
if (
|
|
(error as any).cause.code &&
|
|
(error as any).cause.code === 4001
|
|
) {
|
|
addToast?.({
|
|
title: 'User canceled transaction',
|
|
description: 'You have canceled the transaction.',
|
|
})
|
|
return
|
|
}
|
|
}
|
|
addToast?.({
|
|
title: 'Could not place bid',
|
|
description: 'The transaction was not completed.',
|
|
})
|
|
}}
|
|
/>
|
|
)
|
|
}
|
|
|
|
export default Bid
|