mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
another network change fix
This commit is contained in:
parent
a322c443ed
commit
9a321e2500
@ -1,16 +1,30 @@
|
|||||||
import React, { ReactElement, useEffect } from 'react'
|
import React, { ReactElement, useEffect } from 'react'
|
||||||
import { useOcean } from '@oceanprotocol/react'
|
import { useOcean } from '@oceanprotocol/react'
|
||||||
import { getOceanConfig } from './wrapRootElement'
|
import { getOceanConfig } from './wrapRootElement'
|
||||||
|
import appConfig from '../../app.config'
|
||||||
|
import { Logger } from '@oceanprotocol/lib'
|
||||||
|
|
||||||
export function NetworkMonitor(): ReactElement {
|
export function NetworkMonitor(): ReactElement {
|
||||||
|
const { metadataStoreUri } = appConfig
|
||||||
const { connect, web3Provider } = useOcean()
|
const { connect, web3Provider } = useOcean()
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!web3Provider) return
|
if (!web3Provider) return
|
||||||
|
|
||||||
async function handleNetworkChanged(chainId: string) {
|
async function handleNetworkChanged(chainId: string) {
|
||||||
const config = getOceanConfig(Number(chainId.replace('0x', '')))
|
const initialConfig = getOceanConfig(Number(chainId.replace('0x', '')))
|
||||||
await connect(config)
|
|
||||||
|
const newConfig = {
|
||||||
|
...initialConfig,
|
||||||
|
// add metadataStoreUri only when defined
|
||||||
|
...(metadataStoreUri && { metadataStoreUri })
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await connect(newConfig)
|
||||||
|
} catch (error) {
|
||||||
|
Logger.error(error.message)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
web3Provider.on('chainChanged', handleNetworkChanged)
|
web3Provider.on('chainChanged', handleNetworkChanged)
|
||||||
@ -18,7 +32,7 @@ export function NetworkMonitor(): ReactElement {
|
|||||||
return () => {
|
return () => {
|
||||||
web3Provider.removeListener('chainChanged', handleNetworkChanged)
|
web3Provider.removeListener('chainChanged', handleNetworkChanged)
|
||||||
}
|
}
|
||||||
}, [web3Provider, connect])
|
}, [web3Provider, connect, metadataStoreUri])
|
||||||
|
|
||||||
return <></>
|
return <></>
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user