/* eslint-disable eslint-comments/disable-enable-pair */ /* eslint-disable no-nested-ternary */ import { FormControl } from 'components/FormControl' import { FormGroup } from 'components/FormGroup' import { AddressList } from 'components/forms/AddressList' import { useAddressListState } from 'components/forms/AddressList.hooks' import { useInputState, useNumberInputState } from 'components/forms/FormInput.hooks' import { InputDateTime } from 'components/InputDateTime' import type { WhitelistFlexMember } from 'components/WhitelistFlexUpload' import { WhitelistFlexUpload } from 'components/WhitelistFlexUpload' import type { TokenInfo } from 'config/token' import { useGlobalSettings } from 'contexts/globalSettings' import React, { useEffect, useState } from 'react' import { isValidAddress } from 'utils/isValidAddress' import { useWallet } from 'utils/wallet' import { Conditional } from '../../Conditional' import { AddressInput, NumberInput } from '../../forms/FormInput' import { JsonPreview } from '../../JsonPreview' import { WhitelistUpload } from '../../WhitelistUpload' interface WhitelistDetailsProps { onChange: (data: WhitelistDetailsDataProps) => void mintingTokenFromFactory?: TokenInfo importedWhitelistDetails?: WhitelistDetailsDataProps } export interface WhitelistDetailsDataProps { whitelistState: WhitelistState whitelistType: WhitelistType contractAddress?: string members?: string[] | WhitelistFlexMember[] unitPrice?: string startTime?: string endTime?: string perAddressLimit?: number memberLimit?: number admins?: string[] adminsMutable?: boolean } type WhitelistState = 'none' | 'existing' | 'new' type WhitelistType = 'standard' | 'flex' export const WhitelistDetails = ({ onChange, mintingTokenFromFactory, importedWhitelistDetails, }: WhitelistDetailsProps) => { const wallet = useWallet() const { timezone } = useGlobalSettings() const [whitelistState, setWhitelistState] = useState('none') const [whitelistType, setWhitelistType] = useState('standard') const [startDate, setStartDate] = useState(undefined) const [endDate, setEndDate] = useState(undefined) const [whitelistStandardArray, setWhitelistStandardArray] = useState([]) const [whitelistFlexArray, setWhitelistFlexArray] = useState([]) const [adminsMutable, setAdminsMutable] = useState(true) const whitelistAddressState = useInputState({ id: 'whitelist-address', name: 'whitelistAddress', title: 'Whitelist Address', defaultValue: '', }) const unitPriceState = useNumberInputState({ id: 'unit-price', name: 'unitPrice', title: 'Unit Price', subtitle: `Token price for whitelisted addresses \n (min. 0 ${ mintingTokenFromFactory ? mintingTokenFromFactory.displayName : 'STARS' })`, placeholder: '25', }) const memberLimitState = useNumberInputState({ id: 'member-limit', name: 'memberLimit', title: 'Member Limit', subtitle: 'Maximum number of whitelisted addresses', placeholder: '1000', }) const perAddressLimitState = useNumberInputState({ id: 'per-address-limit', name: 'perAddressLimit', title: 'Per Address Limit', subtitle: 'Maximum number of tokens per whitelisted address', placeholder: '5', }) const addressListState = useAddressListState() const whitelistFileOnChange = (data: string[]) => { setWhitelistStandardArray(data) } const whitelistFlexFileOnChange = (whitelistData: WhitelistFlexMember[]) => { setWhitelistFlexArray(whitelistData) } useEffect(() => { if (!importedWhitelistDetails) { setWhitelistStandardArray([]) setWhitelistFlexArray([]) } }, [whitelistType]) useEffect(() => { const data: WhitelistDetailsDataProps = { whitelistState, whitelistType, contractAddress: whitelistAddressState.value .toLowerCase() .replace(/,/g, '') .replace(/"/g, '') .replace(/'/g, '') .replace(/ /g, ''), members: whitelistType === 'standard' ? whitelistStandardArray : whitelistFlexArray, unitPrice: unitPriceState.value ? (Number(unitPriceState.value) * 1_000_000).toString() : unitPriceState.value === 0 ? '0' : undefined, startTime: startDate ? (startDate.getTime() * 1_000_000).toString() : '', endTime: endDate ? (endDate.getTime() * 1_000_000).toString() : '', perAddressLimit: perAddressLimitState.value, memberLimit: memberLimitState.value, admins: [ ...new Set( addressListState.values .map((a) => a.address.trim()) .filter((address) => address !== '' && isValidAddress(address.trim()) && address.startsWith('stars')), ), ], adminsMutable, } onChange(data) // eslint-disable-next-line react-hooks/exhaustive-deps }, [ whitelistAddressState.value, unitPriceState.value, memberLimitState.value, perAddressLimitState.value, startDate, endDate, whitelistStandardArray, whitelistFlexArray, whitelistState, addressListState.values, adminsMutable, ]) // make the necessary changes with respect to imported whitelist details useEffect(() => { if (importedWhitelistDetails) { setWhitelistState(importedWhitelistDetails.whitelistState) setWhitelistType(importedWhitelistDetails.whitelistType) whitelistAddressState.onChange( importedWhitelistDetails.contractAddress ? importedWhitelistDetails.contractAddress : '', ) unitPriceState.onChange( importedWhitelistDetails.unitPrice ? Number(importedWhitelistDetails.unitPrice) / 1000000 : 0, ) memberLimitState.onChange(importedWhitelistDetails.memberLimit ? importedWhitelistDetails.memberLimit : 0) perAddressLimitState.onChange( importedWhitelistDetails.perAddressLimit ? importedWhitelistDetails.perAddressLimit : 0, ) setStartDate( importedWhitelistDetails.startTime ? new Date(Number(importedWhitelistDetails.startTime) / 1_000_000) : undefined, ) setEndDate( importedWhitelistDetails.endTime ? new Date(Number(importedWhitelistDetails.endTime) / 1_000_000) : undefined, ) setAdminsMutable(importedWhitelistDetails.adminsMutable ? importedWhitelistDetails.adminsMutable : true) importedWhitelistDetails.admins?.forEach((admin) => { addressListState.reset() addressListState.add({ address: admin }) }) if (importedWhitelistDetails.whitelistType === 'standard') { setWhitelistStandardArray([]) importedWhitelistDetails.members?.forEach((member) => { setWhitelistStandardArray((standardArray) => [...standardArray, member as string]) }) } else { setWhitelistFlexArray([]) importedWhitelistDetails.members?.forEach((member) => { setWhitelistFlexArray((flexArray) => [ ...flexArray, { address: (member as WhitelistFlexMember).address, mint_count: (member as WhitelistFlexMember).mint_count, }, ]) }) } } // eslint-disable-next-line react-hooks/exhaustive-deps }, [importedWhitelistDetails]) useEffect(() => { if (whitelistState === 'new' && wallet.address) { addressListState.reset() addressListState.add({ address: wallet.address }) } // eslint-disable-next-line react-hooks/exhaustive-deps }, [whitelistState, wallet.address]) return (
{ setWhitelistState('none') setWhitelistType('standard') }} type="radio" value="None" />
{ setWhitelistState('existing') }} type="radio" value="Existing" />
{ setWhitelistState('new') }} type="radio" value="New" />
{ setWhitelistType('standard') }} type="radio" value="nft-storage" />
{ setWhitelistType('flex') }} type="radio" value="flex" />
setStartDate( timezone === 'Local' ? date : new Date(date.getTime() - new Date().getTimezoneOffset() * 60 * 1000), ) } value={ timezone === 'Local' ? startDate : startDate ? new Date(startDate.getTime() + new Date().getTimezoneOffset() * 60 * 1000) : undefined } /> setEndDate( timezone === 'Local' ? date : new Date(date.getTime() - new Date().getTimezoneOffset() * 60 * 1000), ) } value={ timezone === 'Local' ? endDate : endDate ? new Date(endDate.getTime() + new Date().getTimezoneOffset() * 60 * 1000) : undefined } />
0}> 0}>
) }