fix: prevent double encode if we are on /search endpoint (#3238)

This commit is contained in:
Brandon Cohen
2023-01-13 18:21:54 -05:00
committed by GitHub
parent c2a1a20a3b
commit a343f8ad91
7 changed files with 25 additions and 28 deletions

View File

@@ -8,23 +8,6 @@ import useDebouncedState from './useDebouncedState';
type Url = string | UrlObject;
const extraEncodes: [RegExp, string][] = [
[/\(/g, '%28'],
[/\)/g, '%29'],
[/!/g, '%21'],
[/\*/g, '%2A'],
];
export const encodeURIExtraParams = (string: string): string => {
let finalString = encodeURIComponent(string);
extraEncodes.forEach((encode) => {
finalString = finalString.replace(encode[0], encode[1]);
});
return finalString;
};
interface SearchObject {
searchValue: string;
searchOpen: boolean;
@@ -55,7 +38,7 @@ const useSearchInput = (): SearchObject => {
pathname: router.pathname,
query: {
...router.query,
query: encodeURIExtraParams(debouncedValue),
query: debouncedValue,
},
});
} else {
@@ -63,7 +46,7 @@ const useSearchInput = (): SearchObject => {
router
.push({
pathname: '/search',
query: { query: encodeURIExtraParams(debouncedValue) },
query: { query: debouncedValue },
})
.then(() => window.scrollTo(0, 0));
}
@@ -106,7 +89,7 @@ const useSearchInput = (): SearchObject => {
* is on /search
*/
useEffect(() => {
if (router.query.query !== encodeURIExtraParams(debouncedValue)) {
if (router.query.query !== debouncedValue) {
setSearchValue(
router.query.query
? decodeURIComponent(router.query.query as string)