2020-09-04 11:24:11 +02:00
|
|
|
import React, { useCallback } from 'react'
|
2020-08-10 12:34:54 +02:00
|
|
|
import { useOcean } from '@oceanprotocol/react'
|
|
|
|
import { ConfigHelper } from '@oceanprotocol/lib'
|
|
|
|
import { useEffect } from 'react'
|
|
|
|
|
2020-09-04 11:24:11 +02:00
|
|
|
export const NetworkMonitor = () => {
|
2020-08-11 09:30:27 +02:00
|
|
|
const { connect, web3Provider } = useOcean()
|
2020-08-10 12:34:54 +02:00
|
|
|
|
2020-09-07 13:11:46 +02:00
|
|
|
const handleNetworkChanged = useCallback(
|
|
|
|
(chainId: number) => {
|
2020-09-09 16:10:03 +02:00
|
|
|
const config = new ConfigHelper().getConfig(chainId)
|
2020-09-07 13:11:46 +02:00
|
|
|
connect(config)
|
|
|
|
},
|
|
|
|
[connect]
|
|
|
|
)
|
2020-09-04 11:24:11 +02:00
|
|
|
|
2020-08-11 09:30:27 +02:00
|
|
|
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)
|
|
|
|
}
|
2020-09-04 11:24:11 +02:00
|
|
|
}, [web3Provider, handleNetworkChanged])
|
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
|
|
|
}
|