Skip to content

Commit

Permalink
Fix auto reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Lbqds committed Nov 23, 2024
1 parent 172d819 commit 56c481c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 33 deletions.
80 changes: 50 additions & 30 deletions packages/web3-react/src/components/AlephiumConnect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
subscribeToTxStatus,
web3
} from '@alephium/web3'
import { Theme, Mode, CustomTheme, connectorIds, ProviderTheme } from '../types'
import { Theme, Mode, CustomTheme, ProviderTheme } from '../types'
import { routes } from './Common/Modal'
import {
AlephiumBalanceContext,
Expand All @@ -48,6 +48,8 @@ import {
import { getLastConnectedAccount, removeLastConnectedAccount } from '../utils/storage'
import { ConnectResult, getConnectorById } from '../utils/connector'
import { useInjectedProviders } from '../hooks/useInjectedProviders'
import { injectedProviderStore } from '../utils/providers'
import { AlephiumWindowObject } from '@alephium/get-extension-wallet'

export const ConnectSettingProvider: React.FC<{
theme?: Theme
Expand Down Expand Up @@ -176,39 +178,57 @@ export const AlephiumConnectProvider: React.FC<{
)

useEffect(() => {
const func = async () => {
const onDisconnected = () => {
removeLastConnectedAccount()
updateAccount(undefined)
updateSignerProvider(undefined)
}
const onConnected = (result: ConnectResult) => {
updateAccount(result.account)
updateSignerProvider(result.signerProvider)
const onDisconnected = () => {
removeLastConnectedAccount()
updateAccount(undefined)
updateSignerProvider(undefined)
}
const onConnected = (result: ConnectResult) => {
updateAccount(result.account)
updateSignerProvider(result.signerProvider)
}

const reconnectByInjected = async (providers: AlephiumWindowObject[], times = 0) => {
if (times >= 20) {
onDisconnected()
return
}
const result = await getConnectorById('injected').autoConnect?.({
network,
addressGroup,
keyType,
onDisconnected,
onConnected,
allInjectedProviders: providers
})
if (result !== undefined) return
setTimeout(() => reconnectByInjected(injectedProviderStore.getProviders(), times + 1), 100)
}

const func = async () => {
try {
const lastConnectorId = lastConnectedAccount?.connectorId
const allConnectorIds = Array.from(connectorIds)
const sortedConnectorIds =
lastConnectorId === undefined
? allConnectorIds
: [lastConnectorId].concat(allConnectorIds.filter((c) => c !== lastConnectorId))

for (const connectorId of sortedConnectorIds) {
const connector = getConnectorById(connectorId)
if (connector.autoConnect !== undefined) {
const result = await connector.autoConnect({
network,
addressGroup,
keyType,
onDisconnected,
onConnected,
allInjectedProviders: connectorId === 'injected' ? allInjectedProviders : undefined
})
if (result !== undefined) {
return
}
if (lastConnectorId === undefined) {
onDisconnected()
return
}

if (lastConnectorId === 'injected') {
reconnectByInjected(allInjectedProviders)
return
}

const connector = getConnectorById(lastConnectorId)
if (connector.autoConnect !== undefined) {
const result = await connector.autoConnect({
network,
addressGroup,
keyType,
onDisconnected,
onConnected
})
if (result !== undefined) {
return
}
}
} catch (error) {
Expand Down
6 changes: 3 additions & 3 deletions packages/web3-react/src/utils/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
import { Account, NetworkId, SignerProvider, KeyType } from '@alephium/web3'
import { WalletConnectProvider } from '@alephium/walletconnect-provider'
import QRCodeModal from '@alephium/walletconnect-qrcode-modal'
import { AlephiumWindowObject, getDefaultAlephiumWallet } from '@alephium/get-extension-wallet'
import { alephiumProvider, AlephiumWindowObject, getWalletObject } from '@alephium/get-extension-wallet'
import { setLastConnectedAccount } from './storage'
import { ConnectorId } from '../types'

Expand Down Expand Up @@ -130,7 +130,7 @@ const wcDisconnect = async (signerProvider: SignerProvider): Promise<void> => {

const injectedConnect = async (options: InjectedConnectOptions): Promise<Account | undefined> => {
try {
const windowAlephium = options.injectedProvider ?? (await getDefaultAlephiumWallet())
const windowAlephium = options.injectedProvider ?? getWalletObject(alephiumProvider.id)
const enableOptions = {
addressGroup: options.addressGroup,
keyType: options.keyType,
Expand Down Expand Up @@ -159,7 +159,7 @@ const injectedAutoConnect = async (options: InjectedAutoConnectOptions): Promise
try {
const allProviders = options.allInjectedProviders ?? []
if (allProviders.length === 0) {
const windowAlephium = await getDefaultAlephiumWallet()
const windowAlephium = getWalletObject(alephiumProvider.id)
if (windowAlephium !== undefined) {
allProviders.push(windowAlephium)
}
Expand Down

0 comments on commit 56c481c

Please sign in to comment.