Skip to content

Commit 36efe90

Browse files
committed
Use consistent encoding for text in search params
Previously, text was sometimes encoded as foo%20bar, other times as foo+bar. Let's always use the more readable, plus-based encoding when we are dealing with free form text that might contain spaces.
1 parent e0d21c0 commit 36efe90

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

src/app/pages/SearchResultsPage/useRedirectIfSingleResult.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useEffect } from 'react'
22
import { useNavigate } from 'react-router-dom'
33
import { isConsensusBlock, isConsensusTransaction, SearchResults } from './hooks'
4-
import { RouteUtils } from '../../utils/route-utils'
4+
import { encodeStringForUrl, RouteUtils } from '../../utils/route-utils'
55
import { isItemInScope, SearchScope } from '../../../types/searchScope'
66
import { Network } from '../../../types/network'
77
import { exhaustedTypeWarning } from '../../../types/errors'
@@ -53,7 +53,7 @@ export function useRedirectIfSingleResult(
5353
(!!consensusAccount && item.address.toLowerCase() === consensusAccount.toLowerCase())
5454
) // If we found this account based on address, then we don't want to highlight that.
5555
) {
56-
redirectTo += `?q=${query}`
56+
redirectTo += `?q=${encodeStringForUrl(query)}`
5757
}
5858
break
5959
case 'contract':
@@ -63,13 +63,13 @@ export function useRedirectIfSingleResult(
6363
redirectTo = `${RouteUtils.getTokenRoute(
6464
item,
6565
item.eth_contract_addr || item.contract_addr,
66-
)}?q=${query}`
66+
)}?q=${encodeStringForUrl(query)}`
6767
break
6868
case 'proposal':
69-
redirectTo = `${RouteUtils.getProposalRoute(item.network, item.id)}?q=${query}`
69+
redirectTo = `${RouteUtils.getProposalRoute(item.network, item.id)}?q=${encodeStringForUrl(query)}`
7070
break
7171
case 'roflApp':
72-
redirectTo = `${RouteUtils.getRoflAppRoute(item.network, item.id)}?q=${query}`
72+
redirectTo = `${RouteUtils.getRoflAppRoute(item.network, item.id)}?q=${encodeStringForUrl(query)}`
7373
break
7474
default:
7575
exhaustedTypeWarning('Unexpected result type', item)

src/app/utils/route-utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ export const isScopeHidden = (scope: SearchScope): boolean =>
8080

8181
export const isNotInHiddenScope = (item: HasScope) => !isScopeHidden(item)
8282

83+
export const encodeStringForUrl = (text: string) => encodeURIComponent(text).replace(/%20/g, '+')
84+
8385
const formatPreservedParams = (searchParams: URLSearchParams | undefined, paramsToKeep: string[]): string => {
8486
if (!searchParams) return ''
8587
const toDelete: string[] = []
@@ -200,7 +202,7 @@ export abstract class RouteUtils {
200202
static getSearchRoute = (scope: SearchScope | undefined, searchTerm: string) => {
201203
return scope
202204
? `${this.getScopeRoute(scope)}/search?q=${encodeURIComponent(searchTerm)}`
203-
: `/search?q=${encodeURIComponent(searchTerm)}`
205+
: `/search?q=${encodeStringForUrl(searchTerm)}`
204206
}
205207

206208
static getTokenRoute = (scope: SearchScope, tokenAddress: string) => {

0 commit comments

Comments
 (0)