@@ -9,54 +9,66 @@ import { initializeAppKit } from '~lib/appkit-miniapp'
99import { getConfiguredNetworkAsync } from '~lib/network-config'
1010
1111export function Layout ( ) {
12- const { caipNetwork, switchNetwork } = useAppKitNetwork ( )
1312 const { isInitialized } = useMiniApp ( )
14- const [ appKitInitialized , setAppKitInitialized ] = useState ( false )
13+ const [ appKitReady , setAppKitReady ] = useState ( false )
1514 const [ detectedNetwork , setDetectedNetwork ] = useState < AppKitNetwork | null > ( null )
1615 const [ shouldAutoSwitch , setShouldAutoSwitch ] = useState ( false )
1716
18- // Detect network once on mount and cache it
17+ // Detect network and initialize AppKit before rendering
1918 useEffect ( ( ) => {
20- const initializeNetworkDetection = async ( ) => {
19+ const initialize = async ( ) => {
20+ if ( ! isInitialized ) return
21+
2122 try {
2223 // Check if network is manually configured
2324 const isManuallyConfigured = ! ! import . meta. env . ETHEREUM_NETWORK
2425
25- // This will detect sequencer network and validate against env var
26+ // Detect sequencer network and validate against env var
2627 const network = await getConfiguredNetworkAsync ( )
2728 setDetectedNetwork ( network )
2829
2930 // Only auto-switch if network was NOT manually configured
3031 setShouldAutoSwitch ( ! isManuallyConfigured )
31- } catch ( error ) {
32- console . error ( 'Failed to initialize network detection:' , error )
33- }
34- }
35-
36- initializeNetworkDetection ( )
37- } , [ ] )
38-
39- // Initialize AppKit once mini app context is ready
40- useEffect ( ( ) => {
41- const initialize = async ( ) => {
42- if ( ! isInitialized ) return
4332
44- try {
45- // Initialize AppKit with conditional Farcaster support
46- await initializeAppKit ( )
47- setAppKitInitialized ( true )
33+ // Initialize AppKit with the detected network
34+ await initializeAppKit ( network )
35+ setAppKitReady ( true )
4836 } catch ( error ) {
49- console . error ( 'Failed to initialize AppKit:' , error )
50- setAppKitInitialized ( true ) // Still allow app to continue
37+ console . error ( 'Failed to initialize network detection or AppKit:' , error )
38+ setAppKitReady ( true ) // Still allow app to continue
5139 }
5240 }
5341
5442 initialize ( )
5543 } , [ isInitialized ] )
5644
45+ // Don't render until AppKit is ready
46+ if ( ! appKitReady ) {
47+ return (
48+ < div className = 'min-h-screen bg-davinci-paper-base/30 flex items-center justify-center' >
49+ < div className = 'text-center' >
50+ < div className = 'animate-spin rounded-full h-12 w-12 border-b-2 border-davinci-black-alt mx-auto mb-4' > </ div >
51+ < p className = 'text-davinci-black-alt' > Initializing...</ p >
52+ </ div >
53+ </ div >
54+ )
55+ }
56+
57+ return < LayoutContent detectedNetwork = { detectedNetwork } shouldAutoSwitch = { shouldAutoSwitch } />
58+ }
59+
60+ function LayoutContent ( {
61+ detectedNetwork,
62+ shouldAutoSwitch
63+ } : {
64+ detectedNetwork : AppKitNetwork | null
65+ shouldAutoSwitch : boolean
66+ } ) {
67+ const { caipNetwork, switchNetwork } = useAppKitNetwork ( )
68+
5769 // Switch to detected network only if auto-detection is enabled
5870 useEffect ( ( ) => {
59- if ( ! caipNetwork || ! appKitInitialized || ! detectedNetwork || ! shouldAutoSwitch ) return
71+ if ( ! caipNetwork || ! detectedNetwork || ! shouldAutoSwitch ) return
6072
6173 // Only switch if using auto-detection (no manual env var configuration)
6274 if ( caipNetwork . id !== detectedNetwork . id ) {
@@ -66,7 +78,7 @@ export function Layout() {
6678 console . error ( 'Failed to switch to detected network:' , error )
6779 }
6880 }
69- } , [ caipNetwork , switchNetwork , appKitInitialized , detectedNetwork , shouldAutoSwitch ] )
81+ } , [ caipNetwork , switchNetwork , detectedNetwork , shouldAutoSwitch ] )
7082
7183 return (
7284 < div className = 'min-h-screen bg-davinci-paper-base/30 flex flex-col font-work-sans' >
0 commit comments