diff --git a/src/components/ContractFunctions/AccountFunctions.tsx b/src/components/ContractFunctions/AccountFunctions.tsx index 98f9a76..687745f 100644 --- a/src/components/ContractFunctions/AccountFunctions.tsx +++ b/src/components/ContractFunctions/AccountFunctions.tsx @@ -3,12 +3,20 @@ import { useAirkit } from "../../hooks/useAirkit"; import { isUrl } from "../../utils"; import { Button } from "../common/Button"; import { useLogging } from "../../hooks/useLogging"; +import { useState } from "react"; export const AccountFunctions = () => { const { airService } = useAirkit(); const { addresses, address } = useAccount(); const { data: balance } = useBalance({ address }); const { setLog } = useLogging(); + const [loadingStates, setLoadingStates] = useState>( + {} + ); + + const setLoading = (key: string, loading: boolean) => { + setLoadingStates((prev) => ({ ...prev, [key]: loading })); + }; const getUserInfo = async () => { try { @@ -129,7 +137,7 @@ export const AccountFunctions = () => { } }; - const ACCOUNT_FUNCTIONS: Record void> = { + const ACCOUNT_FUNCTIONS: Record Promise | void> = { "Get User Info": getUserInfo, "Get Accounts": getAccounts, "Get Account Balance": getAccountBalance, @@ -147,8 +155,20 @@ export const AccountFunctions = () => {

Account Functions

{Object.entries(ACCOUNT_FUNCTIONS).map(([key, value]) => ( - ))}
diff --git a/src/components/ContractFunctions/ContractFunctions.tsx b/src/components/ContractFunctions/ContractFunctions.tsx index 778e971..95d7c8e 100644 --- a/src/components/ContractFunctions/ContractFunctions.tsx +++ b/src/components/ContractFunctions/ContractFunctions.tsx @@ -14,12 +14,19 @@ import { MOCK_ERC721_CONTRACT, } from "../../utils/constants"; import { Button } from "../common/Button"; -import { useMemo } from "react"; +import { useMemo, useState } from "react"; export const ContractFunctions = () => { const { setLog } = useLogging(); const { address } = useAccount(); const config = useConfig(); + const [loadingStates, setLoadingStates] = useState>( + {} + ); + + const setLoading = (key: string, loading: boolean) => { + setLoadingStates((prev) => ({ ...prev, [key]: loading })); + }; const data = useMemo( () => @@ -92,12 +99,16 @@ export const ContractFunctions = () => { }; const balanceOfTokenFn = async () => { - const { data: result } = await refetchErc20Balance(); - if (typeof result !== "bigint") { - setLog("No balance of token", "info"); - return; + try { + const { data: result } = await refetchErc20Balance(); + if (typeof result !== "bigint") { + setLog("No balance of token", "info"); + return; + } + setLog(`Balance of token: ${formatEther(result)}`, "info"); + } catch (error) { + setLog(`Error getting balance: ${error}`, "error"); } - setLog(`Balance of token: ${formatEther(result)}`, "info"); }; const mintMockERC721Fn = async () => { @@ -115,12 +126,16 @@ export const ContractFunctions = () => { }; const balanceOfERC721Fn = async () => { - const { data: result } = await refetchErc721Balance(); - if (typeof result !== "bigint") { - setLog("No balance of token", "info"); - return; + try { + const { data: result } = await refetchErc721Balance(); + if (typeof result !== "bigint") { + setLog("No balance of token", "info"); + return; + } + setLog(`Balance of token: ${result}`, "info"); + } catch (error) { + setLog(`Error getting balance: ${error}`, "error"); } - setLog(`Balance of token: ${result}`, "info"); }; const multicallFn = async () => { @@ -147,7 +162,7 @@ export const ContractFunctions = () => { } }; - const CONTRACT_FUNCTIONS: Record void> = { + const CONTRACT_FUNCTIONS: Record Promise | void> = { "Estimate Gas (transfer)": estimateGasForTransferTokenFn, "mint (ERC20)": mintMockTokenFn, "transfer (ERC20)": transferTokenFn, @@ -162,8 +177,20 @@ export const ContractFunctions = () => {

Contract Functions

{Object.entries(CONTRACT_FUNCTIONS).map(([key, value]) => ( - ))}
diff --git a/src/components/ContractFunctions/SigningFunctions.tsx b/src/components/ContractFunctions/SigningFunctions.tsx index d40b39b..2a6beb1 100644 --- a/src/components/ContractFunctions/SigningFunctions.tsx +++ b/src/components/ContractFunctions/SigningFunctions.tsx @@ -1,18 +1,23 @@ import { useSignMessage } from "wagmi"; import { Button } from "../common/Button"; import { useLogging } from "../../hooks/useLogging"; +import { useState } from "react"; export const SigningFunctions = () => { const { signMessageAsync } = useSignMessage(); const { setLog } = useLogging(); + const [loading, setLoading] = useState(false); const signMessageFn = async () => { + setLoading(true); try { const message = "Hello, World!"; const signature = await signMessageAsync({ message }); setLog(`Signature: ${signature}`, "success"); } catch (error) { setLog(`Error: ${error}`, "error"); + } finally { + setLoading(false); } }; @@ -20,8 +25,8 @@ export const SigningFunctions = () => {

Signing Functions

-
diff --git a/src/components/common/Button.tsx b/src/components/common/Button.tsx index 65819af..e75b3d8 100644 --- a/src/components/common/Button.tsx +++ b/src/components/common/Button.tsx @@ -8,6 +8,32 @@ type ButtonProps = { variant?: "primary" | "danger" | "outline"; } & React.ButtonHTMLAttributes; +const getVariantStyles = ( + variant: NonNullable, + disabled: boolean = false +) => { + const baseStyles = "p-2 rounded-md cursor-pointer"; + + const variantStyles = { + primary: "bg-blue-500 text-white", + danger: "bg-red-400 text-white", + outline: "border border-gray-300 text-black", + } satisfies Record; + + const hoverStyles = { + primary: "hover:bg-blue-400", + danger: "hover:bg-red-500", + outline: "hover:bg-gray-100", + } satisfies Record; + + return cn( + baseStyles, + variantStyles[variant], + !disabled && hoverStyles[variant], + disabled && "opacity-50 cursor-not-allowed" + ); +}; + export const Button: FC = ({ children, onClick, @@ -18,17 +44,7 @@ export const Button: FC = ({ }) => { return (