Skip to content

Commit 9499022

Browse files
committed
fix missing discovered network set to appkit
Signed-off-by: p4u <pau@dabax.net>
1 parent 9c1784e commit 9499022

3 files changed

Lines changed: 43 additions & 35 deletions

File tree

src/Layout.tsx

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,66 @@ import { initializeAppKit } from '~lib/appkit-miniapp'
99
import { getConfiguredNetworkAsync } from '~lib/network-config'
1010

1111
export 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'>

src/lib/appkit-miniapp.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EthersAdapter } from '@reown/appkit-adapter-ethers'
22
import { AppKit, createAppKit } from '@reown/appkit/react'
3-
import { getConfiguredNetwork } from './network-config'
3+
import type { AppKitNetwork } from '@reown/appkit/networks'
44

55
// 1. Get projectId from environment
66
const projectId = import.meta.env.WALLETCONNECT_PROJECT_ID
@@ -13,23 +13,21 @@ const metadata = {
1313
icons: ['/images/davinci-icon-small.png'],
1414
}
1515

16-
// 3. Get configured network
17-
const network = getConfiguredNetwork()
18-
19-
// 4. Create Ethers Adapter with conditional provider
16+
// 3. Create Ethers Adapter with conditional provider
2017
const ethersAdapter = new EthersAdapter()
2118

2219
let appKit: AppKit | null = null
2320

2421
/**
25-
* Initialize AppKit - simplified version since mini app logic is now in context
22+
* Initialize AppKit with the detected network
23+
* @param network The network to initialize AppKit with
2624
*/
27-
export async function initializeAppKit() {
25+
export async function initializeAppKit(network: AppKitNetwork) {
2826
if (appKit) {
2927
return appKit
3028
}
3129

32-
console.log('Initializing AppKit...')
30+
console.log('Initializing AppKit with network:', network.name)
3331

3432
// Create standard AppKit (works for both mini app and regular web)
3533
appKit = createAppKit({

src/main.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import { OfflineIndicator } from '~components/offline-indicator'
77
import { VocdoniApiProvider } from '~components/vocdoni-api-context'
88
import { MiniAppProvider } from '~contexts/MiniAppContext'
99
import { RouterProvider } from '~router'
10-
// Initialize AppKit
11-
import '~lib/appkit'
1210

1311
import '@fontsource/averia-libre/400.css'
1412
import '@fontsource/averia-libre/700.css'

0 commit comments

Comments
 (0)