generated from Tahul/vue-composable-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #182 from vu3th/feat/useAutoConnect
feat: useAutoConnect
- Loading branch information
Showing
15 changed files
with
212 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { defineStore } from 'pinia' | ||
|
||
export const useAppStore = defineStore('appStore', () => { | ||
const darkMode = ref(false) | ||
|
||
return { | ||
darkMode, | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { onMounted, ref } from 'vue' | ||
import { useConnect } from './services/connect' | ||
import { ConnectOptions, ProviderTarget } from './types' | ||
import { useConnectors } from './services/connectors' | ||
import { getLastConnectedBrowserWallet } from './services/localStorage' | ||
import { isWindowEthereumAvailable } from './utils' | ||
|
||
export function useAutoConnect(pinia?: any) { | ||
const isAutoConnecting = ref(false) | ||
const error = ref<Error | null>(null) | ||
|
||
const { connectors } = useConnectors(pinia) | ||
const { connectTo } = useConnect(pinia) | ||
|
||
onMounted(async () => { | ||
try { | ||
isAutoConnecting.value = true | ||
if (isMobileAppBrowser()) { | ||
await autoConnect('window.ethereum') | ||
} else { | ||
await autoConnect('rdns') | ||
} | ||
} catch (err: any) { | ||
error.value = err | ||
} finally { | ||
isAutoConnecting.value = false | ||
} | ||
}) | ||
|
||
async function autoConnect(target: ProviderTarget) { | ||
const browserWallet = connectors.value.find(conn => conn.name === 'BrowserWallet') | ||
if (!browserWallet) return | ||
|
||
let options: ConnectOptions<'BrowserWallet'> | ||
|
||
switch (target) { | ||
case 'window.ethereum': | ||
if (!isWindowEthereumAvailable) return | ||
options = { | ||
target: 'window.ethereum', | ||
} | ||
break | ||
case 'rdns': | ||
const lastRdns = getLastConnectedBrowserWallet() | ||
if (!lastRdns) return | ||
|
||
options = { target: 'rdns', rdns: lastRdns } | ||
break | ||
default: | ||
const err = new Error('target is required') | ||
error.value = err | ||
throw err | ||
} | ||
|
||
try { | ||
await connectTo('BrowserWallet', options) | ||
} catch (err: any) { | ||
error.value = err | ||
throw err | ||
} | ||
} | ||
|
||
return { isAutoConnecting, error } | ||
} | ||
|
||
/** | ||
* Check whether the browser is within a mobile app (such as a WebView) rather than a standalone mobile browser like Chrome App | ||
* @returns boolean | ||
*/ | ||
export 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './format' | ||
export * from './check' | ||
export * from './assert' | ||
|
||
export const isWindowEthereumAvailable = typeof window !== 'undefined' && !!window.ethereum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.