Skip to content

Commit

Permalink
Rm raw query parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh committed Nov 8, 2023
1 parent ef608a5 commit 1873504
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 42 deletions.
9 changes: 4 additions & 5 deletions src/pages/wc.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useEffect } from 'react'
import type { NextPage } from 'next'
import { useRouter } from 'next/router'
import { parse } from 'querystring'
import useLastSafe from '@/hooks/useLastSafe'
import { AppRoutes } from '@/config/routes'
import { WC_URI_SEARCH_PARAM } from '@/services/walletconnect/useWalletConnectSearchParamUri'

const WcPage: NextPage = () => {
const router = useRouter()
Expand All @@ -14,22 +14,21 @@ const WcPage: NextPage = () => {
return
}

// Don't use router.query because it cuts off internal paramters of the WC URI (e.g. symKey)
const { uri } = parse(window.location.search.slice(1))
const { uri } = router.query

router.replace(
lastSafe
? {
pathname: AppRoutes.home,
query: {
safe: lastSafe,
wc: uri,
[WC_URI_SEARCH_PARAM]: uri,
},
}
: {
pathname: AppRoutes.welcome,
query: {
wc: uri,
[WC_URI_SEARCH_PARAM]: uri,
},
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ describe('useWalletConnectSearchParamUri', () => {
it('should return the wc uri search param value when present', () => {
mockRouter.query = { wc: 'wc:123' }

Object.defineProperty(window, 'location', {
writable: true,
value: {
pathname: window.location.pathname,
search: '?wc=wc:123',
},
})

const { result } = renderHook(() => useWalletConnectSearchParamUri())
const [wcUri] = result.current

Expand All @@ -49,14 +41,6 @@ describe('useWalletConnectSearchParamUri', () => {
mockRouter.pathname = '/test'
mockRouter.query = { test: 'example', wc: 'wc:123' }

Object.defineProperty(window, 'location', {
writable: true,
value: {
pathname: window.location.pathname,
search: '?wc=wc:123&test=example',
},
})

const { result } = renderHook(() => useWalletConnectSearchParamUri())
const [wcUri, setWcUri] = result.current

Expand All @@ -77,14 +61,6 @@ describe('useWalletConnectSearchParamUri', () => {
mockRouter.pathname = '/test'
mockRouter.query = { test: 'example', wc: 'wc:123' }

Object.defineProperty(window, 'location', {
writable: true,
value: {
pathname: window.location.pathname,
search: '?wc=wc:123&test=example',
},
})

const { result } = renderHook(() => useWalletConnectSearchParamUri())
const [wcUri, setWcUri] = result.current

Expand Down
17 changes: 4 additions & 13 deletions src/services/walletconnect/useWalletConnectSearchParamUri.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import { useRouter } from 'next/router'
import { parse } from 'querystring'
import { useCallback, useEffect, useState } from 'react'
import { useCallback } from 'react'

const WC_URI_SEARCH_PARAM = 'wc'
export const WC_URI_SEARCH_PARAM = 'wc'

export function useWalletConnectSearchParamUri(): [string | null, (wcUri: string | null) => void] {
const router = useRouter()
const wcQuery = router.query[WC_URI_SEARCH_PARAM]
const [rawUrlParam, setRawUrlParam] = useState('')

useEffect(() => {
// Don't use router.query because it cuts off internal paramters of the WC URI (e.g. symKey)
const query = parse(window.location.search.slice(1))
const wcUri = query[WC_URI_SEARCH_PARAM] || ''
setRawUrlParam(wcUri.toString())
}, [wcQuery])
const wcUri = (router.query[WC_URI_SEARCH_PARAM] || '').toString() || null

const setWcUri = useCallback(
(wcUri: string | null) => {
Expand All @@ -34,5 +25,5 @@ export function useWalletConnectSearchParamUri(): [string | null, (wcUri: string
[router],
)

return [rawUrlParam || null, setWcUri]
return [wcUri, setWcUri]
}

0 comments on commit 1873504

Please sign in to comment.