-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: Wallet not found sometimes after refresh page * fix: wallet not found
- Loading branch information
Showing
1 changed file
with
19 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,37 @@ | ||
import { merge } from "lodash-es"; | ||
import { useEffect, useState } from "react"; | ||
import { createGlobalState, useMountedState } from "react-use"; | ||
import { useCallback, useEffect, useState } from "react"; | ||
import { createGlobalState } from "react-use"; | ||
|
||
const useGlobalInjectedWeb3 = createGlobalState(null); | ||
|
||
export default function useInjectedWeb3() { | ||
const isMounted = useMountedState(); | ||
const [injectedWeb3, _setInjectedWeb3] = useGlobalInjectedWeb3(); | ||
const [loading, setLoading] = useState(true); | ||
|
||
function setInjectedWeb3(value) { | ||
_setInjectedWeb3(merge(injectedWeb3, value)); | ||
} | ||
const setInjectedWeb3 = useCallback( | ||
(newValue) => { | ||
_setInjectedWeb3((oldValue) => merge(oldValue, newValue)); | ||
}, | ||
[_setInjectedWeb3], | ||
); | ||
|
||
useEffect(() => { | ||
function handleWeb3() { | ||
setLoading(false); | ||
setInjectedWeb3(window.injectedWeb3); | ||
} | ||
|
||
if (isMounted()) { | ||
if (typeof window !== "undefined") { | ||
if (window.injectedWeb3) { | ||
handleWeb3(); | ||
} | ||
} else { | ||
setTimeout(() => { | ||
handleWeb3(); | ||
}, 1000); | ||
} | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [isMounted]); | ||
handleWeb3(); | ||
const timeout1 = setTimeout(handleWeb3, 1000); | ||
const timeout2 = setTimeout(handleWeb3, 2000); | ||
const timeout3 = setTimeout(handleWeb3, 5000); | ||
|
||
return () => { | ||
clearTimeout(timeout1); | ||
clearTimeout(timeout2); | ||
clearTimeout(timeout3); | ||
}; | ||
}, [setInjectedWeb3]); | ||
|
||
return { loading, injectedWeb3, setInjectedWeb3 }; | ||
} |