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

pull/3086/head
Brandon Cohen 2 years ago committed by GitHub
parent c2a1a20a3b
commit a343f8ad91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,7 +2,7 @@ import Button from '@app/components/Common/Button';
import Tooltip from '@app/components/Common/Tooltip'; import Tooltip from '@app/components/Common/Tooltip';
import { sliderTitles } from '@app/components/Discover/constants'; import { sliderTitles } from '@app/components/Discover/constants';
import MediaSlider from '@app/components/MediaSlider'; import MediaSlider from '@app/components/MediaSlider';
import { encodeURIExtraParams } from '@app/hooks/useSearchInput'; import { encodeURIExtraParams } from '@app/hooks/useDiscover';
import type { import type {
TmdbCompanySearchResponse, TmdbCompanySearchResponse,
TmdbGenre, TmdbGenre,

@ -1,8 +1,7 @@
import Header from '@app/components/Common/Header'; import Header from '@app/components/Common/Header';
import ListView from '@app/components/Common/ListView'; import ListView from '@app/components/Common/ListView';
import PageTitle from '@app/components/Common/PageTitle'; import PageTitle from '@app/components/Common/PageTitle';
import useDiscover from '@app/hooks/useDiscover'; import useDiscover, { encodeURIExtraParams } from '@app/hooks/useDiscover';
import { encodeURIExtraParams } from '@app/hooks/useSearchInput';
import globalMessages from '@app/i18n/globalMessages'; import globalMessages from '@app/i18n/globalMessages';
import Error from '@app/pages/_error'; import Error from '@app/pages/_error';
import type { TmdbKeyword } from '@server/api/themoviedb/interfaces'; import type { TmdbKeyword } from '@server/api/themoviedb/interfaces';

@ -1,8 +1,7 @@
import Header from '@app/components/Common/Header'; import Header from '@app/components/Common/Header';
import ListView from '@app/components/Common/ListView'; import ListView from '@app/components/Common/ListView';
import PageTitle from '@app/components/Common/PageTitle'; import PageTitle from '@app/components/Common/PageTitle';
import useDiscover from '@app/hooks/useDiscover'; import useDiscover, { encodeURIExtraParams } from '@app/hooks/useDiscover';
import { encodeURIExtraParams } from '@app/hooks/useSearchInput';
import globalMessages from '@app/i18n/globalMessages'; import globalMessages from '@app/i18n/globalMessages';
import Error from '@app/pages/_error'; import Error from '@app/pages/_error';
import type { TmdbKeyword } from '@server/api/themoviedb/interfaces'; import type { TmdbKeyword } from '@server/api/themoviedb/interfaces';

@ -14,7 +14,7 @@ import RecentRequestsSlider from '@app/components/Discover/RecentRequestsSlider'
import StudioSlider from '@app/components/Discover/StudioSlider'; import StudioSlider from '@app/components/Discover/StudioSlider';
import TvGenreSlider from '@app/components/Discover/TvGenreSlider'; import TvGenreSlider from '@app/components/Discover/TvGenreSlider';
import MediaSlider from '@app/components/MediaSlider'; import MediaSlider from '@app/components/MediaSlider';
import { encodeURIExtraParams } from '@app/hooks/useSearchInput'; import { encodeURIExtraParams } from '@app/hooks/useDiscover';
import { Permission, useUser } from '@app/hooks/useUser'; import { Permission, useUser } from '@app/hooks/useUser';
import globalMessages from '@app/i18n/globalMessages'; import globalMessages from '@app/i18n/globalMessages';
import { Transition } from '@headlessui/react'; import { Transition } from '@headlessui/react';

@ -1,4 +1,4 @@
import { encodeURIExtraParams } from '@app/hooks/useSearchInput'; import { encodeURIExtraParams } from '@app/hooks/useDiscover';
import type { import type {
TmdbCompanySearchResponse, TmdbCompanySearchResponse,
TmdbGenre, TmdbGenre,

@ -1,4 +1,3 @@
import { encodeURIExtraParams } from '@app/hooks/useSearchInput';
import { MediaStatus } from '@server/constants/media'; import { MediaStatus } from '@server/constants/media';
import useSWRInfinite from 'swr/infinite'; import useSWRInfinite from 'swr/infinite';
import useSettings from './useSettings'; import useSettings from './useSettings';
@ -28,6 +27,23 @@ interface DiscoverResult<T, S> {
firstResultData?: BaseSearchResult<T> & S; firstResultData?: BaseSearchResult<T> & S;
} }
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;
};
const useDiscover = < const useDiscover = <
T extends BaseMedia, T extends BaseMedia,
S = Record<string, never>, S = Record<string, never>,

@ -8,23 +8,6 @@ import useDebouncedState from './useDebouncedState';
type Url = string | UrlObject; 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 { interface SearchObject {
searchValue: string; searchValue: string;
searchOpen: boolean; searchOpen: boolean;
@ -55,7 +38,7 @@ const useSearchInput = (): SearchObject => {
pathname: router.pathname, pathname: router.pathname,
query: { query: {
...router.query, ...router.query,
query: encodeURIExtraParams(debouncedValue), query: debouncedValue,
}, },
}); });
} else { } else {
@ -63,7 +46,7 @@ const useSearchInput = (): SearchObject => {
router router
.push({ .push({
pathname: '/search', pathname: '/search',
query: { query: encodeURIExtraParams(debouncedValue) }, query: { query: debouncedValue },
}) })
.then(() => window.scrollTo(0, 0)); .then(() => window.scrollTo(0, 0));
} }
@ -106,7 +89,7 @@ const useSearchInput = (): SearchObject => {
* is on /search * is on /search
*/ */
useEffect(() => { useEffect(() => {
if (router.query.query !== encodeURIExtraParams(debouncedValue)) { if (router.query.query !== debouncedValue) {
setSearchValue( setSearchValue(
router.query.query router.query.query
? decodeURIComponent(router.query.query as string) ? decodeURIComponent(router.query.query as string)

Loading…
Cancel
Save