-
Notifications
You must be signed in to change notification settings - Fork 2
[demo_web] add isSignedInRef to track recent state in getKey callback #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3a12f8a
6287151
2119235
dd7cdb6
01c7729
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const COSMOS_CHAIN_ID = "cosmoshub-4"; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,52 @@ | ||
| import { useEffect, useRef, useState } from "react"; | ||
|
|
||
| import { COSMOS_CHAIN_ID } from "@oko-wallet-demo-web/constants/cosmos"; | ||
| import { useSDKState } from "@oko-wallet-demo-web/state/sdk"; | ||
| import { useUserInfoState } from "@oko-wallet-demo-web/state/user_info"; | ||
| import { useEffect, useState } from "react"; | ||
|
|
||
| export function useAddresses() { | ||
| const okoCosmos = useSDKState((state) => state.oko_cosmos); | ||
| const okoEth = useSDKState((state) => state.oko_eth); | ||
| const isSignedIn = useUserInfoState((state) => state.isSignedIn); | ||
| const isSignedRef = useRef(isSignedIn); | ||
| isSignedRef.current = isSignedIn; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why necessary?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ^ as I mentioned PR, Within the callback function, it is necessary to retrieve the most recent isSignedIn state.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @blacktoast I still don't see why this ref is necessary. Could we hop on a short call when you are available? |
||
|
|
||
| const [cosmosAddress, setCosmosAddress] = useState<string | null>(null); | ||
| const [ethAddress, setEthAddress] = useState<string | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| if (!isSignedIn) { | ||
| if (cosmosAddress) { | ||
| setCosmosAddress(null); | ||
| } | ||
|
|
||
| if (ethAddress) { | ||
| setEthAddress(null); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| const loadAddresses = async () => { | ||
| try { | ||
| const promises = []; | ||
|
|
||
| if (okoCosmos) { | ||
| promises.push( | ||
| okoCosmos | ||
| .getKey("cosmoshub-4") | ||
| .then((key) => setCosmosAddress(key.bech32Address)), | ||
| okoCosmos.getKey(COSMOS_CHAIN_ID).then((key) => { | ||
| if (isSignedRef.current) { | ||
| setCosmosAddress(key.bech32Address); | ||
| } | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| if (okoEth) { | ||
| promises.push( | ||
| okoEth.getAddress().then((addr) => setEthAddress(addr)), | ||
| okoEth.getAddress().then((addr) => { | ||
| if (isSignedRef.current) { | ||
| setEthAddress(addr); | ||
| } | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -38,16 +59,6 @@ export function useAddresses() { | |
| if (isSignedIn) { | ||
| loadAddresses(); | ||
| } | ||
|
|
||
| if (!isSignedIn) { | ||
| if (cosmosAddress) { | ||
| setCosmosAddress(null); | ||
| } | ||
|
|
||
| if (ethAddress) { | ||
| setEthAddress(null); | ||
| } | ||
| } | ||
| }, [isSignedIn, okoCosmos, okoEth]); | ||
|
|
||
| return { cosmosAddress, ethAddress }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
constants can be anything. Too general this file is.