import React, { useState } from "react"; import { useForm } from "react-hook-form"; import { Stack, Switch, Text, FormControl, FormLabel, Input, FormHelperText, FormErrorMessage, Icon, ButtonGroup, Button, Flex, Link, } from "@chakra-ui/react"; import * as yup from "yup"; import { yupResolver } from "@hookform/resolvers/yup"; import { RiErrorWarningFill } from "react-icons/ri"; import { NATIVE_MINT } from "@solana/spl-token"; import { FormControlWithError } from "../form/FormControlWithError"; import { MintSelect } from "../form/MintSelect"; import { usePublicKey } from "@strata-foundation/react"; import { routes } from "../../routes"; export interface ITokenFormValues { isExisting: boolean; type: "native" | "token"; amount: number; mint: string; startingPrice?: number; legalDisclosure?: boolean; } export interface ITokenFormProps { defaultValues: any; onSubmit: (data: any) => void; onBack: () => void; } export const validationSchema = (isExisting: boolean) => { return yup .object({ amount: yup.number().required().min(0), ...(isExisting ? { mint: yup.string().required(), } : { startingPrice: yup.number().required().min(0), legalDisclosure: yup.bool().oneOf([true], "Field must be checked"), }), }) .required(); }; export const TokenForm: React.FC = ({ onSubmit, onBack, defaultValues, }) => { const [innerIsExisting, setInnerIsExisting] = useState( defaultValues.isExisting ); const { handleSubmit, setValue, register, getValues, clearErrors, setError, watch, formState: { errors }, } = useForm({ mode: "onChange", resolver: yupResolver(validationSchema(innerIsExisting)), defaultValues, }); const { mint, amount, isExisting } = watch(); const mintKey = usePublicKey(mint); const inputBg = { bg: "gray.200", _dark: { bg: "gray.800" } }; const helpTextColor = { color: "black", _dark: { color: "gray.400" } }; const clearValues = () => { const { isExisting, ...values } = getValues(); Object.keys(values).forEach((field) => setValue(field as any, null)); }; const handleToggleExisting = (e: React.ChangeEvent) => { clearValues(); clearErrors(); setValue("isExisting", e.target.checked); setInnerIsExisting(e.target.checked); }; const handleMintChange = (mint: string) => { setValue("mint", mint); if (mint) { clearErrors("mint"); } else { setError("mint", { message: "Mint is a required field" }); } }; const handleOnSubmit = (data: any) => { onSubmit({ type: isExisting && mintKey?.equals(NATIVE_MINT) ? "native" : "token", ...data, }); }; return (
Use existing token {isExisting ? ( {isExisting ? "Mint" : "Purchase Mint"} {!errors.mint?.message ? ( {isExisting ? "The mint of the existing token to use for this permission." : "The mint that should be used to purchase this token."}  If you want users using SOL, use  handleMintChange(NATIVE_MINT.toString())} > {NATIVE_MINT.toString()} ) : ( {errors.mint.message}. If you want users using SOL, use  handleMintChange(NATIVE_MINT.toString())} > {NATIVE_MINT.toString()} )} ) : ( <> We'll create a token for you thats bonded to SOL, via a stable curve, with 5% buy royalties. If you want a more advanced token/curve please{" "} launch one. )} {!isExisting && ( I have read and agree to the{" "} strata.im Terms of Service {errors.legalDisclosure?.message && ( {errors.legalDisclosure.message} )} )}
); };