Skip to content

Commit 6a6b0fb

Browse files
authored
[MNY-254] SDK: Preserve token and amount selection in BuyWidget when going back to initial screen (#8260)
1 parent c934102 commit 6a6b0fb

File tree

2 files changed

+42
-39
lines changed

2 files changed

+42
-39
lines changed

packages/thirdweb/src/react/web/ui/Bridge/BuyWidget.tsx

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { trackPayEvent } from "../../../../analytics/track/pay.js";
66
import type { TokenWithPrices } from "../../../../bridge/index.js";
77
import type { Chain } from "../../../../chains/types.js";
88
import type { ThirdwebClient } from "../../../../client/client.js";
9+
import { NATIVE_TOKEN_ADDRESS } from "../../../../constants/addresses.js";
910
import type { SupportedFiatCurrency } from "../../../../pay/convert/type.js";
1011
import type { PurchaseData } from "../../../../pay/types.js";
1112
import type { Address } from "../../../../utils/address.js";
@@ -28,7 +29,11 @@ import connectLocaleEn from "../ConnectWallet/locale/en.js";
2829
import { EmbedContainer } from "../ConnectWallet/Modal/ConnectEmbed.js";
2930
import { DynamicHeight } from "../components/DynamicHeight.js";
3031
import { ErrorBanner } from "./ErrorBanner.js";
31-
import { FundWallet } from "./FundWallet.js";
32+
import {
33+
type AmountSelection,
34+
FundWallet,
35+
type SelectedToken,
36+
} from "./FundWallet.js";
3237
import { PaymentDetails } from "./payment-details/PaymentDetails.js";
3338
import { PaymentSelection } from "./payment-selection/PaymentSelection.js";
3439
import { SuccessScreen } from "./payment-success/SuccessScreen.js";
@@ -454,6 +459,22 @@ function BridgeWidgetContent(
454459
[props.onCancel],
455460
);
456461

462+
const [amountSelection, setAmountSelection] = useState<AmountSelection>({
463+
type: "token",
464+
value: props.amount ?? "",
465+
});
466+
467+
const [selectedToken, setSelectedToken] = useState<SelectedToken>(() => {
468+
if (!props.chain?.id) {
469+
return undefined;
470+
}
471+
472+
return {
473+
chainId: props.chain.id,
474+
tokenAddress: props.tokenAddress || NATIVE_TOKEN_ADDRESS,
475+
};
476+
});
477+
457478
if (screen.id === "1:buy-ui" || !activeWalletInfo) {
458479
return (
459480
<FundWallet
@@ -479,11 +500,10 @@ function BridgeWidgetContent(
479500
}}
480501
buttonLabel={props.buttonLabel}
481502
currency={props.currency}
482-
initialSelection={{
483-
tokenAddress: props.tokenAddress,
484-
chainId: props.chain?.id,
485-
amount: props.amount,
486-
}}
503+
selectedToken={selectedToken}
504+
setSelectedToken={setSelectedToken}
505+
amountSelection={amountSelection}
506+
setAmountSelection={setAmountSelection}
487507
/>
488508
);
489509
}

