Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
fix extension connection issue
Browse files Browse the repository at this point in the history
  • Loading branch information
karl-kallavus committed Feb 15, 2023
1 parent 13cf8fe commit 09c198e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
22 changes: 20 additions & 2 deletions islands/WalletConnect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { signal } from "@preact/signals"
import { getWallets } from "@talisman-connect/wallets"
import type { Wallet, WalletAccount } from "@talisman-connect/wallets"
import { DAPP_NAME, SELECTED_ACCOUNT, SELECTED_WALLET } from "misc"
import type { Web3GlobalThis } from "misc"
import { useCallback, useEffect } from "preact/hooks"

const selectedWallet = signal<Wallet | undefined>(undefined)
Expand All @@ -12,7 +13,6 @@ const selectedAccount = signal<WalletAccount | undefined>(undefined)
const setSelectedWallet = async (wallet: Wallet) => {
try {
await wallet.enable(DAPP_NAME)
// TODO implement unsubscribe
await wallet.subscribeAccounts((accounts_) => {
if (accounts_) {
accounts.value = accounts_
Expand All @@ -25,6 +25,24 @@ const setSelectedWallet = async (wallet: Wallet) => {
}
}

const setIntervalToSelectWallet = (wallet: Wallet) => {
let counter = 0
// Sets an interval to listen to `window` until the
// `injectedWeb3` property is present
const interval = setInterval(() => {
counter++
if (counter === 10) {
clearInterval(interval)
} else {
const isExtensionLoaded = (window as Web3GlobalThis)?.injectedWeb3
setSelectedWallet(wallet)
if (isExtensionLoaded) {
clearInterval(interval)
}
}
}, 100)
}

function SelectWallet({ wallets }: { wallets: Wallet[] }) {
const selectWallet = useCallback((wallet: Wallet) => (event: Event) => {
event.preventDefault()
Expand Down Expand Up @@ -141,7 +159,7 @@ export default function WalletConnect() {
selectedAccount.value = selectedAccount_
}
} else {
setSelectedWallet(selectedWallet_)
setIntervalToSelectWallet(selectedWallet_)
}
}
}, [selectedWallet.value])
Expand Down
1 change: 1 addition & 0 deletions misc/mod.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./constants.ts"
export * from "./types.ts"

0 comments on commit 09c198e

Please sign in to comment.