|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { |
| 4 | + Accordion, |
| 5 | + AccordionContent, |
| 6 | + AccordionItem, |
| 7 | + AccordionTrigger, |
| 8 | +} from "@/components/ui/accordion"; |
| 9 | +import { Alert, AlertTitle } from "@/components/ui/alert"; |
| 10 | +import { |
| 11 | + Form, |
| 12 | + FormControl, |
| 13 | + FormField, |
| 14 | + FormItem, |
| 15 | + FormLabel, |
| 16 | + FormMessage, |
| 17 | +} from "@/components/ui/form"; |
| 18 | +import { Input } from "@/components/ui/input"; |
| 19 | +import { Textarea } from "@/components/ui/textarea"; |
| 20 | +import { useResolveContractAbi } from "@3rdweb-sdk/react/hooks/useResolveContractAbi"; |
| 21 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 22 | +import { useMutation } from "@tanstack/react-query"; |
| 23 | +import { TransactionButton } from "components/buttons/TransactionButton"; |
| 24 | +import { useTxNotifications } from "hooks/useTxNotifications"; |
| 25 | +import { CircleAlertIcon } from "lucide-react"; |
| 26 | +import { useForm } from "react-hook-form"; |
| 27 | +import { z } from "zod"; |
| 28 | +import { ModuleCardUI, type ModuleCardUIProps } from "./module-card"; |
| 29 | +import type { ModuleInstanceProps } from "./module-instance"; |
| 30 | + |
| 31 | +const setSharedMetadataFormSchema = z.object({ |
| 32 | + name: z.string().min(1), |
| 33 | + description: z.string().min(1), |
| 34 | + imageUri: z.string().optional(), |
| 35 | + animationUri: z.string().optional(), |
| 36 | +}); |
| 37 | + |
| 38 | +export type SetSharedMetadataFormValues = z.infer< |
| 39 | + typeof setSharedMetadataFormSchema |
| 40 | +>; |
| 41 | + |
| 42 | +function ModuleExplorer(props: ModuleInstanceProps) { |
| 43 | + const { contract } = props; |
| 44 | + |
| 45 | + const abiQuery = useResolveContractAbi(contract); |
| 46 | + console.log("abiQuery", abiQuery); |
| 47 | + |
| 48 | + return ( |
| 49 | + <ModuleExplorerUI |
| 50 | + {...props} |
| 51 | + abi={abiQuery.data} |
| 52 | + contractChainId={props.contract.chain.id} |
| 53 | + /> |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +export function ModuleExplorerUI( |
| 58 | + props: Omit<ModuleCardUIProps, "children" | "updateButton"> & { |
| 59 | + abi: Abi | undefined; |
| 60 | + contractChainId: number; |
| 61 | + }, |
| 62 | +) { |
| 63 | + return ( |
| 64 | + <ModuleCardUI {...props}> |
| 65 | + <div className="h-1" /> |
| 66 | + <Accordion type="single" collapsible className="-mx-1"> |
| 67 | + <AccordionItem value="metadata" className="border-none"> |
| 68 | + <AccordionTrigger className="border-border border-t px-1"> |
| 69 | + Set Shared Metadata |
| 70 | + </AccordionTrigger> |
| 71 | + <AccordionContent className="px-1"> |
| 72 | + {props.isOwnerAccount && ( |
| 73 | + <SetSharedMetadataSection |
| 74 | + setSharedMetadata={props.setSharedMetadata} |
| 75 | + contractChainId={props.contractChainId} |
| 76 | + /> |
| 77 | + )} |
| 78 | + {!props.isOwnerAccount && ( |
| 79 | + <Alert variant="info"> |
| 80 | + <CircleAlertIcon className="size-5" /> |
| 81 | + <AlertTitle> |
| 82 | + You don't have permission to set shared metadata on this |
| 83 | + contract |
| 84 | + </AlertTitle> |
| 85 | + </Alert> |
| 86 | + )} |
| 87 | + </AccordionContent> |
| 88 | + </AccordionItem> |
| 89 | + </Accordion> |
| 90 | + </ModuleCardUI> |
| 91 | + ); |
| 92 | +} |
| 93 | + |
| 94 | +function SetSharedMetadataSection(props: { |
| 95 | + setSharedMetadata: (values: SetSharedMetadataFormValues) => Promise<void>; |
| 96 | + contractChainId: number; |
| 97 | +}) { |
| 98 | + const form = useForm<SetSharedMetadataFormValues>({ |
| 99 | + resolver: zodResolver(setSharedMetadataFormSchema), |
| 100 | + values: { |
| 101 | + name: "", |
| 102 | + description: "", |
| 103 | + imageUri: "", |
| 104 | + animationUri: "", |
| 105 | + }, |
| 106 | + reValidateMode: "onChange", |
| 107 | + }); |
| 108 | + |
| 109 | + const setSharedMetadataNotifications = useTxNotifications( |
| 110 | + "Successfully set shared metadata", |
| 111 | + "Failed to set shared metadata", |
| 112 | + ); |
| 113 | + |
| 114 | + const setSharedMetadataMutation = useMutation({ |
| 115 | + mutationFn: props.setSharedMetadata, |
| 116 | + onSuccess: setSharedMetadataNotifications.onSuccess, |
| 117 | + onError: setSharedMetadataNotifications.onError, |
| 118 | + }); |
| 119 | + |
| 120 | + const onSubmit = async () => { |
| 121 | + setSharedMetadataMutation.mutateAsync(form.getValues()); |
| 122 | + }; |
| 123 | + |
| 124 | + return ( |
| 125 | + <Form {...form}> |
| 126 | + <form onSubmit={form.handleSubmit(onSubmit)}> |
| 127 | + <div className="flex flex-col gap-6"> |
| 128 | + <div className="flex flex-col gap-6"> |
| 129 | + <FormField |
| 130 | + control={form.control} |
| 131 | + name="name" |
| 132 | + render={({ field }) => ( |
| 133 | + <FormItem> |
| 134 | + <FormLabel>Name</FormLabel> |
| 135 | + <FormControl> |
| 136 | + <Input {...field} /> |
| 137 | + </FormControl> |
| 138 | + <FormMessage /> |
| 139 | + </FormItem> |
| 140 | + )} |
| 141 | + /> |
| 142 | + |
| 143 | + <FormField |
| 144 | + control={form.control} |
| 145 | + name="description" |
| 146 | + render={({ field }) => ( |
| 147 | + <FormItem> |
| 148 | + <FormLabel>Description</FormLabel> |
| 149 | + <FormControl> |
| 150 | + <Textarea {...field} /> |
| 151 | + </FormControl> |
| 152 | + <FormMessage /> |
| 153 | + </FormItem> |
| 154 | + )} |
| 155 | + /> |
| 156 | + |
| 157 | + <FormField |
| 158 | + control={form.control} |
| 159 | + name="imageUri" |
| 160 | + render={({ field }) => ( |
| 161 | + <FormItem className="flex-1"> |
| 162 | + <FormLabel>Image URI</FormLabel> |
| 163 | + <FormControl> |
| 164 | + <Input {...field} /> |
| 165 | + </FormControl> |
| 166 | + <FormMessage /> |
| 167 | + </FormItem> |
| 168 | + )} |
| 169 | + /> |
| 170 | + <FormField |
| 171 | + control={form.control} |
| 172 | + name="animationUri" |
| 173 | + render={({ field }) => ( |
| 174 | + <FormItem className="flex-1"> |
| 175 | + <FormLabel>Animation URI</FormLabel> |
| 176 | + <FormControl> |
| 177 | + <Input {...field} /> |
| 178 | + </FormControl> |
| 179 | + <FormMessage /> |
| 180 | + </FormItem> |
| 181 | + )} |
| 182 | + /> |
| 183 | + </div> |
| 184 | + |
| 185 | + <div className="flex justify-end"> |
| 186 | + <TransactionButton |
| 187 | + size="sm" |
| 188 | + className="min-w-24" |
| 189 | + disabled={setSharedMetadataMutation.isPending} |
| 190 | + type="submit" |
| 191 | + isLoading={setSharedMetadataMutation.isPending} |
| 192 | + colorScheme="primary" |
| 193 | + transactionCount={1} |
| 194 | + txChainID={props.contractChainId} |
| 195 | + > |
| 196 | + Update |
| 197 | + </TransactionButton> |
| 198 | + </div> |
| 199 | + </div> |
| 200 | + </form> |
| 201 | + </Form> |
| 202 | + ); |
| 203 | +} |
| 204 | + |
| 205 | +export default OpenEditionMetadataModule; |
0 commit comments