2020-08-10 12:34:54 +02:00
|
|
|
import React from 'react'
|
|
|
|
import { useOcean } from '@oceanprotocol/react'
|
|
|
|
import { ConfigHelper } from '@oceanprotocol/lib'
|
|
|
|
import { useEffect } from 'react'
|
|
|
|
|
|
|
|
export function NetworkMonitor() {
|
2020-08-11 09:30:27 +02:00
|
|
|
const { connect, web3Provider } = useOcean()
|
2020-08-10 12:34:54 +02:00
|
|
|
|
2020-08-11 12:15:20 +02:00
|
|
|
const handleNetworkChanged = (chainId: number) => {
|
2020-08-11 09:30:27 +02:00
|
|
|
// const config = getOceanConfig(chainId)
|
2020-08-11 12:15:20 +02:00
|
|
|
// temp hack
|
|
|
|
let network = ''
|
|
|
|
switch (chainId) {
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
network = 'mainnet'
|
|
|
|
}
|
|
|
|
case 4: {
|
|
|
|
network = 'rinkeby'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const config = new ConfigHelper().getConfig(network)
|
2020-08-11 09:30:27 +02:00
|
|
|
connect(config)
|
|
|
|
}
|
|
|
|
useEffect(() => {
|
|
|
|
if (!web3Provider) return
|
2020-08-10 12:34:54 +02:00
|
|
|
|
2020-08-11 09:30:27 +02:00
|
|
|
web3Provider.on('chainChanged', handleNetworkChanged)
|
2020-08-10 12:34:54 +02:00
|
|
|
|
2020-08-11 09:30:27 +02:00
|
|
|
return () => {
|
|
|
|
web3Provider.removeListener('chainChanged', handleNetworkChanged)
|
|
|
|
}
|
|
|
|
}, [web3Provider])
|
2020-08-10 12:34:54 +02:00
|
|
|
|
2020-08-11 09:30:27 +02:00
|
|
|
return <></>
|
2020-08-10 12:34:54 +02:00
|
|
|
}
|