Skip to content

Commit 562c534

Browse files
[SDK] feat: new PayEmbed UI for selecting tokens (#6257)
1 parent b882e29 commit 562c534

File tree

12 files changed

+626
-521
lines changed

12 files changed

+626
-521
lines changed

.changeset/pink-guests-agree.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+
New PayEmbed UI for token selection

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

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { ThirdwebClient } from "../../../../../../client/client.js";
55
import { NATIVE_TOKEN_ADDRESS } from "../../../../../../constants/addresses.js";
66
import type { BuyWithCryptoStatus } from "../../../../../../pay/buyWithCrypto/getStatus.js";
77
import type { BuyWithFiatStatus } from "../../../../../../pay/buyWithFiat/getStatus.js";
8+
import { formatNumber } from "../../../../../../utils/formatNumber.js";
89
import type { Account } from "../../../../../../wallets/interfaces/wallet.js";
910
import type { WalletId } from "../../../../../../wallets/wallet-types.js";
1011
import {
@@ -56,6 +57,7 @@ import { FiatValue } from "./swap/FiatValue.js";
5657
import { PaymentSelectionScreen } from "./swap/PaymentSelectionScreen.js";
5758
import { SwapFlow } from "./swap/SwapFlow.js";
5859
import { SwapScreenContent } from "./swap/SwapScreenContent.js";
60+
import { TokenSelectorScreen } from "./swap/TokenSelectorScreen.js";
5961
import { TransferFlow } from "./swap/TransferFlow.js";
6062
import {
6163
type SupportedChainAndTokens,
@@ -453,30 +455,39 @@ function BuyScreenContent(props: BuyScreenContentProps) {
453455
}
454456

455457
return (
456-
<TokenSelector
458+
<TokenSelectorScreen
457459
onBack={goBack}
458-
tokenList={(
459-
(fromChain?.id ? sourceSupportedTokens[fromChain.id] : undefined) ||
460-
[]
461-
).filter((x) => x.address !== NATIVE_TOKEN_ADDRESS)}
462-
onTokenSelect={(tokenInfo) => {
463-
setFromToken(tokenInfo);
460+
onConnect={() => {
461+
setScreen({
462+
id: "connect-payer-wallet",
463+
backScreen: screen,
464+
});
465+
}}
466+
modalTitle="Available tokens"
467+
connectLocale={props.connectLocale}
468+
client={props.client}
469+
sourceTokens={sourceSupportedTokens}
470+
sourceSupportedTokens={sourceSupportedTokens}
471+
toChain={toChain}
472+
toToken={toToken}
473+
tokenAmount={tokenAmount}
474+
fromChain={fromChain}
475+
fromToken={fromToken}
476+
mode={payOptions.mode}
477+
hiddenWallets={props.hiddenWallets}
478+
onSelect={(w, token, chain) => {
479+
const account = w.getAccount();
480+
if (account) {
481+
setPayer({
482+
account,
483+
chain,
484+
wallet: w,
485+
});
486+
setFromToken(token);
487+
setFromChain(chain);
488+
}
464489
goBack();
465490
}}
466-
chain={fromChain}
467-
chainSelection={
468-
// hide chain selection if it's disabled
469-
payOptions.buyWithCrypto !== false &&
470-
payOptions.buyWithCrypto?.prefillSource?.allowEdits?.chain !== false
471-
? {
472-
chains: supportedSourcesQuery.data.map((x) => x.chain),
473-
select: (c) => setFromChain(c),
474-
}
475-
: undefined
476-
}
477-
connectLocale={connectLocale}
478-
client={client}
479-
modalTitle="Pay with"
480491
/>
481492
);
482493
}
@@ -550,23 +561,23 @@ function BuyScreenContent(props: BuyScreenContentProps) {
550561
payWithFiatEnabled={props.payOptions.buyWithFiat !== false}
551562
toChain={toChain}
552563
toToken={toToken}
564+
fromToken={fromToken}
565+
fromChain={fromChain}
553566
tokenAmount={tokenAmount}
554-
onSelect={(w, token, chain) => {
555-
const account = w.getAccount();
556-
if (account) {
557-
setPayer({
558-
account,
559-
chain,
560-
wallet: w,
561-
});
562-
setFromToken(token);
563-
setFromChain(chain);
564-
setScreen({ id: "buy-with-crypto" });
565-
}
567+
onContinue={() => {
568+
setScreen({ id: "buy-with-crypto" });
566569
}}
567570
onSelectFiat={() => {
568571
setScreen({ id: "buy-with-fiat" });
569572
}}
573+
onPickToken={() => {
574+
setScreen({
575+
id: "select-from-token",
576+
backScreen: {
577+
id: "select-payment-method",
578+
},
579+
});
580+
}}
570581
showAllWallets={!!props.connectOptions?.showAllWallets}
571582
wallets={props.connectOptions?.wallets}
572583
onBack={() => {
@@ -659,8 +670,9 @@ function SelectedTokenInfo(props: {
659670
disabled?: boolean;
660671
}) {
661672
const getWidth = () => {
662-
let chars = props.tokenAmount.replace(".", "").length;
663-
const hasDot = props.tokenAmount.includes(".");
673+
const amount = formatNumber(Number(props.tokenAmount), 5).toString();
674+
let chars = amount.replace(".", "").length;
675+
const hasDot = amount.includes(".");
664676
if (hasDot) {
665677
chars += 0.3;
666678
}
@@ -685,7 +697,11 @@ function SelectedTokenInfo(props: {
685697
placeholder="0"
686698
type="text"
687699
data-placeholder={props.tokenAmount === ""}
688-
value={props.tokenAmount || "0"}
700+
value={
701+
props.tokenAmount
702+
? formatNumber(Number(props.tokenAmount), 5)
703+
: "0"
704+
}
689705
disabled={props.disabled}
690706
onClick={(e) => {
691707
// put cursor at the end of the input
@@ -885,7 +901,7 @@ function MainScreen(props: {
885901
client={props.client}
886902
/>
887903

888-
<Spacer y="xl" />
904+
<Spacer y="md" />
889905

890906
{/* Continue */}
891907
<Container flex="column" gap="sm">

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ import {
3232
NATIVE_TOKEN,
3333
isNativeToken,
3434
} from "../nativeToken.js";
35-
import { WalletRow } from "./WalletSelectorButton.js";
3635
import { useTransactionCostAndData } from "./main/useBuyTxStates.js";
36+
import { WalletRow } from "./swap/TokenSelectorScreen.js";
3737
import type { SupportedChainAndTokens } from "./swap/useSwapSupportedChains.js";
3838

3939
export function TransactionModeScreen(props: {

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

Lines changed: 0 additions & 220 deletions
This file was deleted.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ export function useFromTokenSelectionStates(options: {
8080

8181
// --------------------------------------------------------------------------
8282
const firstSupportedSource = supportedSources?.length
83-
? supportedSources[0]
83+
? supportedSources.length === 1
84+
? supportedSources[0]
85+
: supportedSources.find((x) => x.chain.id !== 1) // dont use mainnet as a default source, unless its the only source
8486
: undefined;
8587

8688
// Source token and chain selection ---------------------------------------------------

0 commit comments

Comments
 (0)