/* eslint-disable eslint-comments/disable-enable-pair */ /* eslint-disable no-nested-ternary */ import { FormControl } from 'components/FormControl' import { FormGroup } from 'components/FormGroup' import { useInputState, useNumberInputState } from 'components/forms/FormInput.hooks' import { InputDateTime } from 'components/InputDateTime' import { openEditionMinterList } from 'config/minter' import type { TokenInfo } from 'config/token' import { stars, tokensList } from 'config/token' import { useGlobalSettings } from 'contexts/globalSettings' import React, { useEffect, useState } from 'react' import { resolveAddress } from 'utils/resolveAddress' import { useWallet } from 'utils/wallet' import { NumberInput, TextInput } from '../forms/FormInput' import type { UploadMethod } from './OffChainMetadataUploadDetails' interface MintingDetailsProps { onChange: (data: MintingDetailsDataProps) => void uploadMethod: UploadMethod minimumMintPrice: number mintTokenFromFactory?: TokenInfo | undefined importedMintingDetails?: MintingDetailsDataProps } export interface MintingDetailsDataProps { unitPrice: string perAddressLimit: number startTime: string endTime: string paymentAddress?: string selectedMintToken?: TokenInfo } export const MintingDetails = ({ onChange, uploadMethod, minimumMintPrice, mintTokenFromFactory, importedMintingDetails, }: MintingDetailsProps) => { const wallet = useWallet() const [timestamp, setTimestamp] = useState() const [endTimestamp, setEndTimestamp] = useState() const [selectedMintToken, setSelectedMintToken] = useState(stars) const [mintingDetailsImported, setMintingDetailsImported] = useState(false) const { timezone } = useGlobalSettings() const unitPriceState = useNumberInputState({ id: 'unitPrice', name: 'unitPrice', title: 'Mint Price', subtitle: `Price of each token (min. ${minimumMintPrice} ${ mintTokenFromFactory ? mintTokenFromFactory.displayName : 'STARS' })`, placeholder: '50', }) const perAddressLimitState = useNumberInputState({ id: 'peraddresslimit', name: 'peraddresslimit', title: 'Per Address Limit', subtitle: '', placeholder: '1', }) const paymentAddressState = useInputState({ id: 'payment-address', name: 'paymentAddress', title: 'Payment Address (optional)', subtitle: 'Address to receive minting revenues (defaults to current wallet address)', placeholder: 'stars1234567890abcdefghijklmnopqrstuvwxyz...', }) const resolvePaymentAddress = async () => { await resolveAddress(paymentAddressState.value.trim(), wallet).then((resolvedAddress) => { paymentAddressState.onChange(resolvedAddress) }) } useEffect(() => { if (!importedMintingDetails || (importedMintingDetails && mintingDetailsImported)) { void resolvePaymentAddress() } }, [paymentAddressState.value]) useEffect(() => { const data: MintingDetailsDataProps = { unitPrice: unitPriceState.value ? (Number(unitPriceState.value) * 1_000_000).toString() : unitPriceState.value === 0 ? '0' : '', perAddressLimit: perAddressLimitState.value, startTime: timestamp ? (timestamp.getTime() * 1_000_000).toString() : '', endTime: endTimestamp ? (endTimestamp.getTime() * 1_000_000).toString() : '', paymentAddress: paymentAddressState.value.trim(), selectedMintToken, } onChange(data) // eslint-disable-next-line react-hooks/exhaustive-deps }, [ unitPriceState.value, perAddressLimitState.value, timestamp, endTimestamp, paymentAddressState.value, selectedMintToken, ]) useEffect(() => { if (importedMintingDetails) { console.log('Selected Token ID: ', importedMintingDetails.selectedMintToken?.id) unitPriceState.onChange(Number(importedMintingDetails.unitPrice) / 1000000) perAddressLimitState.onChange(importedMintingDetails.perAddressLimit) setTimestamp(new Date(Number(importedMintingDetails.startTime) / 1_000_000)) setEndTimestamp(new Date(Number(importedMintingDetails.endTime) / 1_000_000)) paymentAddressState.onChange(importedMintingDetails.paymentAddress ? importedMintingDetails.paymentAddress : '') setSelectedMintToken(tokensList.find((token) => token.id === importedMintingDetails.selectedMintToken?.id)) setMintingDetailsImported(true) } // eslint-disable-next-line react-hooks/exhaustive-deps }, [importedMintingDetails]) return (
setTimestamp( timezone === 'Local' ? date : new Date(date.getTime() - new Date().getTimezoneOffset() * 60 * 1000), ) } value={ timezone === 'Local' ? timestamp : timestamp ? new Date(timestamp.getTime() + new Date().getTimezoneOffset() * 60 * 1000) : undefined } /> setEndTimestamp( timezone === 'Local' ? date : new Date(date.getTime() - new Date().getTimezoneOffset() * 60 * 1000), ) } value={ timezone === 'Local' ? endTimestamp : endTimestamp ? new Date(endTimestamp.getTime() + new Date().getTimezoneOffset() * 60 * 1000) : undefined } />
) }