fix: fixed account change and refresh issues (#498)
This commit is contained in:
parent
8bb308c3e0
commit
308ec7dba4
@ -1,7 +1,7 @@
|
|||||||
import classNames from 'classnames'
|
import classNames from 'classnames'
|
||||||
import useAccountId from 'hooks/useAccountId'
|
|
||||||
import { NavLink, useParams } from 'react-router-dom'
|
import { NavLink, useParams } from 'react-router-dom'
|
||||||
|
|
||||||
|
import useAccountId from 'hooks/useAccountId'
|
||||||
import { getRoute } from 'utils/route'
|
import { getRoute } from 'utils/route'
|
||||||
|
|
||||||
const underlineClasses =
|
const underlineClasses =
|
||||||
|
@ -26,7 +26,7 @@ import { BNCoin } from 'types/classes/BNCoin'
|
|||||||
import { byDenom } from 'utils/array'
|
import { byDenom } from 'utils/array'
|
||||||
import { defaultFee } from 'utils/constants'
|
import { defaultFee } from 'utils/constants'
|
||||||
import { getCapLeftWithBuffer } from 'utils/generic'
|
import { getCapLeftWithBuffer } from 'utils/generic'
|
||||||
import { BN, asyncThrottle } from 'utils/helpers'
|
import { asyncThrottle, BN } from 'utils/helpers'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
buyAsset: Asset
|
buyAsset: Asset
|
||||||
|
@ -65,7 +65,8 @@ export default function WalletConnectedButton() {
|
|||||||
if (!currentWallet) return
|
if (!currentWallet) return
|
||||||
disconnectWallet(currentWallet)
|
disconnectWallet(currentWallet)
|
||||||
useStore.setState({ client: undefined, address: undefined, accounts: null, balances: [] })
|
useStore.setState({ client: undefined, address: undefined, accounts: null, balances: [] })
|
||||||
if (focusComponent)
|
|
||||||
|
if (focusComponent) {
|
||||||
useStore.setState({
|
useStore.setState({
|
||||||
focusComponent: {
|
focusComponent: {
|
||||||
component: <WalletSelect />,
|
component: <WalletSelect />,
|
||||||
@ -74,6 +75,8 @@ export default function WalletConnectedButton() {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
navigate(getRoute(getPage(pathname)))
|
navigate(getRoute(getPage(pathname)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate'
|
import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate'
|
||||||
import { useShuttle } from '@delphi-labs/shuttle-react'
|
import { useShuttle } from '@delphi-labs/shuttle-react'
|
||||||
import { useCallback, useEffect } from 'react'
|
import { useCallback, useEffect, useMemo } from 'react'
|
||||||
|
|
||||||
import { CircularProgress } from 'components/CircularProgress'
|
import { CircularProgress } from 'components/CircularProgress'
|
||||||
import FullOverlayContent from 'components/FullOverlayContent'
|
import FullOverlayContent from 'components/FullOverlayContent'
|
||||||
@ -30,11 +30,17 @@ const mapErrorMessages = (providerId: string, errorMessage: string) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function WalletConnecting(props: Props) {
|
export default function WalletConnecting(props: Props) {
|
||||||
const { recentWallet, connect, simulate, sign, broadcast } = useShuttle()
|
const { recentWallet, connect, simulate, sign, broadcast, mobileProviders, extensionProviders } =
|
||||||
|
useShuttle()
|
||||||
|
const providers = useMemo(
|
||||||
|
() => [...mobileProviders, ...extensionProviders],
|
||||||
|
[mobileProviders, extensionProviders],
|
||||||
|
)
|
||||||
const [isConnecting, setIsConnecting] = useToggle()
|
const [isConnecting, setIsConnecting] = useToggle()
|
||||||
const providerId = props.providerId ?? recentWallet?.providerId
|
const providerId = props.providerId ?? recentWallet?.providerId
|
||||||
const isAutoConnect = props.autoConnect
|
const isAutoConnect = props.autoConnect
|
||||||
const client = useStore((s) => s.client)
|
const client = useStore((s) => s.client)
|
||||||
|
const address = useStore((s) => s.address)
|
||||||
|
|
||||||
const handleConnect = useCallback(
|
const handleConnect = useCallback(
|
||||||
(extensionProviderId: string) => {
|
(extensionProviderId: string) => {
|
||||||
@ -64,9 +70,7 @@ export default function WalletConnecting(props: Props) {
|
|||||||
})
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setIsConnecting(false)
|
setIsConnecting(false)
|
||||||
|
|
||||||
if (isAutoConnect) return
|
if (isAutoConnect) return
|
||||||
|
|
||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
useStore.setState({
|
useStore.setState({
|
||||||
client: undefined,
|
client: undefined,
|
||||||
@ -86,21 +90,22 @@ export default function WalletConnecting(props: Props) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setTimeout(
|
if (!isConnecting) handleConnectAsync()
|
||||||
() => {
|
|
||||||
if (isConnecting) return
|
|
||||||
handleConnectAsync()
|
|
||||||
},
|
|
||||||
isAutoConnect ? 1000 : 0,
|
|
||||||
)
|
|
||||||
},
|
},
|
||||||
[broadcast, connect, client, isAutoConnect, isConnecting, setIsConnecting, sign, simulate],
|
[broadcast, connect, client, isAutoConnect, isConnecting, setIsConnecting, sign, simulate],
|
||||||
)
|
)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isConnecting || !providerId) return
|
const provider = providers.find((p) => p.id === providerId)
|
||||||
handleConnect(providerId)
|
if (
|
||||||
}, [isConnecting, providerId, handleConnect])
|
!provider ||
|
||||||
|
!provider.initialized ||
|
||||||
|
isConnecting ||
|
||||||
|
(recentWallet && recentWallet.account.address === address)
|
||||||
|
)
|
||||||
|
return
|
||||||
|
handleConnect(provider.id)
|
||||||
|
}, [handleConnect, isConnecting, providerId, providers, recentWallet, address])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<FullOverlayContent
|
<FullOverlayContent
|
||||||
|
@ -1,40 +1,27 @@
|
|||||||
import { useEffect } from 'react'
|
import { useEffect } from 'react'
|
||||||
import { useLocation, useNavigate, useParams } from 'react-router-dom'
|
|
||||||
|
|
||||||
import WalletConnectButton from 'components/Wallet/WalletConnectButton'
|
import WalletConnectButton from 'components/Wallet/WalletConnectButton'
|
||||||
import WalletConnectedButton from 'components/Wallet/WalletConnectedButton'
|
import WalletConnectedButton from 'components/Wallet/WalletConnectedButton'
|
||||||
import WalletConnecting from 'components/Wallet/WalletConnecting'
|
import WalletConnecting from 'components/Wallet/WalletConnecting'
|
||||||
import useCurrentWallet from 'hooks/useCurrentWallet'
|
import useCurrentWallet from 'hooks/useCurrentWallet'
|
||||||
import useStore from 'store'
|
import useStore from 'store'
|
||||||
import { getPage, getRoute } from 'utils/route'
|
|
||||||
|
|
||||||
export default function Wallet() {
|
export default function Wallet() {
|
||||||
const navigate = useNavigate()
|
|
||||||
const { address: addressInUrl } = useParams()
|
|
||||||
const { pathname } = useLocation()
|
|
||||||
const currentWallet = useCurrentWallet()
|
const currentWallet = useCurrentWallet()
|
||||||
const address = useStore((s) => s.address)
|
const address = useStore((s) => s.address)
|
||||||
const client = useStore((s) => s.client)
|
const focusComponent = useStore((s) => s.focusComponent)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!currentWallet) {
|
if (!currentWallet) return
|
||||||
useStore.setState({ address: undefined, accounts: null, client: undefined })
|
if (currentWallet.account.address === address || focusComponent) return
|
||||||
return
|
useStore.setState({
|
||||||
}
|
address: undefined,
|
||||||
|
client: undefined,
|
||||||
if (client) {
|
focusComponent: {
|
||||||
if (currentWallet.account.address !== address)
|
component: <WalletConnecting autoConnect />,
|
||||||
useStore.setState({ address: currentWallet.account.address })
|
},
|
||||||
return
|
})
|
||||||
}
|
}, [currentWallet, address, focusComponent])
|
||||||
useStore.setState({ focusComponent: { component: <WalletConnecting autoConnect /> } })
|
|
||||||
}, [currentWallet, client, address])
|
|
||||||
|
|
||||||
// Redirect when switching wallets or on first connection
|
|
||||||
useEffect(() => {
|
|
||||||
if (!address || address === addressInUrl) return
|
|
||||||
navigate(getRoute(getPage(pathname), address))
|
|
||||||
}, [address, addressInUrl, navigate, pathname])
|
|
||||||
|
|
||||||
return address ? <WalletConnectedButton /> : <WalletConnectButton />
|
return address ? <WalletConnectedButton /> : <WalletConnectButton />
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ import getAccountIds from 'api/wallets/getAccountIds'
|
|||||||
export default function useAccountIds(address?: string) {
|
export default function useAccountIds(address?: string) {
|
||||||
return useSWR(`wallets/${address}/account-ids`, () => getAccountIds(address), {
|
return useSWR(`wallets/${address}/account-ids`, () => getAccountIds(address), {
|
||||||
suspense: true,
|
suspense: true,
|
||||||
fallback: [],
|
fallback: [] as string[],
|
||||||
revalidateOnFocus: false,
|
revalidateOnFocus: false,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,8 @@ export function getRoute(page: Page, address?: string, accountId?: string | null
|
|||||||
|
|
||||||
if (accountId) {
|
if (accountId) {
|
||||||
url.searchParams.append('accountId', accountId)
|
url.searchParams.append('accountId', accountId)
|
||||||
|
} else {
|
||||||
|
url.searchParams.delete('accountId')
|
||||||
}
|
}
|
||||||
|
|
||||||
return url.pathname + url.search
|
return url.pathname + url.search
|
||||||
|
Loading…
Reference in New Issue
Block a user