packages/thirdweb/src/react/web/ui/Bridge/FundWallet.tsx

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { ArrowDownIcon } from "@radix-ui/react-icons";
44
import { useState } from "react";
55
import type { TokenWithPrices } from "../../../../bridge/types/Token.js";
66
import type { ThirdwebClient } from "../../../../client/client.js";
7-
import { NATIVE_TOKEN_ADDRESS } from "../../../../constants/addresses.js";
87
import {
98
getFiatSymbol,
109
type SupportedFiatCurrency,
@@ -86,11 +85,10 @@ type FundWalletProps = {
8685
*/
8786
showThirdwebBranding: boolean;
8887

89-
initialSelection: {
90-
tokenAddress: string | undefined;
91-
chainId: number | undefined;
92-
amount: string | undefined;
93-
};
88+
selectedToken: SelectedToken | undefined;
89+
setSelectedToken: (token: SelectedToken | undefined) => void;
90+
amountSelection: AmountSelection;
91+
setAmountSelection: (amountSelection: AmountSelection) => void;
9492

9593
/**
9694
* The currency to use for the payment.
@@ -116,14 +114,14 @@ type FundWalletProps = {
116114
};
117115
};
118116

119-
type SelectedToken =
117+
export type SelectedToken =
120118
| {
121119
chainId: number;
122120
tokenAddress: string;
123121
}
124122
| undefined;
125123

126-
type AmountSelection =
124+
export type AmountSelection =
127125
| {
128126
type: "usd";
129127
value: string;
@@ -134,10 +132,6 @@ type AmountSelection =
134132
};
135133

136134
export function FundWallet(props: FundWalletProps) {
137-
const [amountSelection, setAmountSelection] = useState<AmountSelection>({
138-
type: "token",
139-
value: props.initialSelection.amount ?? "",
140-
});
141135
const theme = useCustomTheme();
142136
const activeWalletInfo = useActiveWalletInfo();
143137
const receiver =
@@ -154,29 +148,18 @@ export function FundWallet(props: FundWalletProps) {
154148
checksumAddress(activeWalletInfo?.activeAccount?.address)
155149
: true);
156150

157-
const [selectedToken, setSelectedToken] = useState<SelectedToken>(() => {
158-
if (!props.initialSelection.chainId) {
159-
return undefined;
160-
}
161-
162-
return {
163-
chainId: props.initialSelection.chainId,
164-
tokenAddress: props.initialSelection.tokenAddress || NATIVE_TOKEN_ADDRESS,
165-
};
166-
});
167-
168151
const tokenQuery = useTokenQuery({
169-
tokenAddress: selectedToken?.tokenAddress,
170-
chainId: selectedToken?.chainId,
152+
tokenAddress: props.selectedToken?.tokenAddress,
153+
chainId: props.selectedToken?.chainId,
171154
client: props.client,
172155
});
173156

174157
const destinationToken =
175158
tokenQuery.data?.type === "success" ? tokenQuery.data.token : undefined;
176159

177160
const tokenBalanceQuery = useTokenBalance({
178-
chainId: selectedToken?.chainId,
179-
tokenAddress: selectedToken?.tokenAddress,
161+
chainId: props.selectedToken?.chainId,
162+
tokenAddress: props.selectedToken?.tokenAddress,
180163
client: props.client,
181164
walletAddress: activeWalletInfo?.activeAccount?.address,
182165
});
@@ -227,9 +210,9 @@ export function FundWallet(props: FundWalletProps) {
227210
activeWalletInfo={activeWalletInfo}
228211
onClose={() => setIsTokenSelectionOpen(false)}
229212
client={props.client}
230-
selectedToken={selectedToken}
213+
selectedToken={props.selectedToken}
231214
setSelectedToken={(token) => {
232-
setSelectedToken(token);
215+
props.setSelectedToken(token);
233216
setIsTokenSelectionOpen(false);
234217
}}
235218
/>
@@ -240,11 +223,11 @@ export function FundWallet(props: FundWalletProps) {
240223
<TokenSection
241224
title={actionLabel}
242225
presetOptions={props.presetOptions}
243-
amountSelection={amountSelection}
244-
setAmount={setAmountSelection}
226+
amountSelection={props.amountSelection}
227+
setAmount={props.setAmountSelection}
245228
activeWalletInfo={activeWalletInfo}
246229
selectedToken={
247-
selectedToken
230+
props.selectedToken
248231
? {
249232
data:
250233
tokenQuery.data?.type === "success"
@@ -310,7 +293,7 @@ export function FundWallet(props: FundWalletProps) {
310293

311294
const fiatPricePerToken = destinationToken.prices[props.currency];
312295
const { tokenValue } = getAmounts(
313-
amountSelection,
296+
props.amountSelection,
314297
fiatPricePerToken,
315298
);
316299

0 commit comments

Comments
 (0)