11import { lazy , Suspense } from "react" ;
22import { LoadingDots } from "@/components/ui/LoadingDots" ;
3+ import {
4+ quotes ,
5+ stringifyImports ,
6+ stringifyProps ,
7+ } from "../../../lib/code-gen" ;
38import type { BridgeComponentsPlaygroundOptions } from "./types" ;
49
510const CodeClient = lazy ( ( ) =>
@@ -16,79 +21,124 @@ function CodeLoading() {
1621 ) ;
1722}
1823
19- export function CodeGen ( props : { options : BridgeComponentsPlaygroundOptions } ) {
24+ export function CodeGen ( props : {
25+ widget : "buy" | "checkout" | "transaction" ;
26+ options : BridgeComponentsPlaygroundOptions ;
27+ } ) {
2028 return (
2129 < div className = "flex w-full grow flex-col" >
2230 < Suspense fallback = { < CodeLoading /> } >
23- < CodeClient className = "grow" code = { getCode ( props . options ) } lang = "tsx" />
31+ < CodeClient
32+ className = "grow"
33+ code = { getCode ( props . widget , props . options ) }
34+ lang = "tsx"
35+ />
2436 </ Suspense >
2537 </ div >
2638 ) ;
2739}
2840
29- function getCode ( options : BridgeComponentsPlaygroundOptions ) {
41+ function getCode (
42+ widget : "buy" | "checkout" | "transaction" ,
43+ options : BridgeComponentsPlaygroundOptions ,
44+ ) {
45+ const componentName =
46+ widget === "buy"
47+ ? "BuyWidget"
48+ : widget === "checkout"
49+ ? "CheckoutWidget"
50+ : widget === "transaction"
51+ ? "TransactionWidget"
52+ : "" ;
53+
3054 const imports = {
31- chains : [ ] as string [ ] ,
32- react : [ "PayEmbed" ] as string [ ] ,
33- thirdweb : [ ] as string [ ] ,
34- wallets : [ ] as string [ ] ,
55+ "thirdweb/ chains" : [ ] as string [ ] ,
56+ "thirdweb/ react" : [ componentName ] as string [ ] ,
57+ thirdweb : [ "createThirdwebClient" , "defineChain" ] as string [ ] ,
58+ "thirdweb/ wallets" : [ "createWallet" ] as string [ ] ,
3559 } ;
3660
37- // Check if we have a custom chain (not base chain which has id 8453)
38- const isCustomChain =
39- options . payOptions . buyTokenChain &&
40- options . payOptions . buyTokenChain . id !== 8453 ;
61+ let themeProp : string | undefined ;
62+ if (
63+ options . theme . type === "dark" &&
64+ Object . keys ( options . theme . darkColorOverrides || { } ) . length > 0
65+ ) {
66+ themeProp = `darkTheme({
67+ colors: ${ JSON . stringify ( options . theme . darkColorOverrides ) } ,
68+ })` ;
69+ imports [ "thirdweb/react" ] . push ( "darkTheme" ) ;
70+ }
4171
42- if ( isCustomChain ) {
43- // Add defineChain to imports if using a custom chain
44- imports . thirdweb . push ( "defineChain" ) ;
45- } else {
46- // Otherwise use the base chain
47- imports . chains . push ( "base" ) ;
72+ if ( options . theme . type === "light" ) {
73+ if ( Object . keys ( options . theme . lightColorOverrides || { } ) . length > 0 ) {
74+ themeProp = `lightTheme({
75+ colors: ${ JSON . stringify ( options . theme . lightColorOverrides ) } ,
76+ })` ;
77+ imports [ "thirdweb/react" ] . push ( "lightTheme" ) ;
78+ } else {
79+ themeProp = quotes ( "light" ) ;
80+ }
4881 }
4982
50- imports . wallets . push ( "createWallet" ) ;
83+ const transaction =
84+ widget === "transaction"
85+ ? `claimTo({
86+ contract: nftContract,
87+ quantity: 1n,
88+ to: account?.address || "",
89+ tokenId: 2n,
90+ })`
91+ : undefined ;
5192
52- const componentName = ( ( ) => {
53- switch ( options . payOptions . widget ) {
54- case "buy" :
55- return "BuyWidget" ;
56- case "checkout" :
57- return "CheckoutWidget" ;
58- case "transaction" :
59- return "TransactionWidget" ;
60- default :
61- return "PayEmbed" ;
62- }
63- } ) ( ) ;
64- imports . react . push ( componentName ) ;
65- imports . chains . push ( "defineChain" ) ;
93+ const props : Record < string , string | undefined | boolean > = {
94+ theme : themeProp ,
95+ client : "client" ,
96+ description : options . payOptions . description
97+ ? quotes ( options . payOptions . description )
98+ : undefined ,
99+ image : options . payOptions . image
100+ ? quotes ( options . payOptions . image )
101+ : undefined ,
102+ name : options . payOptions . title
103+ ? quotes ( options . payOptions . title )
104+ : undefined ,
105+ paymentMethods :
106+ options . payOptions . paymentMethods . length === 2
107+ ? undefined
108+ : JSON . stringify ( options . payOptions . paymentMethods ) ,
109+ currency : options . payOptions . currency
110+ ? quotes ( options . payOptions . currency )
111+ : undefined ,
112+ chain : `defineChain(${ options . payOptions . buyTokenChain . id } )` ,
113+ showThirdwebBranding :
114+ options . payOptions . showThirdwebBranding === false ? false : undefined ,
115+ amount : options . payOptions . buyTokenAmount
116+ ? quotes ( options . payOptions . buyTokenAmount )
117+ : undefined ,
118+ tokenAddress : options . payOptions . buyTokenAddress
119+ ? quotes ( options . payOptions . buyTokenAddress )
120+ : undefined ,
121+ seller : options . payOptions . sellerAddress
122+ ? quotes ( options . payOptions . sellerAddress )
123+ : undefined ,
124+ payOptions : options . payOptions . buttonLabel
125+ ? JSON . stringify ( {
126+ buttonLabel : options . payOptions . buttonLabel ,
127+ } )
128+ : undefined ,
129+ transaction : transaction ,
130+ } ;
66131
67132 return `\
68- import { createThirdwebClient } from "thirdweb";
69- ${ imports . react . length > 0 ? `import { ${ imports . react . join ( ", " ) } } from "thirdweb/react";` : "" }
70- ${ imports . thirdweb . length > 0 ? `import { ${ imports . thirdweb . join ( ", " ) } } from "thirdweb";` : "" }
133+ ${ stringifyImports ( imports ) }
71134
72135const client = createThirdwebClient({
73136 clientId: "....",
74137});
75138
76139function Example() {
77140 return (
78- <${ componentName }
79- client={client}
80- chain={defineChain(${ options . payOptions . buyTokenChain . id } )}
81- amount="${ options . payOptions . buyTokenAmount } "${ options . payOptions . buyTokenAddress ? `\n\t tokenAddress="${ options . payOptions . buyTokenAddress } "` : "" } ${ options . payOptions . sellerAddress ? `\n\t seller="${ options . payOptions . sellerAddress } "` : "" } ${ options . payOptions . title ? `\n\t ${ options . payOptions . widget === "checkout" ? "name" : "title" } ="${ options . payOptions . title } "` : "" } ${ options . payOptions . image ? `\n\t image="${ options . payOptions . image } "` : "" } ${ options . payOptions . description ? `\n\t description="${ options . payOptions . description } "` : "" } ${ options . payOptions . buttonLabel ? `\n\t buttonLabel="${ options . payOptions . buttonLabel } "` : "" } ${ options . payOptions . paymentMethods && options . payOptions . paymentMethods . length > 0 ? `\n\t paymentMethods={${ JSON . stringify ( options . payOptions . paymentMethods ) } }` : "" } ${ options . payOptions . currency ? `\n\t currency="${ options . payOptions . currency } "` : "" } ${
82- options . payOptions . widget === "transaction"
83- ? `\n\t transaction={claimTo({
84- contract: nftContract,
85- quantity: 1n,
86- tokenId: 2n,
87- to: account?.address || "",
88- })}`
89- : ""
90- }
91- />
141+ <${ componentName } ${ stringifyProps ( props ) } />
92142 );
93143}` ;
94144}
0 commit comments