Skip to content

Commit 46a426b

Browse files
committed
[CORE-654] Nebula replace sendTransaction function with useSendTransaction 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 -->
1 parent 7711efe commit 46a426b

File tree

2 files changed

+42
-43
lines changed

2 files changed

+42
-43
lines changed

apps/dashboard/src/app/nebula-app/(app)/components/ChatPageContent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function ChatPageContent(props: {
2222
}) {
2323
const address = useActiveAccount()?.address;
2424
const activeChain = useActiveWalletChain();
25-
const client = useThirdwebClient();
25+
const client = useThirdwebClient(props.authToken);
2626
const [userHasSubmittedMessage, setUserHasSubmittedMessage] = useState(false);
2727
const [messages, setMessages] = useState<Array<ChatMessage>>(() => {
2828
if (props.session?.history) {

apps/dashboard/src/app/nebula-app/(app)/components/Chats.tsx

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { ScrollShadow } from "@/components/ui/ScrollShadow/ScrollShadow";
33
import { Spinner } from "@/components/ui/Spinner/Spinner";
44
import { Alert, AlertTitle } from "@/components/ui/alert";
55
import { Button } from "@/components/ui/button";
6-
import { getThirdwebClient } from "@/constants/thirdweb.server";
76
import { cn } from "@/lib/utils";
87
import type { Account as TWAccount } from "@3rdweb-sdk/react/hooks/useApi";
98
import { useMutation } from "@tanstack/react-query";
@@ -17,8 +16,7 @@ import {
1716
import { useEffect, useRef, useState } from "react";
1817
import { toast } from "sonner";
1918
import type { ThirdwebClient } from "thirdweb";
20-
import { sendTransaction } from "thirdweb";
21-
import { useActiveAccount } from "thirdweb/react";
19+
import { useSendTransaction } from "thirdweb/react";
2220
import type { Account } from "thirdweb/wallets";
2321
import { TransactionButton } from "../../../../components/buttons/TransactionButton";
2422
import { MarkdownRenderer } from "../../../../components/contract-components/published-contract/markdown-renderer";
@@ -170,9 +168,10 @@ export function Chats(props: {
170168
{message.text}
171169
</span>
172170
) : message.type === "send_transaction" ? (
173-
<SendTransactionButton
171+
<ExecuteTransaction
174172
txData={message.data}
175173
twAccount={props.twAccount}
174+
client={props.client}
176175
/>
177176
) : (
178177
<span className="leading-loose">{message.text}</span>
@@ -203,6 +202,29 @@ export function Chats(props: {
203202
);
204203
}
205204

205+
function ExecuteTransaction(props: {
206+
txData: SendTransactionOption | null;
207+
twAccount: TWAccount;
208+
client: ThirdwebClient;
209+
}) {
210+
if (!props.txData) {
211+
return (
212+
<Alert variant="destructive">
213+
<AlertCircleIcon className="size-5" />
214+
<AlertTitle>Failed to parse transaction data</AlertTitle>
215+
</Alert>
216+
);
217+
}
218+
219+
return (
220+
<SendTransactionButton
221+
txData={props.txData}
222+
twAccount={props.twAccount}
223+
client={props.client}
224+
/>
225+
);
226+
}
227+
206228
function MessageActions(props: {
207229
authToken: string;
208230
requestId: string;
@@ -297,51 +319,28 @@ function MessageActions(props: {
297319
}
298320

299321
function SendTransactionButton(props: {
300-
txData: SendTransactionOption | null;
322+
txData: SendTransactionOption;
301323
twAccount: TWAccount;
324+
client: ThirdwebClient;
302325
}) {
303-
const account = useActiveAccount();
304-
const chain = useV5DashboardChain(props.txData?.chainId);
305-
306-
const sendTxMutation = useMutation({
307-
mutationFn: () => {
308-
if (!account) {
309-
throw new Error("No active account");
310-
}
311-
312-
if (!props.txData || !chain) {
313-
throw new Error("Invalid transaction");
314-
}
315-
316-
return sendTransaction({
317-
account,
318-
transaction: {
319-
...props.txData,
320-
nonce: Number(props.txData.nonce),
321-
to: props.txData.to || undefined, // Get rid of the potential null value
322-
chain,
323-
client: getThirdwebClient(),
324-
},
325-
});
326-
},
327-
});
328-
329-
if (!props.txData) {
330-
return (
331-
<Alert variant="destructive">
332-
<AlertCircleIcon className="size-5" />
333-
<AlertTitle>Failed to parse transaction data</AlertTitle>
334-
</Alert>
335-
);
336-
}
326+
const { txData } = props;
327+
const sendTransaction = useSendTransaction();
328+
const chain = useV5DashboardChain(txData.chainId);
337329

338330
return (
339331
<TransactionButton
340-
isPending={sendTxMutation.isPending}
332+
isPending={sendTransaction.isPending}
341333
transactionCount={1}
342-
txChainID={props.txData.chainId}
334+
txChainID={txData.chainId}
343335
onClick={() => {
344-
const promise = sendTxMutation.mutateAsync();
336+
const promise = sendTransaction.mutateAsync({
337+
...props.txData,
338+
nonce: Number(txData.nonce),
339+
to: txData.to || undefined, // Get rid of the potential null value
340+
chain: chain,
341+
client: props.client,
342+
});
343+
345344
toast.promise(promise, {
346345
success: "Transaction sent successfully",
347346
error: "Failed to send transaction",

0 commit comments

Comments
 (0)