Skip to content

Commit

Permalink
[CORE-654] Nebula replace sendTransaction function with useSendTransa…
Browse files Browse the repository at this point in the history
…ction hook (#5797)

## Problem solved

Short description of the bug fixed or feature added

<!-- start pr-codex -->

---

## PR-Codex overview
This PR focuses on enhancing the transaction handling in the `Chats` component by improving the transaction execution process and ensuring the use of an authenticated `ThirdwebClient`.

### Detailed summary
- Updated `client` initialization in `ChatPageContent` to include `props.authToken`.
- Introduced `ExecuteTransaction` component to handle transaction execution.
- Replaced `SendTransactionButton` with `ExecuteTransaction` in `Chats`.
- Refactored `SendTransactionButton` to use `useSendTransaction` instead of `sendTransaction`.
- Simplified error handling for transaction data parsing.

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}`

<!-- end pr-codex -->
  • Loading branch information
MananTank committed Dec 19, 2024
1 parent 7711efe commit 46a426b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 43 deletions.
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

0 comments on commit 46a426b

Please sign in to comment.