11"use client" ;
2- import { MultiNetworkSelector } from "@/components/blocks/NetworkSelectors" ;
32import { SettingsCard } from "@/components/blocks/SettingsCard" ;
43import { Button } from "@/components/ui/button" ;
54import { Checkbox } from "@/components/ui/checkbox" ;
@@ -24,17 +23,22 @@ import { Skeleton } from "@/components/ui/skeleton";
2423import { Switch } from "@/components/ui/switch" ;
2524import { cn } from "@/lib/utils" ;
2625import { 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 { getCachedChain } from "thirdweb/dist/types/chains/utils" ;
32+ import {
33+ useActiveWalletChain ,
34+ useSwitchActiveWalletChain ,
35+ } from "thirdweb/react" ;
3136import { getSocialIcon } from "thirdweb/wallets/in-app" ;
3237import {
3338 DEFAULT_ACCOUNT_FACTORY_V0_6 ,
3439 DEFAULT_ACCOUNT_FACTORY_V0_7 ,
3540} from "thirdweb/wallets/smart" ;
3641import invariant from "tiny-invariant" ;
37- import { FormErrorMessage } from "tw-components" ;
3842import { z } from "zod" ;
3943import { type Ecosystem , authOptions } from "../../../../../types" ;
4044import { useUpdateEcosystem } from "../../hooks/use-update-ecosystem" ;
@@ -46,7 +50,6 @@ type AuthOptionsFormData = {
4650 customHeaders : { key : string ; value : string } [ ] ;
4751 useSmartAccount : boolean ;
4852 sponsorGas : boolean ;
49- chainIds : number [ ] ;
5053 accountFactoryType : "v0.6" | "v0.7" | "custom" ;
5154 customAccountFactoryAddress : string ;
5255} ;
@@ -60,7 +63,6 @@ export function AuthOptionsForm({ ecosystem }: { ecosystem: Ecosystem }) {
6063 customHeaders : ecosystem . customAuthOptions ?. authEndpoint ?. headers || [ ] ,
6164 useSmartAccount : ! ! ecosystem . smartAccountOptions ,
6265 sponsorGas : ecosystem . smartAccountOptions ?. sponsorGas || false ,
63- chainIds : ecosystem . smartAccountOptions ?. chainIds ?? [ ] ,
6466 accountFactoryType :
6567 ecosystem . smartAccountOptions ?. accountFactoryAddress ===
6668 DEFAULT_ACCOUNT_FACTORY_V0_7
@@ -88,7 +90,6 @@ export function AuthOptionsForm({ ecosystem }: { ecosystem: Ecosystem }) {
8890 . optional ( ) ,
8991 useSmartAccount : z . boolean ( ) ,
9092 sponsorGas : z . boolean ( ) ,
91- chainIds : z . array ( z . number ( ) ) ,
9293 accountFactoryType : z . enum ( [ "v0.6" , "v0.7" , "custom" ] ) ,
9394 customAccountFactoryAddress : z . string ( ) . optional ( ) ,
9495 } )
@@ -97,8 +98,7 @@ export function AuthOptionsForm({ ecosystem }: { ecosystem: Ecosystem }) {
9798 if (
9899 data . useSmartAccount &&
99100 data . customAccountFactoryAddress &&
100- ( ! isAddress ( data . customAccountFactoryAddress ) ||
101- ! data . customAccountFactoryAddress )
101+ ! isAddress ( data . customAccountFactoryAddress )
102102 ) {
103103 return false ;
104104 }
@@ -109,18 +109,6 @@ export function AuthOptionsForm({ ecosystem }: { ecosystem: Ecosystem }) {
109109 path : [ "customAccountFactoryAddress" ] ,
110110 } ,
111111 )
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- )
124112 . refine (
125113 ( data ) => {
126114 if ( data . useCustomAuth && ! data . customAuthEndpoint ) {
@@ -151,6 +139,17 @@ export function AuthOptionsForm({ ecosystem }: { ecosystem: Ecosystem }) {
151139 } ,
152140 } ) ;
153141
142+ const switchChain = useSwitchActiveWalletChain ( ) ;
143+ const currentChain = useActiveWalletChain ( ) ;
144+ if (
145+ ecosystem . smartAccountOptions ?. defaultChainId &&
146+ currentChain &&
147+ currentChain . id !== ecosystem . smartAccountOptions ?. defaultChainId
148+ ) {
149+ const chainId = ecosystem . smartAccountOptions ?. defaultChainId ;
150+ switchChain ( getCachedChain ( chainId ) ) ;
151+ }
152+
154153 const onSubmit = ( data : AuthOptionsFormData ) => {
155154 let customAuthOptions : Ecosystem [ "customAuthOptions" ] | null = null ;
156155 if ( data . useCustomAuth && data . customAuthEndpoint ) {
@@ -180,12 +179,22 @@ export function AuthOptionsForm({ ecosystem }: { ecosystem: Ecosystem }) {
180179 accountFactoryAddress = DEFAULT_ACCOUNT_FACTORY_V0_7 ;
181180 break ;
182181 case "custom" :
182+ if ( ! data . customAccountFactoryAddress ) {
183+ toast . error ( "Please enter a custom account factory address" ) ;
184+ return ;
185+ }
183186 accountFactoryAddress = data . customAccountFactoryAddress ;
184187 break ;
185188 }
186189
190+ if ( ! currentChain ?. id ) {
191+ toast . error ( "Please select a default chain ID" ) ;
192+ return ;
193+ }
187194 smartAccountOptions = {
188- chainIds : data . chainIds ,
195+ defaultChainId : currentChain ?. id ,
196+ chainIds : [ ] , // TODO: remove once backend is updated
197+
189198 sponsorGas : data . sponsorGas ,
190199 accountFactoryAddress,
191200 } ;
@@ -443,42 +452,25 @@ export function AuthOptionsForm({ ecosystem }: { ecosystem: Ecosystem }) {
443452 ) }
444453 />
445454
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- Select which blockchain networks will be supported by the
460- smart account. The first value will be the chain that
461- users are connected to by default when logging into the
462- < a
463- href = { `https://${ ecosystem . slug } .ecosystem.thirdweb.com` }
464- className = "text-link-foreground"
465- target = "_blank"
466- rel = "noreferrer"
467- >
468- ecosystem homepage
469- </ a >
470- .
471- </ FormDescription >
472- < FormErrorMessage >
473- {
474- form . getFieldState ( "chainIds" , form . formState ) . error
475- ?. message
476- }
477- </ FormErrorMessage >
478- </ FormItem >
479- ) }
480- />
481-
455+ < FormItem >
456+ < FormLabel > Default Chain ID</ FormLabel >
457+ < FormControl >
458+ < NetworkSelectorButton />
459+ </ FormControl >
460+ < FormDescription >
461+ This will be the chain ID the smart account will be
462+ initialized to on your{ " " }
463+ < a
464+ href = { `https://${ ecosystem . slug } .ecosystem.thirdweb.com` }
465+ className = "text-link-foreground"
466+ target = "_blank"
467+ rel = "noreferrer"
468+ >
469+ ecosystem page
470+ </ a >
471+ .
472+ </ FormDescription >
473+ </ FormItem >
482474 < FormField
483475 control = { form . control }
484476 name = "accountFactoryType"
0 commit comments