Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deep linking within in-app browsers #266

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 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,25 @@ 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()
}
}

useEffect(() => {
Expand Down
Loading