Skip to content

Commit

Permalink
Merge pull request #1322 from DA0-DA0/development
Browse files Browse the repository at this point in the history
Deploy wallet connection improvement
  • Loading branch information
NoahSaso committed Aug 3, 2023
2 parents 692a089 + 4f860c6 commit d622929
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
37 changes: 24 additions & 13 deletions packages/stateful/components/WalletProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import {
useRef,
} from 'react'
import { useTranslation } from 'react-i18next'
import { useRecoilValue } from 'recoil'

import { mountedInBrowserAtom } from '@dao-dao/state/recoil'
import { Web3AuthPrompt } from '@dao-dao/types'
import {
CHAIN_ENDPOINTS,
Expand Down Expand Up @@ -206,32 +208,41 @@ const InnerWalletProvider = ({ children }: PropsWithChildren<{}>) => {
}, [chain.chain_id, walletManager])

// Auto-connect to Keplr mobile web if in that context.
const attemptedKeplrMobileWebConnectionRef = useRef(false)
const mountedInBrowser = useRecoilValue(mountedInBrowserAtom)
const connectingKeplrMobileWebConnectionRef = useRef(false)
useEffect(() => {
if (
typeof window === 'undefined' ||
!mountedInBrowser ||
!isWalletDisconnected ||
attemptedKeplrMobileWebConnectionRef.current
connectingKeplrMobileWebConnectionRef.current
) {
return
}

// Don't try again.
attemptedKeplrMobileWebConnectionRef.current = true
// Only try once at a time.
connectingKeplrMobileWebConnectionRef.current = true

// Connect.
;(async () => {
const keplr = await (
await import('@keplr-wallet/stores')
).getKeplrFromWindow()
const keplrWallet = walletRepo.wallets.find(
(wallet) => wallet.walletInfo.name === 'keplr-extension'
)
if (keplr && keplrWallet && keplr.mode === 'mobile-web') {
keplrWallet.connect()
try {
const keplr = await (
await import('@keplr-wallet/stores')
).getKeplrFromWindow()
const keplrWallet = walletRepo.wallets.find(
(wallet) =>
wallet.walletInfo.name === keplrExtensionWallets[0].walletInfo.name
)
if (keplr && keplrWallet && keplr.mode === 'mobile-web') {
await keplrWallet.connect()
}
} catch (err) {
console.error('Keplr mobile web connection failed', err)
} finally {
connectingKeplrMobileWebConnectionRef.current = false
}
})()
}, [isWalletDisconnected, walletRepo])
}, [isWalletDisconnected, mountedInBrowser, walletRepo])

return <>{children}</>
}
3 changes: 2 additions & 1 deletion packages/stateful/components/wallet/WalletUiConnected.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ export const WalletUiConnected = ({
</div>

{/* In Keplr mobile web, the wallet is force connected and cannot be logged out of, so only show the log out button for all other options. */}
{(walletRepo?.current.walletName !== keplrExtensionWallet.walletName ||
{(walletRepo?.current.walletInfo.name !==
keplrExtensionWallet.walletInfo.name ||
!(walletRepo?.current.client instanceof KeplrClient) ||
walletRepo?.current.client.client.mode !== 'mobile-web') && (
<Button
Expand Down
29 changes: 24 additions & 5 deletions packages/stateful/hooks/useWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ import { useChain as useWalletChain } from '@cosmos-kit/react-lite'
import { useEffect, useMemo, useRef, useState } from 'react'
import { useRecoilValue } from 'recoil'

import { walletChainIdAtom } from '@dao-dao/state/recoil'
import { useChainContextIfAvailable } from '@dao-dao/stateless'
import {
walletChainIdAtom,
walletHexPublicKeySelector,
} from '@dao-dao/state/recoil'
import {
useCachedLoading,
useChainContextIfAvailable,
} from '@dao-dao/stateless'
import { LoadingData } from '@dao-dao/types'
import { getChainForChainId } from '@dao-dao/utils'

Expand Down Expand Up @@ -47,6 +53,16 @@ export const useWallet = ({
const [account, setAccount] = useState<WalletAccount>()
const [hexPublicKeyData, setHexPublicKeyData] = useState<string>()

const hexPublicKeyFromChain = useCachedLoading(
_walletChain.address && loadAccount
? walletHexPublicKeySelector({
walletAddress: _walletChain.address,
chainId: _walletChain.chain.chain_id,
})
: undefined,
undefined
)

useEffect(() => {
if (!loadAccount) {
return
Expand Down Expand Up @@ -84,9 +100,11 @@ export const useWallet = ({
// Use chain from our version of the chain-registry.
chain: getChainForChainId(walletChainRef.current.chain.chain_id),
account,
hexPublicKey: !hexPublicKeyData
? { loading: true }
: { loading: false, data: hexPublicKeyData },
hexPublicKey: hexPublicKeyData
? { loading: false, data: hexPublicKeyData }
: !hexPublicKeyFromChain.loading && hexPublicKeyFromChain.data
? { loading: false, data: hexPublicKeyFromChain.data }
: { loading: true },
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[
Expand All @@ -95,6 +113,7 @@ export const useWallet = ({
walletChainRef.current.address,
walletChainRef.current.chain.chain_id,
walletChainRef.current.status,
hexPublicKeyFromChain,
]
)

Expand Down

2 comments on commit d622929

@vercel
Copy link

@vercel vercel bot commented on d622929 Aug 3, 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 d622929 Aug 3, 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.