This repository has been archived by the owner on Jan 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* deal with incorrect network * PR changes, thank you to the legend @Velenir * rel noopener Update src/hooks/useNetworkCheck.tsx Co-authored-by: Velenir <Velenir@users.noreply.github.com> * block toast click close or button Co-authored-by: Velenir <Velenir@users.noreply.github.com>
- Loading branch information
Showing
2 changed files
with
104 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React from 'react' | ||
import { toast } from 'toastify' | ||
|
||
import { useWalletConnection } from 'hooks/useWalletConnection' | ||
|
||
import { Network } from 'types' | ||
|
||
const { config: networkIdList } = CONFIG.exchangeContractConfig | ||
const availableNetworks = new Set(networkIdList.map(({ networkId }) => networkId)) | ||
const availableNetworksName = networkIdList | ||
.map(({ networkId: id }, idx, arr) => (idx === arr.length - 1 ? 'or ' : '') + Network[id]) | ||
.join(', ') | ||
|
||
const NetworkErrorMessage: React.FC = () => ( | ||
<div> | ||
<div>Please switch to a supported network!</div> | ||
<strong>{availableNetworksName}</strong> | ||
<br /> | ||
<br /> | ||
<div> | ||
Click{' '} | ||
<a href="https://github.com/gnosis/dex-react/wiki#-compatible-networks" target="_blank" rel="noopener noreferrer"> | ||
here | ||
</a>{' '} | ||
for more information | ||
</div> | ||
</div> | ||
) | ||
|
||
function runCheckOnValidNetworks(networkId?: number): boolean { | ||
if (!networkId) return true | ||
|
||
return availableNetworks.has(networkId) | ||
} | ||
|
||
const useNetworkCheck = (): void => { | ||
const { networkId } = useWalletConnection() | ||
|
||
React.useEffect(() => { | ||
const isValidOrNonNetwork = runCheckOnValidNetworks(networkId) | ||
|
||
if (isValidOrNonNetwork) return | ||
|
||
const promisedToastID = toast.error(<NetworkErrorMessage />, { | ||
autoClose: false, | ||
closeOnClick: false, | ||
closeButton: false, | ||
}) | ||
|
||
return (): void => { | ||
promisedToastID.then((toastID) => toast.dismiss(toastID)) | ||
} | ||
}, [networkId]) | ||
} | ||
|
||
export default useNetworkCheck |