import { Button, Flex, FormControl, FormHelperText, FormLabel, HStack, Icon, Image, Input, Stack, Text, Textarea, useRadioGroup, } from "@chakra-ui/react"; import { StorageProvider } from "@strata-foundation/spl-utils"; import React, { useEffect, useState } from "react"; import { useFormContext } from "react-hook-form"; import { RiCheckFill } from "react-icons/ri"; import { FormControlWithError } from "./FormControlWithError"; import { RadioCardWithAffordance } from "./RadioCard"; export interface IMetadataFormProps { image: File; name: string; description: string; provider: StorageProvider; } export function TokenMetadataInputs({ entityName = "post", }: { entityName?: string; }) { const { register, watch, formState: { errors }, clearErrors, setValue, setError, } = useFormContext(); const { image } = watch(); const [imgUrl, setImgUrl] = useState(); const hiddenFileInput = React.useRef(null); const { getRootProps, getRadioProps } = useRadioGroup({ name: "provider", onChange: (option) => setValue("provider", option as StorageProvider), }); const group = getRootProps(); const storageOptions = [ { value: StorageProvider.Arweave, heading: "Arweave", illustration: "/arweave.png", helpText: "Instant storage for your image and description, but you will need to pay a small fee in Sol for the upload", }, ]; useEffect(() => { if (image) { const reader = new FileReader(); reader.onload = (event) => { setImgUrl((event.target?.result as string) || ""); }; reader.readAsDataURL(image); } else { setImgUrl(undefined); } }, [image]); // Default to Arweave useEffect(() => { setValue("provider", StorageProvider.Arweave); }, [setValue]); const provider = watch("provider"); const handleImageChange = (e: React.ChangeEvent) => { const file = e.target.files![0]; const sizeKB = file.size / 1024; if (sizeKB < 25) { // @ts-ignore setError("image", { type: "manual", message: `The file ${file.name} is too small. It is ${ Math.round(10 * sizeKB) / 10 }KB but should be at least 25KB.`, }); return; } // @ts-ignore setValue("image", file || null); // @ts-ignore clearErrors("image"); }; return ( <> Photo {image && ( {image.name} {image.name} )} {errors.image?.message || `The image that will be displayed with this ${entityName}`} {/* {storageOptions.map(({ value, heading, illustration, helpText }) => { const radio = getRadioProps({ value }); return ( {`${value}-illustration`} {heading} {helpText} ); })} */}