Clean up whitelist file contents prior to collection creation

This commit is contained in:
Serkan Reis 2022-11-04 14:53:11 +03:00
parent a29cd50886
commit 2579e449be
3 changed files with 25 additions and 9 deletions

View File

@ -2,6 +2,8 @@ import clsx from 'clsx'
import React from 'react'
import { toast } from 'react-hot-toast'
import { isValidAddress } from '../utils/isValidAddress'
interface WhitelistUploadProps {
onChange: (data: string[]) => void
}
@ -9,16 +11,30 @@ interface WhitelistUploadProps {
export const WhitelistUpload = ({ onChange }: WhitelistUploadProps) => {
const onFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
if (!event.target.files) return toast.error('Error opening file')
if (event.target.files[0].type !== 'text/plain') return toast.error('Invalid file type')
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (event.target.files[0]?.type !== 'text/plain') {
toast.error('Invalid file type')
return onChange([])
}
if (event.target.files.length === 0) {
toast.error('No file selected')
return onChange([])
}
const reader = new FileReader()
reader.onload = (e: ProgressEvent<FileReader>) => {
const text = e.target?.result?.toString()
let newline = '\n'
if (text?.includes('\r')) newline = '\r'
if (text?.includes('\r\n')) newline = '\r\n'
const data = text?.split(newline)
return onChange([...new Set(data?.filter((address) => address !== '') || [])])
const cleanText = text?.toLowerCase().replace(/,/g, '').replace(/"/g, '').replace(/'/g, '').replace(/ /g, '')
const data = cleanText?.split(newline)
return onChange([
...new Set(
data?.filter((address) => address !== '' && isValidAddress(address) && address.startsWith('stars')) || [],
),
])
}
reader.readAsText(event.target.files[0])
}
@ -37,7 +53,7 @@ export const WhitelistUpload = ({ onChange }: WhitelistUploadProps) => {
'before:absolute before:inset-0 before:hover:bg-white/5 before:transition',
)}
id="whitelist-file"
multiple
multiple={false}
onChange={onFileChange}
type="file"
/>

View File

@ -39,7 +39,7 @@ export const WhitelistDetails = ({ onChange }: WhitelistDetailsProps) => {
defaultValue: '',
})
const uniPriceState = useNumberInputState({
const unitPriceState = useNumberInputState({
id: 'unit-price',
name: 'unitPrice',
title: 'Unit Price',
@ -72,7 +72,7 @@ export const WhitelistDetails = ({ onChange }: WhitelistDetailsProps) => {
whitelistType: whitelistState,
contractAddress: whitelistAddressState.value,
members: whitelistArray,
unitPrice: uniPriceState.value ? (Number(uniPriceState.value) * 1_000_000).toString() : '',
unitPrice: unitPriceState.value ? (Number(unitPriceState.value) * 1_000_000).toString() : '',
startTime: startDate ? (startDate.getTime() * 1_000_000).toString() : '',
endTime: endDate ? (endDate.getTime() * 1_000_000).toString() : '',
perAddressLimit: perAddressLimitState.value,
@ -82,7 +82,7 @@ export const WhitelistDetails = ({ onChange }: WhitelistDetailsProps) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
whitelistAddressState.value,
uniPriceState.value,
unitPriceState.value,
memberLimitState.value,
perAddressLimitState.value,
startDate,
@ -160,7 +160,7 @@ export const WhitelistDetails = ({ onChange }: WhitelistDetailsProps) => {
<Conditional test={whitelistState === 'new'}>
<div className="grid grid-cols-2">
<FormGroup subtitle="Information about your minting settings" title="Whitelist Minting Details">
<NumberInput isRequired {...uniPriceState} />
<NumberInput isRequired {...unitPriceState} />
<NumberInput isRequired {...memberLimitState} />
<NumberInput isRequired {...perAddressLimitState} />
<FormControl

View File

@ -1,6 +1,6 @@
{
"name": "stargaze-studio",
"version": "0.2.4",
"version": "0.2.5",
"workspaces": [
"packages/*"
],