Skip to content

Commit f368793

Browse files
committed
[SDK] Fix: PayEmbed Error State (#5463)
CNCT-2385 <!-- start pr-codex --> --- ## PR-Codex overview This PR focuses on improving error handling and user experience in the `BuyScreen` and `TransactionModeScreen` components of the `thirdweb` package. It adds error states for loading issues and modifies error messages for better clarity. ### Detailed summary - Updated error message for missing account in `useBuyTxStates.ts`. - Introduced an error state in `BuyScreen` for `supportedDestinationsQuery` failures. - Added loading and error handling in `TransactionModeScreen` for transaction cost and chain data. - Enhanced user feedback with `ErrorState` and `LoadingScreen` components. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent b5227c9 commit f368793

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

.changeset/plenty-dragons-carry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Fixes PayEmbed error state appearing on certain errors

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/BuyScreen.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { useBuyWithFiatQuote } from "../../../../../core/hooks/pay/useBuyWithFia
2424
import { useActiveAccount } from "../../../../../core/hooks/wallets/useActiveAccount.js";
2525
import { invalidateWalletBalance } from "../../../../../core/providers/invalidateWalletBalance.js";
2626
import type { SupportedTokens } from "../../../../../core/utils/defaultTokens.js";
27+
import { ErrorState } from "../../../../wallets/shared/ErrorState.js";
2728
import { LoadingScreen } from "../../../../wallets/shared/LoadingScreen.js";
2829
import type { PayEmbedConnectOptions } from "../../../PayEmbed.js";
2930
import { ChainName } from "../../../components/ChainName.js";
@@ -105,6 +106,24 @@ export default function BuyScreen(props: BuyScreenProps) {
105106
isTestMode,
106107
);
107108

109+
if (supportedDestinationsQuery.isError) {
110+
return (
111+
<Container
112+
style={{
113+
minHeight: "350px",
114+
}}
115+
fullHeight
116+
flex="row"
117+
center="both"
118+
>
119+
<ErrorState
120+
title="Something went wrong"
121+
onTryAgain={supportedDestinationsQuery.refetch}
122+
/>
123+
</Container>
124+
);
125+
}
126+
108127
if (!supportedDestinationsQuery.data) {
109128
return <LoadingScreen />;
110129
}
@@ -137,9 +156,11 @@ type BuyScreenContentProps = {
137156
*/
138157
function BuyScreenContent(props: BuyScreenContentProps) {
139158
const { client, supportedDestinations, connectLocale, payOptions } = props;
159+
console.log("BuyScreenContent");
140160

141161
const activeAccount = useActiveAccount();
142162
const { payer, setPayer } = usePayerSetup();
163+
console.log("payer", payer);
143164

144165
const [screen, setScreen] = useState<SelectedScreen>({
145166
id: "main",
@@ -477,6 +498,8 @@ function BuyScreenContent(props: BuyScreenContentProps) {
477498
);
478499
}
479500

501+
console.log("SCREEN", screen.id);
502+
480503
return (
481504
<Container animate="fadein">
482505
<div>

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/TransactionModeScreen.tsx

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { useWalletBalance } from "../../../../../core/hooks/others/useWalletBala
1313
import { useActiveAccount } from "../../../../../core/hooks/wallets/useActiveAccount.js";
1414
import { useActiveWallet } from "../../../../../core/hooks/wallets/useActiveWallet.js";
1515
import { hasSponsoredTransactionsEnabled } from "../../../../../core/utils/wallet.js";
16+
import { ErrorState } from "../../../../wallets/shared/ErrorState.js";
1617
import { LoadingScreen } from "../../../../wallets/shared/LoadingScreen.js";
1718
import type { PayEmbedConnectOptions } from "../../../PayEmbed.js";
1819
import { ChainIcon } from "../../../components/ChainIcon.js";
@@ -54,9 +55,19 @@ export function TransactionModeScreen(props: {
5455
supportedDestinations,
5556
onContinue,
5657
} = props;
57-
const { data: chainData } = useChainMetadata(payUiOptions.transaction.chain);
58+
const {
59+
data: chainData,
60+
error: chainDataError,
61+
isLoading: chainDataLoading,
62+
refetch: chainDataRefetch,
63+
} = useChainMetadata(payUiOptions.transaction.chain);
5864
const metadata = payUiOptions.metadata;
59-
const { data: transactionCostAndData } = useTransactionCostAndData({
65+
const {
66+
data: transactionCostAndData,
67+
error: transactionCostAndDataError,
68+
isLoading: transactionCostAndDataLoading,
69+
refetch: transactionCostAndDataRefetch,
70+
} = useTransactionCostAndData({
6071
transaction: payUiOptions.transaction,
6172
account: payerAccount,
6273
supportedDestinations,
@@ -80,6 +91,36 @@ export function TransactionModeScreen(props: {
8091
},
8192
);
8293

94+
if (transactionCostAndDataLoading || chainDataLoading) {
95+
return <LoadingScreen />;
96+
}
97+
98+
if (transactionCostAndDataError || chainDataError) {
99+
return (
100+
<Container
101+
style={{
102+
minHeight: "350px",
103+
}}
104+
fullHeight
105+
flex="row"
106+
center="both"
107+
>
108+
<ErrorState
109+
title={
110+
transactionCostAndDataError?.message ||
111+
chainDataError?.message ||
112+
"Something went wrong"
113+
}
114+
onTryAgain={
115+
transactionCostAndDataError
116+
? transactionCostAndDataRefetch
117+
: chainDataRefetch
118+
}
119+
/>
120+
</Container>
121+
);
122+
}
123+
83124
if (!transactionCostAndData || !chainData) {
84125
return <LoadingScreen />;
85126
}

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/main/useBuyTxStates.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function useTransactionCostAndData(args: {
5858
],
5959
queryFn: async () => {
6060
if (!account) {
61-
throw new Error("No account");
61+
throw new Error("No payer account found");
6262
}
6363

6464
const erc20Value = await resolvePromisedValue(transaction.erc20Value);
@@ -112,7 +112,6 @@ export function useTransactionCostAndData(args: {
112112
getChainMetadata(transaction.chain),
113113
getTransactionGasCost(transaction, account?.address),
114114
]);
115-
116115
const walletBalance = nativeWalletBalance;
117116
const transactionValueWei =
118117
(await resolvePromisedValue(transaction.value)) || 0n;
@@ -129,7 +128,7 @@ export function useTransactionCostAndData(args: {
129128
transactionValueWei,
130129
} satisfies TransactionCostAndData;
131130
},
132-
enabled: !!transaction && !!account && !!txQueryKey,
131+
enabled: !!transaction && !!txQueryKey,
133132
refetchInterval: () => {
134133
if (transaction.erc20Value) {
135134
// if erc20 value is set, we don't need to poll

0 commit comments

Comments
 (0)