|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { |
| 4 | + Accordion, |
| 5 | + AccordionContent, |
| 6 | + AccordionItem, |
| 7 | + AccordionTrigger, |
| 8 | +} from "@/components/ui/accordion"; |
| 9 | +import { FormControl, Input, Textarea } from "@chakra-ui/react"; |
| 10 | +import { OpenSeaPropertyBadge } from "components/badges/opensea"; |
| 11 | +import { PropertiesFormControl } from "components/contract-pages/forms/properties.shared"; |
| 12 | +import { FileInput } from "components/shared/FileInput"; |
| 13 | +import { useImageFileOrUrl } from "hooks/useImageFileOrUrl"; |
| 14 | +import type { UseFormReturn } from "react-hook-form"; |
| 15 | +import { FormErrorMessage, FormHelperText, FormLabel } from "tw-components"; |
| 16 | +import type { MetadataUploadFormValues } from "./Mintable"; |
| 17 | + |
| 18 | +export function MetadataUpload(props: { |
| 19 | + form: UseFormReturn<MetadataUploadFormValues>; |
| 20 | +}) { |
| 21 | + const { |
| 22 | + setValue, |
| 23 | + control, |
| 24 | + register, |
| 25 | + watch, |
| 26 | + formState: { errors }, |
| 27 | + } = props.form; |
| 28 | + |
| 29 | + const setFile = (file: File) => { |
| 30 | + if (file.type.includes("image")) { |
| 31 | + // image files |
| 32 | + setValue("image", file); |
| 33 | + if (watch("external_url") instanceof File) { |
| 34 | + setValue("external_url", undefined); |
| 35 | + } |
| 36 | + if (watch("animation_url") instanceof File) { |
| 37 | + setValue("animation_url", undefined); |
| 38 | + } |
| 39 | + } else if ( |
| 40 | + ["audio", "video", "text/html", "model/*"].some((type: string) => |
| 41 | + file.type.includes(type), |
| 42 | + ) || |
| 43 | + file.name?.endsWith(".glb") || |
| 44 | + file.name?.endsWith(".usdz") || |
| 45 | + file.name?.endsWith(".gltf") || |
| 46 | + file.name.endsWith(".obj") |
| 47 | + ) { |
| 48 | + // audio, video, html, and glb (3d) files |
| 49 | + setValue("animation_url", file); |
| 50 | + if (watch("external_url") instanceof File) { |
| 51 | + setValue("external_url", undefined); |
| 52 | + } |
| 53 | + } else if ( |
| 54 | + ["text", "application/pdf"].some((type: string) => |
| 55 | + file.type?.includes(type), |
| 56 | + ) |
| 57 | + ) { |
| 58 | + // text and pdf files |
| 59 | + setValue("external_url", file); |
| 60 | + if (watch("animation_url") instanceof File) { |
| 61 | + setValue("animation_url", undefined); |
| 62 | + } |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + const imageUrl = useImageFileOrUrl(watch("image") as File | string); |
| 67 | + const mediaFileUrl = |
| 68 | + watch("animation_url") instanceof File |
| 69 | + ? watch("animation_url") |
| 70 | + : watch("external_url") instanceof File |
| 71 | + ? watch("external_url") |
| 72 | + : watch("image") instanceof File |
| 73 | + ? imageUrl |
| 74 | + : undefined; |
| 75 | + |
| 76 | + const mediaFileError = |
| 77 | + watch("animation_url") instanceof File |
| 78 | + ? errors?.animation_url |
| 79 | + : watch("external_url") instanceof File |
| 80 | + ? errors?.external_url |
| 81 | + : watch("image") instanceof File |
| 82 | + ? errors?.image |
| 83 | + : undefined; |
| 84 | + |
| 85 | + const externalUrl = watch("external_url"); |
| 86 | + const externalIsTextFile = |
| 87 | + externalUrl instanceof File && |
| 88 | + (externalUrl.type.includes("text") || externalUrl.type.includes("pdf")); |
| 89 | + |
| 90 | + const showCoverImageUpload = |
| 91 | + watch("animation_url") instanceof File || |
| 92 | + watch("external_url") instanceof File; |
| 93 | + |
| 94 | + return ( |
| 95 | + <div className="flex flex-col gap-6"> |
| 96 | + <FormControl isRequired isInvalid={!!errors.name}> |
| 97 | + <FormLabel>Name</FormLabel> |
| 98 | + <Input autoFocus {...register("name")} /> |
| 99 | + <FormErrorMessage>{errors?.name?.message}</FormErrorMessage> |
| 100 | + </FormControl> |
| 101 | + <FormControl isInvalid={!!mediaFileError}> |
| 102 | + <FormLabel>Media</FormLabel> |
| 103 | + <div> |
| 104 | + <FileInput |
| 105 | + previewMaxWidth="200px" |
| 106 | + value={mediaFileUrl as File | string} |
| 107 | + showUploadButton |
| 108 | + showPreview={true} |
| 109 | + setValue={setFile} |
| 110 | + className="rounded border border-border transition-all duration-200" |
| 111 | + selectOrUpload="Upload" |
| 112 | + helperText="Media" |
| 113 | + /> |
| 114 | + </div> |
| 115 | + <FormHelperText> |
| 116 | + You can upload image, audio, video, html, text, pdf, and 3d model |
| 117 | + files here. |
| 118 | + </FormHelperText> |
| 119 | + <FormErrorMessage> |
| 120 | + {mediaFileError?.message as unknown as string} |
| 121 | + </FormErrorMessage> |
| 122 | + </FormControl> |
| 123 | + {showCoverImageUpload && ( |
| 124 | + <FormControl isInvalid={!!errors.image}> |
| 125 | + <FormLabel>Cover Image</FormLabel> |
| 126 | + <FileInput |
| 127 | + previewMaxWidth="200px" |
| 128 | + accept={{ "image/*": [] }} |
| 129 | + value={imageUrl} |
| 130 | + showUploadButton |
| 131 | + setValue={(file) => setValue("image", file)} |
| 132 | + className="rounded border border-border transition-all" |
| 133 | + /> |
| 134 | + <FormHelperText> |
| 135 | + You can optionally upload an image as the cover of your NFT. |
| 136 | + </FormHelperText> |
| 137 | + <FormErrorMessage> |
| 138 | + {errors?.image?.message as unknown as string} |
| 139 | + </FormErrorMessage> |
| 140 | + </FormControl> |
| 141 | + )} |
| 142 | + <FormControl isInvalid={!!errors.description}> |
| 143 | + <FormLabel>Description</FormLabel> |
| 144 | + <Textarea {...register("description")} /> |
| 145 | + <FormErrorMessage>{errors?.description?.message}</FormErrorMessage> |
| 146 | + </FormControl> |
| 147 | + <PropertiesFormControl |
| 148 | + watch={watch} |
| 149 | + // biome-ignore lint/suspicious/noExplicitAny: FIXME |
| 150 | + errors={errors as any} |
| 151 | + control={control} |
| 152 | + register={register} |
| 153 | + setValue={setValue} |
| 154 | + /> |
| 155 | + <Accordion |
| 156 | + type="single" |
| 157 | + collapsible={!(errors.background_color || errors.external_url)} |
| 158 | + > |
| 159 | + <AccordionItem value="advanced-options"> |
| 160 | + <AccordionTrigger className="justify-between"> |
| 161 | + Advanced Options |
| 162 | + </AccordionTrigger> |
| 163 | + <AccordionContent className="flex flex-col gap-6 px-0"> |
| 164 | + <FormControl isInvalid={!!errors.background_color}> |
| 165 | + <FormLabel> |
| 166 | + Background Color <OpenSeaPropertyBadge /> |
| 167 | + </FormLabel> |
| 168 | + <Input max="6" {...register("background_color")} /> |
| 169 | + <FormHelperText> |
| 170 | + Must be a six-character hexadecimal with a pre-pended #. |
| 171 | + </FormHelperText> |
| 172 | + <FormErrorMessage> |
| 173 | + {errors?.background_color?.message} |
| 174 | + </FormErrorMessage> |
| 175 | + </FormControl> |
| 176 | + {!externalIsTextFile && ( |
| 177 | + <FormControl isInvalid={!!errors.external_url}> |
| 178 | + <FormLabel> |
| 179 | + External URL <OpenSeaPropertyBadge /> |
| 180 | + </FormLabel> |
| 181 | + <Input {...register("external_url")} /> |
| 182 | + <FormHelperText> |
| 183 | + This is the URL that will appear below the asset's image |
| 184 | + on OpenSea and will allow users to leave OpenSea and view the |
| 185 | + item on your site. |
| 186 | + </FormHelperText> |
| 187 | + <FormErrorMessage> |
| 188 | + {errors?.external_url?.message as unknown as string} |
| 189 | + </FormErrorMessage> |
| 190 | + </FormControl> |
| 191 | + )} |
| 192 | + <FormControl isInvalid={!!errors.customImage}> |
| 193 | + <FormLabel>Image URL</FormLabel> |
| 194 | + <Input max="6" {...register("customImage")} /> |
| 195 | + <FormHelperText> |
| 196 | + If you already have your NFT image preuploaded, you can set the |
| 197 | + URL or URI here. |
| 198 | + </FormHelperText> |
| 199 | + <FormErrorMessage> |
| 200 | + {errors?.customImage?.message} |
| 201 | + </FormErrorMessage> |
| 202 | + </FormControl> |
| 203 | + <FormControl isInvalid={!!errors.customAnimationUrl}> |
| 204 | + <FormLabel>Animation URL</FormLabel> |
| 205 | + <Input max="6" {...register("customAnimationUrl")} /> |
| 206 | + <FormHelperText> |
| 207 | + If you already have your NFT Animation URL preuploaded, you can |
| 208 | + set the URL or URI here. |
| 209 | + </FormHelperText> |
| 210 | + <FormErrorMessage> |
| 211 | + {errors?.customAnimationUrl?.message} |
| 212 | + </FormErrorMessage> |
| 213 | + </FormControl> |
| 214 | + </AccordionContent> |
| 215 | + </AccordionItem> |
| 216 | + </Accordion> |
| 217 | + </div> |
| 218 | + ); |
| 219 | +} |
0 commit comments