import clsx from 'clsx' import React from 'react' import { toast } from 'react-hot-toast' import { csvToArray } from 'utils/csvToArray' import type { AirdropAllocation } from 'utils/isValidAccountsFile' import { isValidAccountsFile } from 'utils/isValidAccountsFile' interface AirdropUploadProps { onChange: (data: AirdropAllocation[]) => void } export const AirdropUpload = ({ onChange }: AirdropUploadProps) => { const onFileChange = (event: React.ChangeEvent) => { if (!event.target.files) return toast.error('Error opening file') // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (!event.target.files[0]?.name.endsWith('.csv')) { toast.error('Please select a .csv file!') return onChange([]) } const reader = new FileReader() reader.onload = (e: ProgressEvent) => { try { if (!e.target?.result) return toast.error('Error parsing file.') // eslint-disable-next-line @typescript-eslint/no-base-to-string const accountsData = csvToArray(e.target.result.toString()) if (!isValidAccountsFile(accountsData)) { event.target.value = '' return onChange([]) } return onChange(accountsData) } catch (error: any) { toast.error(error.message) } } reader.readAsText(event.target.files[0]) } return (
) }