forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetwork-provider.tsx
More file actions
73 lines (61 loc) · 2.05 KB
/
Copy pathnetwork-provider.tsx
File metadata and controls
73 lines (61 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use client'
import {
createContext,
useCallback,
useContext,
useEffect,
useMemo,
useState,
type ReactNode,
} from 'react'
import { type NetworkName, getNetworkConfig } from '@/lib/stellar'
const NETWORK_KEY = 'flowstar:network'
interface NetworkContextValue {
network: NetworkName
setNetwork: (network: NetworkName) => void
config: ReturnType<typeof getNetworkConfig>
isMockMode: boolean
}
const NetworkContext = createContext<NetworkContextValue | null>(null)
export function NetworkProvider({ children }: { children: ReactNode }) {
const [network, setNetworkState] = useState<NetworkName>('testnet')
const [mounted, setMounted] = useState(false)
useEffect(() => {
setMounted(true)
const saved = localStorage.getItem(NETWORK_KEY) as NetworkName | null
if (saved && ['testnet', 'mainnet'].includes(saved)) {
setNetworkState(saved)
}
}, [])
const setNetwork = useCallback((newNetwork: NetworkName) => {
setNetworkState(newNetwork)
localStorage.setItem(NETWORK_KEY, newNetwork)
}, [])
const config = useMemo(() => getNetworkConfig(network), [network])
const isMockMode = !config.streamContractId
// Startup check: warn (in non-production) when the contract ID is missing
useEffect(() => {
if (mounted && isMockMode && process.env.NODE_ENV !== 'production') {
console.warn(
`[FlowStar] No contract ID configured for ${network} — running in MOCK mode. ` +
'Streams are kept in memory only and reset on reload. ' +
'Copy .env.local.example to .env.local and set the appropriate contract ID.',
)
}
}, [network, isMockMode, mounted])
const value = useMemo<NetworkContextValue>(
() => ({
network,
setNetwork,
config,
isMockMode,
}),
[network, setNetwork, config, isMockMode],
)
return <NetworkContext.Provider value={value}>{children}</NetworkContext.Provider>
}
export function useNetwork() {
const ctx = useContext(NetworkContext)
if (!ctx) throw new Error('useNetwork must be used within a NetworkProvider')
return ctx
}