11"use client" ;
2+ import { MultiNetworkSelector } from "@/components/blocks/NetworkSelectors" ;
23import { SettingsCard } from "@/components/blocks/SettingsCard" ;
34import { Button } from "@/components/ui/button" ;
45import { Checkbox } from "@/components/ui/checkbox" ;
@@ -23,25 +24,17 @@ import { Skeleton } from "@/components/ui/skeleton";
2324import { Switch } from "@/components/ui/switch" ;
2425import { cn } from "@/lib/utils" ;
2526import { zodResolver } from "@hookform/resolvers/zod" ;
26- import { NetworkSelectorButton } from "components/selects/NetworkSelectorButton" ;
2727import { PlusIcon } from "lucide-react" ;
2828import { useFieldArray , useForm } from "react-hook-form" ;
2929import { toast } from "sonner" ;
3030import { isAddress } from "thirdweb" ;
31- import {
32- defineChain ,
33- getCachedChainIfExists ,
34- } from "thirdweb/dist/types/chains/utils" ;
35- import {
36- useActiveWalletChain ,
37- useSwitchActiveWalletChain ,
38- } from "thirdweb/react" ;
3931import { getSocialIcon } from "thirdweb/wallets/in-app" ;
4032import {
4133 DEFAULT_ACCOUNT_FACTORY_V0_6 ,
4234 DEFAULT_ACCOUNT_FACTORY_V0_7 ,
4335} from "thirdweb/wallets/smart" ;
4436import invariant from "tiny-invariant" ;
37+ import { FormErrorMessage } from "tw-components" ;
4538import { z } from "zod" ;
4639import { type Ecosystem , authOptions } from "../../../../../types" ;
4740import { useUpdateEcosystem } from "../../hooks/use-update-ecosystem" ;
@@ -53,6 +46,7 @@ type AuthOptionsFormData = {
5346 customHeaders : { key : string ; value : string } [ ] ;
5447 useSmartAccount : boolean ;
5548 sponsorGas : boolean ;
49+ chainIds : number [ ] ;
5650 accountFactoryType : "v0.6" | "v0.7" | "custom" ;
5751 customAccountFactoryAddress : string ;
5852} ;
@@ -66,6 +60,7 @@ export function AuthOptionsForm({ ecosystem }: { ecosystem: Ecosystem }) {
6660 customHeaders : ecosystem . customAuthOptions ?. authEndpoint ?. headers || [ ] ,
6761 useSmartAccount : ! ! ecosystem . smartAccountOptions ,
6862 sponsorGas : ecosystem . smartAccountOptions ?. sponsorGas || false ,
63+ chainIds : ecosystem . smartAccountOptions ?. chainIds ?? [ ] ,
6964 accountFactoryType :
7065 ecosystem . smartAccountOptions ?. accountFactoryAddress ===
7166 DEFAULT_ACCOUNT_FACTORY_V0_7
@@ -93,6 +88,7 @@ export function AuthOptionsForm({ ecosystem }: { ecosystem: Ecosystem }) {
9388 . optional ( ) ,
9489 useSmartAccount : z . boolean ( ) ,
9590 sponsorGas : z . boolean ( ) ,
91+ chainIds : z . array ( z . number ( ) ) ,
9692 accountFactoryType : z . enum ( [ "v0.6" , "v0.7" , "custom" ] ) ,
9793 customAccountFactoryAddress : z . string ( ) . optional ( ) ,
9894 } )
@@ -101,7 +97,8 @@ export function AuthOptionsForm({ ecosystem }: { ecosystem: Ecosystem }) {
10197 if (
10298 data . useSmartAccount &&
10399 data . customAccountFactoryAddress &&
104- ! isAddress ( data . customAccountFactoryAddress )
100+ ( ! isAddress ( data . customAccountFactoryAddress ) ||
101+ ! data . customAccountFactoryAddress )
105102 ) {
106103 return false ;
107104 }
@@ -112,6 +109,18 @@ export function AuthOptionsForm({ ecosystem }: { ecosystem: Ecosystem }) {
112109 path : [ "customAccountFactoryAddress" ] ,
113110 } ,
114111 )
112+ . refine (
113+ ( data ) => {
114+ if ( data . useSmartAccount && data . chainIds . length === 0 ) {
115+ return false ;
116+ }
117+ return true ;
118+ } ,
119+ {
120+ message : "Please select at least one chain ID" ,
121+ path : [ "chainIds" ] ,
122+ } ,
123+ )
115124 . refine (
116125 ( data ) => {
117126 if ( data . useCustomAuth && ! data . customAuthEndpoint ) {
@@ -142,17 +151,6 @@ export function AuthOptionsForm({ ecosystem }: { ecosystem: Ecosystem }) {
142151 } ,
143152 } ) ;
144153
145- const switchChain = useSwitchActiveWalletChain ( ) ;
146- const currentChain = useActiveWalletChain ( ) ;
147- if (
148- ecosystem . smartAccountOptions ?. defaultChainId &&
149- currentChain &&
150- currentChain . id !== ecosystem . smartAccountOptions ?. defaultChainId
151- ) {
152- const chainId = ecosystem . smartAccountOptions ?. defaultChainId ;
153- switchChain ( getCachedChainIfExists ( chainId ) ?? defineChain ( chainId ) ) ;
154- }
155-
156154 const onSubmit = ( data : AuthOptionsFormData ) => {
157155 let customAuthOptions : Ecosystem [ "customAuthOptions" ] | null = null ;
158156 if ( data . useCustomAuth && data . customAuthEndpoint ) {
@@ -182,22 +180,12 @@ export function AuthOptionsForm({ ecosystem }: { ecosystem: Ecosystem }) {
182180 accountFactoryAddress = DEFAULT_ACCOUNT_FACTORY_V0_7 ;
183181 break ;
184182 case "custom" :
185- if ( ! data . customAccountFactoryAddress ) {
186- toast . error ( "Please enter a custom account factory address" ) ;
187- return ;
188- }
189183 accountFactoryAddress = data . customAccountFactoryAddress ;
190184 break ;
191185 }
192186
193- if ( ! currentChain ?. id ) {
194- toast . error ( "Please select a default chain ID" ) ;
195- return ;
196- }
197187 smartAccountOptions = {
198- defaultChainId : currentChain ?. id ,
199- chainIds : [ ] , // TODO: remove once backend is updated
200-
188+ chainIds : data . chainIds ,
201189 sponsorGas : data . sponsorGas ,
202190 accountFactoryAddress,
203191 } ;
@@ -455,25 +443,32 @@ export function AuthOptionsForm({ ecosystem }: { ecosystem: Ecosystem }) {
455443 ) }
456444 />
457445
458- < FormItem >
459- < FormLabel > Default Chain ID</ FormLabel >
460- < FormControl >
461- < NetworkSelectorButton />
462- </ FormControl >
463- < FormDescription >
464- This will be the chain ID the smart account will be
465- initialized to on your{ " " }
466- < a
467- href = { `https://${ ecosystem . slug } .ecosystem.thirdweb.com` }
468- className = "text-link-foreground"
469- target = "_blank"
470- rel = "noreferrer"
471- >
472- ecosystem page
473- </ a >
474- .
475- </ FormDescription >
476- </ FormItem >
446+ < FormField
447+ control = { form . control }
448+ name = "chainIds"
449+ render = { ( { field } ) => (
450+ < FormItem >
451+ < FormLabel > Allowed Chain Ids</ FormLabel >
452+ < div className = "flex flex-col" >
453+ < MultiNetworkSelector
454+ selectedChainIds = { field . value }
455+ onChange = { field . onChange }
456+ />
457+ </ div >
458+ < FormDescription >
459+ Choose a default account factory or select custom to enter
460+ your own address
461+ </ FormDescription >
462+ < FormErrorMessage >
463+ {
464+ form . getFieldState ( "chainIds" , form . formState ) . error
465+ ?. message
466+ }
467+ </ FormErrorMessage >
468+ </ FormItem >
469+ ) }
470+ />
471+
477472 < FormField
478473 control = { form . control }
479474 name = "accountFactoryType"
0 commit comments