Skip to content

Commit

Permalink
Some bug fixes (#1351)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso authored Aug 28, 2023
1 parent fb4ce81 commit 6799196
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 51 deletions.
3 changes: 1 addition & 2 deletions packages/state/recoil/selectors/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,8 @@ export const usdPriceSelector = selectorFamily<
case ChainId.JunoMainnet:
return get(wyndUsdPriceSelector(denomOrAddress))
case ChainId.OsmosisMainnet:
// TODO(stargaze): get osmosis denoms for stargaze assets
case ChainId.StargazeMainnet:
return get(osmosisUsdPriceSelector(denomOrAddress))
// TODO(stargaze): get osmosis denoms for stargaze assets
}
},
})
Expand Down
18 changes: 12 additions & 6 deletions packages/stateful/command/components/CommandModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ export const CommandModal = ({
// and each useSections hook is different, we need to tell the component to
// re-render as if it's a new component.
const [contextChangeCount, setContextChangeCount] = useState(0)
const setContexts: typeof _setContexts = useCallback((...args) => {
setContextChangeCount((count) => count + 1)
_setContexts(...args)
// Focus input when adding new context.
searchBarRef.current?.focus()
}, [])
const setContexts: typeof _setContexts = useCallback(
(...args) => {
setContextChangeCount((count) => count + 1)
_setContexts(...args)

// Focus input when adding new context if visible.
if (props.visible) {
searchBarRef.current?.focus()
}
},
[props.visible]
)

// Reset with new root context whenever makeRootContext changes. Also
// initializes first time.
Expand Down
52 changes: 12 additions & 40 deletions packages/stateful/hooks/useWallet.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { Chain } from '@chain-registry/types'
import { toHex } from '@cosmjs/encoding'
import {
ChainContext,
ChainWalletContext,
WalletAccount,
} from '@cosmos-kit/core'
import { ChainContext, WalletAccount } from '@cosmos-kit/core'
import { useChain } from '@cosmos-kit/react-lite'
import { getChainWalletContext } from '@cosmos-kit/react-lite/cjs/utils'
import { useEffect, useMemo, useRef, useState } from 'react'
import { useRecoilValue } from 'recoil'

Expand Down Expand Up @@ -47,43 +42,21 @@ export const useWallet = ({
? getChainForChainId(chainId)
: currentChain || getChainForChainId(walletChainId)

// Make sure to enable chain on currently connected wallet.
// TODO this is hacky cosmos-kit should have a better solution
const connectedWallet = useChain(getChainForChainId(walletChainId).chain_name)
const chainWallet = connectedWallet.chainWallet?.mainWallet.getChainWallet(
chain.chain_name
)
chainWallet?.activate()

// Only try to connect once per wallet address. If address changes, try to
// connect again.
const triedToConnect = useRef<string | undefined>(undefined)
if (
connectedWallet.isWalletConnected &&
chainWallet &&
!chainWallet.isWalletConnected &&
(!triedToConnect.current ||
triedToConnect.current === connectedWallet.address)
) {
triedToConnect.current = connectedWallet.address
chainWallet.connect().catch(console.error)
}
const _walletChain = useChain(chain.chain_name)

// Memoize wallet chain since it changes every render. The hook above forces
// re-render when address changes, so this is safe.
const walletChainRef = useRef<ChainWalletContext | undefined>(undefined)
walletChainRef.current = chainWallet
? getChainWalletContext(chain.chain_id, chainWallet)
: undefined
const walletChainRef = useRef(_walletChain)
walletChainRef.current = _walletChain

const [account, setAccount] = useState<WalletAccount>()
const [hexPublicKeyData, setHexPublicKeyData] = useState<string>()

const hexPublicKeyFromChain = useCachedLoading(
walletChainRef.current?.address && loadAccount
_walletChain.address && loadAccount
? walletHexPublicKeySelector({
walletAddress: walletChainRef.current.address,
chainId: walletChainRef.current.chain.chain_id,
walletAddress: _walletChain.address,
chainId: _walletChain.chain.chain_id,
})
: undefined,
undefined
Expand All @@ -94,7 +67,7 @@ export const useWallet = ({
return
}

if (!walletChainRef.current?.isWalletConnected) {
if (!walletChainRef.current.isWalletConnected) {
setAccount(undefined)
setHexPublicKeyData(undefined)
return
Expand All @@ -104,7 +77,7 @@ export const useWallet = ({
if (account?.address !== walletChainRef.current.address) {
;(async () => {
try {
const account = await walletChainRef.current?.getAccount()
const account = await walletChainRef.current.getAccount()
setAccount(account)
setHexPublicKeyData(account && toHex(account.pubkey))
} catch (err) {
Expand All @@ -115,14 +88,13 @@ export const useWallet = ({
}, [
account?.address,
loadAccount,
walletChainRef.current?.address,
walletChainRef.current?.chain.chain_id,
walletChainRef.current?.status,
walletChainRef.current.address,
walletChainRef.current.chain.chain_id,
walletChainRef.current.status,
])

const response = useMemo(
(): UseWalletReturn => ({
...connectedWallet,
...walletChainRef.current,
// Use chain from our version of the chain-registry.
chain,
Expand Down
2 changes: 1 addition & 1 deletion packages/stateless/components/inputs/ImageSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const ImageSelectorModal = <
<TextInput
// Auto focus does not work on mobile Safari by design
// (https://bugs.webkit.org/show_bug.cgi?id=195884#c4).
autoFocus
autoFocus={visible}
error={error}
fieldName={fieldName}
onKeyDown={(e) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/stateless/components/modals/NftSelectionModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export const NftSelectionModal = <T extends NftCardInfo>({
{headerDisplay}

<SearchBar
autoFocus
autoFocus={modalProps.visible}
placeholder={t('info.searchNftsPlaceholder')}
{...searchBarProps}
/>
Expand Down
2 changes: 1 addition & 1 deletion packages/stateless/components/modals/TokenDepositModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export const TokenDepositModal = ({
<NumberInput
// Auto focus does not work on mobile Safari by design
// (https://bugs.webkit.org/show_bug.cgi?id=195884#c4).
autoFocus
autoFocus={modalProps.visible}
max={loadingBalance.loading ? undefined : loadingBalance.data.amount}
min={min}
onInput={(event) =>
Expand Down

2 comments on commit 6799196

@vercel
Copy link

@vercel vercel bot commented on 6799196 Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 6799196 Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.