import React, { useCallback, useEffect, useState } from "react"; import { Plus, Trash, X } from "phosphor-react"; import Button from "./Button"; import Box from "./Box"; import { Stack, Flex, Select } from "."; import { Dialog, DialogContent, DialogTitle, DialogDescription, DialogClose, DialogTrigger, } from "./Dialog"; import { Input, Label } from "./Input"; import { Controller, SubmitHandler, useFieldArray, useForm, } from "react-hook-form"; import { TTS, tts } from "../utils/hookOnCalculator"; import { deployHook } from "../state/actions"; import { useSnapshot } from "valtio"; import state from "../state"; import toast from "react-hot-toast"; import { prepareDeployHookTx, sha256 } from "../state/actions/deployHook"; import estimateFee from "../utils/estimateFee"; const transactionOptions = Object.keys(tts).map((key) => ({ label: key, value: key as keyof TTS, })); export type SetHookData = { Invoke: { value: keyof TTS; label: string; }[]; Fee: string; HookNamespace: string; HookParameters: { HookParameter: { HookParameterName: string; HookParameterValue: string; }; }[]; // HookGrants: { // HookGrant: { // Authorize: string; // HookHash: string; // }; // }[]; }; export const SetHookDialog: React.FC<{ accountAddress: string }> = React.memo( ({ accountAddress }) => { const snap = useSnapshot(state); const account = snap.accounts.find((acc) => acc.address === accountAddress); const [isSetHookDialogOpen, setIsSetHookDialogOpen] = useState(false); const { register, handleSubmit, control, watch, setValue, getValues, formState: { errors }, } = useForm({ defaultValues: { HookNamespace: snap.files?.[snap.activeWat]?.name?.split(".")?.[0] || "", Invoke: transactionOptions.filter((to) => to.label === "ttPAYMENT"), }, }); const { fields, append, remove } = useFieldArray({ control, name: "HookParameters", // unique name for your Field Array }); const [formInitialized, setFormInitialized] = useState(false); const [estimateLoading, setEstimateLoading] = useState(false); // Update value if activeWat changes useEffect(() => { setValue( "HookNamespace", snap.files?.[snap.activeWat]?.name?.split(".")?.[0] || "" ); setFormInitialized(true); }, [snap.activeWat, snap.files, setValue]); // const { // fields: grantFields, // append: grantAppend, // remove: grantRemove, // } = useFieldArray({ // control, // name: "HookGrants", // unique name for your Field Array // }); const [hashedNamespace, setHashedNamespace] = useState(""); const namespace = watch( "HookNamespace", snap.files?.[snap.active]?.name?.split(".")?.[0] || "" ); const calculateHashedValue = useCallback(async () => { const hashedVal = await sha256(namespace); setHashedNamespace(hashedVal.toUpperCase()); }, [namespace]); useEffect(() => { calculateHashedValue(); }, [namespace, calculateHashedValue]); // Calcucate initial fee estimate when modal opens useEffect(() => { if (formInitialized && account) { (async () => { const formValues = getValues(); const tx = await prepareDeployHookTx(account, formValues); if (!tx) { return; } const res = await estimateFee(tx, account); if (res && res.base_fee) { setValue("Fee", res.base_fee); } })(); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [formInitialized]); if (!account) { return null; } const onSubmit: SubmitHandler = async (data) => { const currAccount = state.accounts.find( (acc) => acc.address === account.address ); if (currAccount) currAccount.isLoading = true; const res = await deployHook(account, data); if (currAccount) currAccount.isLoading = false; if (res && res.engine_result === "tesSUCCESS") { toast.success("Transaction succeeded!"); return setIsSetHookDialogOpen(false); } toast.error(`Transaction failed! (${res?.engine_result_message})`); }; return (
Deploy configuration to.label === "ttPAYMENT" )} render={({ field }) => ( {errors.HookNamespace?.type === "required" && ( Namespace is required )} {fields.map((field, index) => ( ))} {errors.Fee?.type === "required" && ( Fee is required )} {/* {grantFields.map((field, index) => ( ))} */} {/* */} {/* */}
); } ); SetHookDialog.displayName = "SetHookDialog"; export default SetHookDialog;