|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { Flex, useDisclosure } from "@chakra-ui/react"; |
| 4 | +import { TransactionButton } from "components/buttons/TransactionButton"; |
| 5 | +import { |
| 6 | + type AirdropAddressInput, |
| 7 | + AirdropUpload, |
| 8 | +} from "contract-ui/tabs/nfts/components/airdrop-upload"; |
| 9 | +import { useTrack } from "hooks/analytics/useTrack"; |
| 10 | +import { useTxNotifications } from "hooks/useTxNotifications"; |
| 11 | +import { UploadIcon } from "lucide-react"; |
| 12 | +import { useForm } from "react-hook-form"; |
| 13 | +import type { ThirdwebContract } from "thirdweb"; |
| 14 | +import { batchMintAdditionalSupplyTo } from "thirdweb/extensions/erc1155"; |
| 15 | +import { useActiveAccount, useSendAndConfirmTransaction } from "thirdweb/react"; |
| 16 | +import { Button, Text } from "tw-components"; |
| 17 | + |
| 18 | +export default function BatchMintAdditionalSupplyTab({ |
| 19 | + contract, |
| 20 | + tokenId, |
| 21 | +}: { |
| 22 | + contract: ThirdwebContract; |
| 23 | + tokenId: string; |
| 24 | +}) { |
| 25 | + const address = useActiveAccount()?.address; |
| 26 | + const { handleSubmit, setValue, watch, reset, formState } = useForm<{ |
| 27 | + addresses: AirdropAddressInput[]; |
| 28 | + }>({ |
| 29 | + defaultValues: { addresses: [] }, |
| 30 | + }); |
| 31 | + const trackEvent = useTrack(); |
| 32 | + |
| 33 | + const { isOpen, onOpen, onClose } = useDisclosure(); |
| 34 | + |
| 35 | + const { mutate, isPending } = useSendAndConfirmTransaction(); |
| 36 | + |
| 37 | + const { onSuccess, onError } = useTxNotifications( |
| 38 | + "Batch mint successful", |
| 39 | + "Error batch mint", |
| 40 | + contract, |
| 41 | + ); |
| 42 | + |
| 43 | + const addresses = watch("addresses"); |
| 44 | + |
| 45 | + return ( |
| 46 | + <div className="flex w-full flex-col gap-2"> |
| 47 | + <form |
| 48 | + onSubmit={handleSubmit(async (_data) => { |
| 49 | + trackEvent({ |
| 50 | + category: "nft", |
| 51 | + action: "airdrop", |
| 52 | + label: "attempt", |
| 53 | + contract_address: contract.address, |
| 54 | + token_id: tokenId, |
| 55 | + }); |
| 56 | + const transaction = batchMintAdditionalSupplyTo({ |
| 57 | + contract, |
| 58 | + tokenId: BigInt(tokenId), |
| 59 | + content: _data.addresses.map((item) => ({ |
| 60 | + to: item.address, |
| 61 | + supply: BigInt(item.quantity), |
| 62 | + })), |
| 63 | + }); |
| 64 | + mutate(transaction, { |
| 65 | + onSuccess: () => { |
| 66 | + trackEvent({ |
| 67 | + category: "nft", |
| 68 | + action: "airdrop", |
| 69 | + label: "success", |
| 70 | + contract_address: contract.address, |
| 71 | + token_id: tokenId, |
| 72 | + }); |
| 73 | + onSuccess(); |
| 74 | + reset(); |
| 75 | + }, |
| 76 | + onError: (error) => { |
| 77 | + trackEvent({ |
| 78 | + category: "nft", |
| 79 | + action: "airdrop", |
| 80 | + label: "success", |
| 81 | + contract_address: contract.address, |
| 82 | + token_id: tokenId, |
| 83 | + error, |
| 84 | + }); |
| 85 | + onError(error); |
| 86 | + }, |
| 87 | + }); |
| 88 | + })} |
| 89 | + > |
| 90 | + <div className="flex flex-col gap-2"> |
| 91 | + <div className="mb-3 flex w-full flex-col gap-6 md:flex-row"> |
| 92 | + <AirdropUpload |
| 93 | + isOpen={isOpen} |
| 94 | + onClose={onClose} |
| 95 | + setAirdrop={(value) => |
| 96 | + setValue("addresses", value, { shouldDirty: true }) |
| 97 | + } |
| 98 | + /> |
| 99 | + <Flex direction={{ base: "column", md: "row" }} gap={4}> |
| 100 | + <Button |
| 101 | + colorScheme="primary" |
| 102 | + borderRadius="md" |
| 103 | + onClick={onOpen} |
| 104 | + rightIcon={<UploadIcon className="size-5" />} |
| 105 | + > |
| 106 | + Upload addresses |
| 107 | + </Button> |
| 108 | + |
| 109 | + <Flex |
| 110 | + gap={2} |
| 111 | + direction="row" |
| 112 | + align="center" |
| 113 | + justify="center" |
| 114 | + color={addresses.length === 0 ? "orange.500" : "green.500"} |
| 115 | + > |
| 116 | + {addresses.length > 0 && ( |
| 117 | + <Text size="body.sm" color="inherit"> |
| 118 | + ● <strong>{addresses.length} addresses</strong> ready to be |
| 119 | + minted |
| 120 | + </Text> |
| 121 | + )} |
| 122 | + </Flex> |
| 123 | + </Flex> |
| 124 | + </div> |
| 125 | + <Text> |
| 126 | + You can mint to a maximum of 250 addresses at a time. If you have |
| 127 | + more, please do it in multiple transactions. |
| 128 | + </Text> |
| 129 | + <TransactionButton |
| 130 | + txChainID={contract.chain.id} |
| 131 | + transactionCount={1} |
| 132 | + isLoading={isPending} |
| 133 | + type="submit" |
| 134 | + colorScheme="primary" |
| 135 | + disabled={!!address && addresses.length === 0} |
| 136 | + alignSelf="flex-end" |
| 137 | + isDisabled={!formState.isDirty} |
| 138 | + > |
| 139 | + Batch mint |
| 140 | + </TransactionButton> |
| 141 | + </div> |
| 142 | + </form> |
| 143 | + </div> |
| 144 | + ); |
| 145 | +} |
0 commit comments