Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CORE-654] Nebula replace sendTransaction function with useSendTransaction hook #5797

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function ChatPageContent(props: {
}) {
const address = useActiveAccount()?.address;
const activeChain = useActiveWalletChain();
const client = useThirdwebClient();
const client = useThirdwebClient(props.authToken);
const [userHasSubmittedMessage, setUserHasSubmittedMessage] = useState(false);
const [messages, setMessages] = useState<Array<ChatMessage>>(() => {
if (props.session?.history) {
Expand Down
83 changes: 41 additions & 42 deletions apps/dashboard/src/app/nebula-app/(app)/components/Chats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ScrollShadow } from "@/components/ui/ScrollShadow/ScrollShadow";
import { Spinner } from "@/components/ui/Spinner/Spinner";
import { Alert, AlertTitle } from "@/components/ui/alert";
import { Button } from "@/components/ui/button";
import { getThirdwebClient } from "@/constants/thirdweb.server";
import { cn } from "@/lib/utils";
import type { Account as TWAccount } from "@3rdweb-sdk/react/hooks/useApi";
import { useMutation } from "@tanstack/react-query";
Expand All @@ -17,8 +16,7 @@ import {
import { useEffect, useRef, useState } from "react";
import { toast } from "sonner";
import type { ThirdwebClient } from "thirdweb";
import { sendTransaction } from "thirdweb";
import { useActiveAccount } from "thirdweb/react";
import { useSendTransaction } from "thirdweb/react";
import type { Account } from "thirdweb/wallets";
import { TransactionButton } from "../../../../components/buttons/TransactionButton";
import { MarkdownRenderer } from "../../../../components/contract-components/published-contract/markdown-renderer";
Expand Down Expand Up @@ -170,9 +168,10 @@ export function Chats(props: {
{message.text}
</span>
) : message.type === "send_transaction" ? (
<SendTransactionButton
<ExecuteTransaction
txData={message.data}
twAccount={props.twAccount}
client={props.client}
/>
) : (
<span className="leading-loose">{message.text}</span>
Expand Down Expand Up @@ -203,6 +202,29 @@ export function Chats(props: {
);
}

function ExecuteTransaction(props: {
txData: SendTransactionOption | null;
twAccount: TWAccount;
client: ThirdwebClient;
}) {
if (!props.txData) {
return (
<Alert variant="destructive">
<AlertCircleIcon className="size-5" />
<AlertTitle>Failed to parse transaction data</AlertTitle>
</Alert>
);
}

return (
<SendTransactionButton
txData={props.txData}
twAccount={props.twAccount}
client={props.client}
/>
);
}

function MessageActions(props: {
authToken: string;
requestId: string;
Expand Down Expand Up @@ -297,51 +319,28 @@ function MessageActions(props: {
}

function SendTransactionButton(props: {
txData: SendTransactionOption | null;
txData: SendTransactionOption;
twAccount: TWAccount;
client: ThirdwebClient;
}) {
const account = useActiveAccount();
const chain = useV5DashboardChain(props.txData?.chainId);

const sendTxMutation = useMutation({
mutationFn: () => {
if (!account) {
throw new Error("No active account");
}

if (!props.txData || !chain) {
throw new Error("Invalid transaction");
}

return sendTransaction({
account,
transaction: {
...props.txData,
nonce: Number(props.txData.nonce),
to: props.txData.to || undefined, // Get rid of the potential null value
chain,
client: getThirdwebClient(),
},
});
},
});

if (!props.txData) {
return (
<Alert variant="destructive">
<AlertCircleIcon className="size-5" />
<AlertTitle>Failed to parse transaction data</AlertTitle>
</Alert>
);
}
const { txData } = props;
const sendTransaction = useSendTransaction();
const chain = useV5DashboardChain(txData.chainId);

return (
<TransactionButton
isPending={sendTxMutation.isPending}
isPending={sendTransaction.isPending}
transactionCount={1}
txChainID={props.txData.chainId}
txChainID={txData.chainId}
onClick={() => {
const promise = sendTxMutation.mutateAsync();
const promise = sendTransaction.mutateAsync({
...props.txData,
nonce: Number(txData.nonce),
to: txData.to || undefined, // Get rid of the potential null value
chain: chain,
client: props.client,
});

toast.promise(promise, {
success: "Transaction sent successfully",
error: "Failed to send transaction",
Expand Down
Loading