Skip to content

Commit

Permalink
Fix deep linking within in-app browsers (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink authored Sep 30, 2024
1 parent 8c368f4 commit b1b1ea2
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions components/views/ScanConnect/ScanConnectMobile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ export default function ScanConnectMobile({
onGetWallet,
noDeepLink,
}: ScanConnectMobileProps) {
const windowRef = useRef<Window | null>(null)
const { setLastUsed } = useWalletHistory()
const { uri, connecting, error, isLoading } = useWcUri(() => {
if (windowRef.current && !windowRef.current.closed) {
windowRef.current.close()
}
setLastUsed(wallet)
handleCancel()
})
Expand All @@ -36,12 +32,27 @@ export default function ScanConnectMobile({
)

function openDeepLink() {
if (windowRef.current && !windowRef.current.closed) {
windowRef.current.close()
}
const deeplink = new URL(service.uid)
deeplink.searchParams.set('uri', uri)
windowRef.current = window.open(deeplink, '_blank')

if (deeplink.toString().startsWith('http')) {
// Workaround for https://github.com/rainbow-me/rainbowkit/issues/524.
// Using 'window.open' causes issues on iOS in non-Safari browsers and
// WebViews where a blank tab is left behind after connecting.
// This is especially bad in some WebView scenarios (e.g. following a
// link from Twitter) where the user doesn't have any mechanism for
// closing the blank tab.
// For whatever reason, links with a target of "_blank" don't suffer
// from this problem, and programmatically clicking a detached link
// element with the same attributes also avoids the issue.
const link = document.createElement('a')
link.href = deeplink.toString()
link.target = '_blank'
link.rel = 'noreferrer noopener'
link.click()
} else {
window.location.href = deeplink.toString()
}
}

useEffect(() => {
Expand Down

0 comments on commit b1b1ea2

Please sign in to comment.