-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
414fedb
commit 7e2f292
Showing
8 changed files
with
100 additions
and
5 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
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,47 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useAppStore } from '../mobx'; | ||
import { getNetworkInSnap, updateNetworkInSnap } from '../lib/snap'; | ||
import { BitcoinNetwork } from '../interface'; | ||
import { AppStatus } from '../mobx/runtime'; | ||
import { capitalize } from '../utils/stringHelper'; | ||
|
||
export const useNetworkCheck = () => { | ||
const { current, persistDataLoaded, settings: { setNetwork }, switchToAccount, runtime: { setStatus } } = useAppStore(); | ||
const [networkMessage, setNetworkMessage] = useState<string>(''); | ||
const [isSettingNetwork, setIsSettingNetwork] = useState<boolean>(false); | ||
|
||
const switchNetworkAndUpdateState = (targetNetwork: BitcoinNetwork) => { | ||
setNetwork(targetNetwork); | ||
current && switchToAccount(current.mfp, current.scriptType, targetNetwork); | ||
setStatus(AppStatus.FetchBalance); | ||
}; | ||
|
||
useEffect(() => { | ||
if(!persistDataLoaded){ | ||
return; | ||
} | ||
|
||
if(current){ | ||
getNetworkInSnap().then(network => { | ||
if(network === ''){ | ||
setIsSettingNetwork(true); | ||
updateNetworkInSnap(current.network).then(() => { | ||
setIsSettingNetwork(false); | ||
}); | ||
} else { | ||
const networkInSnap = network === 'test' ? BitcoinNetwork.Test : BitcoinNetwork.Main; | ||
const isNetworkTheSame = networkInSnap === current.network; | ||
if (!isNetworkTheSame) { | ||
switchNetworkAndUpdateState(networkInSnap); | ||
setNetworkMessage(`Currently In ${capitalize(networkInSnap)}`); | ||
} | ||
} | ||
}); | ||
} | ||
}, [persistDataLoaded, current]); | ||
|
||
return { | ||
isSettingNetwork, | ||
networkMessage | ||
}; | ||
}; |
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
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,3 @@ | ||
export const capitalize = (str: string) => { | ||
return str.charAt(0).toUpperCase() + str.slice(1); | ||
}; |