Skip to content

Commit

Permalink
feat(core,modal): add isWindowEthereumAvailable
Browse files Browse the repository at this point in the history
  • Loading branch information
johnson86tw committed Apr 30, 2024
1 parent 6e88e87 commit 62ed29c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const hideConnectingModal = computed(() => {

<NuxtPage />

<VueDappModal autoConnect :hideConnectingModal="hideConnectingModal"> </VueDappModal>
<VueDappModal :hideConnectingModal="hideConnectingModal"> </VueDappModal>
</NuxtLayout>
</n-config-provider>
</template>
2 changes: 1 addition & 1 deletion app/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const items = computed<Item[]>(() => [
<p v-if="wallet.status === 'connected'">Disconnect</p>
</n-button>

<p class="text-red-500" v-if="wallet.error">{{ wallet.error }}</p>
<p class="text-red-500 text-center" v-if="wallet.error">{{ wallet.error }}</p>

<ClientOnly>
<Vue3EasyDataTable
Expand Down
13 changes: 8 additions & 5 deletions packages/core/src/services/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,19 @@ export function useConnect(pinia?: any) {
}
}

const isWindowEthereumAvailable = typeof window !== 'undefined' && !!window.ethereum

async function autoConnect(rdns?: RDNS | string, isWindowEthereum = false) {
const connectorName = 'BrowserWallet'
const bwConnector = walletStore.connectors.find(conn => conn.name === connectorName)
if (!bwConnector) return
const browserWallet = walletStore.connectors.find(conn => conn.name === 'BrowserWallet')
if (!browserWallet) return

let options = {}

if (isWindowEthereum) {
if (!isWindowEthereumAvailable) return
options = { isWindowEthereum }
} else {
const lastRdns = getLastConnectedBrowserWallet()
if (!lastRdns) return

rdns = rdns || lastRdns
if (!rdns) return
Expand All @@ -119,13 +120,15 @@ export function useConnect(pinia?: any) {
}

try {
await connectTo(bwConnector.name, options)
await connectTo(browserWallet.name, options)
} catch (err: any) {
throw new AutoConnectError(err)
}
}

return {
isWindowEthereumAvailable,

wallet: readonly(walletStore.wallet),

status: computed(() => walletStore.wallet.status),
Expand Down
53 changes: 32 additions & 21 deletions packages/modal/src/VueDappModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,18 @@ const modalOpen = computed(() => props.modelValue ?? store.isModalOpen)
const isAutoConnecting = ref(false)
const { connectors, connectTo, autoConnect, status, providerDetails, hasConnector, disconnect } = useVueDapp()
const {
isWindowEthereumAvailable,
connectors,
connectTo,
autoConnect,
status,
providerDetails,
hasConnector,
disconnect,
} = useVueDapp()
// ============================ feat: autoConnect ============================
onMounted(async () => {
if (props.autoConnect) {
try {
Expand All @@ -59,27 +67,13 @@ onMounted(async () => {
}
})
// Check whether the browser is within a mobile app (such as a WebView) rather than a standalone mobile browser like Chrome App
function isMobileAppBrowser() {
const userAgent = navigator.userAgent
// for ios
if (!userAgent.includes('Safari/') && userAgent.includes('Mobile/')) {
return true
}
// for android
if (userAgent.includes('wv') || userAgent.includes('WebView')) {
return true
}
return false
}
watch(modalOpen, async () => {
// ============================ feat: connect to window.ethereum if there's no EIP-6963 providers ============================
// ============================ feat: connect to window.ethereum if window.ethereum is available and there's no EIP-6963 providers ============================
if (modalOpen.value && providerDetails.value.length === 0 && isMobileAppBrowser()) {
await onClickWallet('BrowserWallet', undefined, true)
if (isWindowEthereumAvailable) {
await onClickWallet('BrowserWallet', undefined, true)
}
return
}
// ============================ feat: auto click BrowserWallet if it's the only connector ============================
Expand Down Expand Up @@ -108,6 +102,23 @@ function onClickCancelConnecting() {
disconnect()
}
// Check whether the browser is within a mobile app (such as a WebView) rather than a standalone mobile browser like Chrome App
function isMobileAppBrowser() {
const userAgent = navigator.userAgent
// for ios
if (!userAgent.includes('Safari/') && userAgent.includes('Mobile/')) {
return true
}
// for android
if (userAgent.includes('wv') || userAgent.includes('WebView')) {
return true
}
return false
}
const vClickOutside = {
beforeMount: (el: any, binding: any) => {
el.clickOutsideEvent = (event: MouseEvent) => {
Expand Down

0 comments on commit 62ed29c

Please sign in to comment.