Skip to content

Commit

Permalink
Merge pull request #2731 from CityOfZion/CU-86dthjvx1-2
Browse files Browse the repository at this point in the history
CU-86dthjvx1 - NEON2 - Investigate the problem on NWD's WC deeplink w…
  • Loading branch information
melanke authored May 28, 2024
2 parents bf9f69f + 99b5162 commit e8ddb4d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
56 changes: 37 additions & 19 deletions app/containers/App/App.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
import React, { useEffect, useState } from 'react'
import React, { useCallback, useEffect, useState } from 'react'

import { useWalletConnectWallet } from '@cityofzion/wallet-connect-sdk-wallet-react'
import { ROUTES } from '../../core/constants'
Expand All @@ -12,7 +12,6 @@ import styles from './App.scss'
import themes from '../../themes'
import ErrorBoundary from '../../components/ErrorBoundaries/Main'
import FramelessNavigation from '../../components/FramelessNavigation'
import { parseQuery } from '../../core/formatters'
import withSettingsContext from '../../hocs/withSettingsContext'
import { getInformationFromSession } from '../../util/walletConnect'

Expand Down Expand Up @@ -44,6 +43,10 @@ const routesWithSideBar = [
ROUTES.NFT,
]

function isWalletConnectUri(uri) {
return /^wc:.+@\d.*$/g.test(uri)
}

const App = ({
children,
address,
Expand All @@ -58,6 +61,24 @@ const App = ({
const { requests, sessions, disconnect } = useWalletConnectWallet()
const [decodedDeeplinkUri, setDecodedDeeplinkUri] = useState(null)

const handleDeeplink = useCallback(async (uri: string) => {
await ipc.invoke('restore')

const realUri = uri.split('uri=').pop()
if (!realUri) return

const decodedUri = decodeURIComponent(realUri)
if (isWalletConnectUri(decodedUri)) {
setDecodedDeeplinkUri(decodedUri)
return
}

const decodedBase64Uri = atob(decodedUri)
if (isWalletConnectUri(decodedBase64Uri)) {
setDecodedDeeplinkUri(decodedBase64Uri)
}
}, [])

useEffect(() => {
async function handleUpgrade() {
checkVersion()
Expand All @@ -72,22 +93,22 @@ const App = ({
handleUpgrade()
}, [])

useEffect(() => {
const listener = async (_event, uri) => {
await ipc.invoke('restore')
setDecodedDeeplinkUri(uri)
}
useEffect(
() => {
ipc.invoke('getInitialDeepLinkUri').then(handleDeeplink)

ipc.on('link', listener)
const listener = async (_event, uri: string) => {
handleDeeplink(uri)
}

return () => {
ipc.off('link', listener)
}
}, [])
ipc.on('link', listener)

useEffect(() => {
ipc.invoke('getInitialDeepLinkUri').then(setDecodedDeeplinkUri)
}, [])
return () => {
ipc.off('link', listener)
}
},
[handleDeeplink],
)

useEffect(
() => {
Expand All @@ -100,12 +121,9 @@ const App = ({
return
}

const { uri } = parseQuery(decodeURI(decodedDeeplinkUri))
if (!uri) return

history.push({
pathname: ROUTES.CONNECT_DAPP,
state: { uri },
state: { uri: decodedDeeplinkUri },
})
setDecodedDeeplinkUri(null)
},
Expand Down
4 changes: 1 addition & 3 deletions app/containers/ConnectDapp/ConnectDapp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ const ConnectDapp = ({ showErrorNotification, history }: Props) => {
useEffect(
() => {
if (!uri) return

const decoded = atob(uri)
handleOnURI(decoded)
handleOnURI(uri)
},
[uri, handleOnURI],
)
Expand Down

0 comments on commit e8ddb4d

Please sign in to comment.