1- import { createContext , FC , useContext , useEffect , useRef } from "react"
1+ import {
2+ createContext ,
3+ FC ,
4+ useContext ,
5+ useEffect ,
6+ useRef ,
7+ useState ,
8+ } from "react"
29import { getThresholdLibProvider , threshold } from "../utils/getThresholdLib"
310import { useLedgerLiveApp } from "./LedgerLiveAppContext"
411import { useIsActive } from "../hooks/useIsActive"
512import { useIsEmbed } from "../hooks/useIsEmbed"
613import { getEthereumDefaultProviderChainId } from "../utils/getEnvVariable"
714import { useWeb3React } from "@web3-react/core"
815import { ChainName } from "../threshold-ts/types"
9- import { isL2Network } from "../networks/utils"
16+ import {
17+ getEthereumNetworkNameFromChainId ,
18+ isL2Network ,
19+ } from "../networks/utils"
1020import { useNonEVMConnection } from "../hooks/useNonEVMConnection"
1121
1222const defaultCrossChainConfig = {
@@ -22,6 +32,7 @@ export const useThreshold = () => {
2232}
2333
2434export const ThresholdProvider : FC = ( { children } ) => {
35+ const [ thresholdState , setThresholdState ] = useState ( threshold )
2536 const { library } = useWeb3React ( )
2637 const hasThresholdLibConfigBeenUpdated = useRef ( false )
2738 const { ledgerLiveAppEthereumSigner } = useLedgerLiveApp ( )
@@ -30,6 +41,13 @@ export const ThresholdProvider: FC = ({ children }) => {
3041 useNonEVMConnection ( )
3142 const { isEmbed } = useIsEmbed ( )
3243
44+ useEffect ( ( ) => {
45+ const unsubscribe = threshold . subscribe ( ( ) =>
46+ setThresholdState ( Object . create ( threshold ) )
47+ )
48+ return unsubscribe
49+ } , [ ] )
50+
3351 useEffect ( ( ) => {
3452 if ( isActive && chainId ) {
3553 threshold . updateConfig ( {
@@ -44,39 +62,39 @@ export const ThresholdProvider: FC = ({ children }) => {
4462 bitcoin : threshold . config . bitcoin ,
4563 crossChain : {
4664 ...defaultCrossChainConfig ,
47- chainName : isL2Network ( chainId ) ? ChainName . Ethereum : null ,
65+ chainName : getEthereumNetworkNameFromChainId ( chainId ) as ChainName ,
4866 isCrossChain : isL2Network ( chainId ) ,
4967 } ,
5068 } )
5169 hasThresholdLibConfigBeenUpdated . current = true
5270 }
5371
54- if ( ! isActive && hasThresholdLibConfigBeenUpdated . current ) {
72+ if ( isNonEVMActive ) {
5573 threshold . updateConfig ( {
56- ethereum : {
57- ...threshold . config . ethereum ,
58- ethereumProviderOrSigner : getThresholdLibProvider ( ) ,
59- account : undefined ,
60- chainId : getEthereumDefaultProviderChainId ( ) ,
61- } ,
74+ ethereum : threshold . config . ethereum ,
6275 bitcoin : threshold . config . bitcoin ,
6376 crossChain : {
64- ...threshold . config . crossChain ,
77+ isCrossChain : true ,
78+ chainName : nonEVMChainName as Exclude < ChainName , ChainName . Ethereum > ,
79+ nonEVMProvider : nonEVMProvider ,
6580 } ,
6681 } )
67- hasThresholdLibConfigBeenUpdated . current = false
6882 }
6983
70- if ( isNonEVMActive ) {
84+ if ( ! isActive && hasThresholdLibConfigBeenUpdated . current ) {
7185 threshold . updateConfig ( {
72- ethereum : threshold . config . ethereum ,
86+ ethereum : {
87+ ...threshold . config . ethereum ,
88+ ethereumProviderOrSigner : getThresholdLibProvider ( ) ,
89+ account : undefined ,
90+ chainId : undefined ,
91+ } ,
7392 bitcoin : threshold . config . bitcoin ,
7493 crossChain : {
75- isCrossChain : true ,
76- chainName : nonEVMChainName as Exclude < ChainName , ChainName . Ethereum > ,
77- nonEVMProvider : nonEVMProvider ,
94+ ...defaultCrossChainConfig ,
7895 } ,
7996 } )
97+ hasThresholdLibConfigBeenUpdated . current = false
8098 }
8199 } , [
82100 isActive ,
@@ -89,7 +107,7 @@ export const ThresholdProvider: FC = ({ children }) => {
89107 ] )
90108
91109 return (
92- < ThresholdContext . Provider value = { threshold } >
110+ < ThresholdContext . Provider value = { thresholdState } >
93111 { children }
94112 </ ThresholdContext . Provider >
95113 )
0 commit comments