From 7640bdd53b7ef193ff08384dbc351e90486e1f0f Mon Sep 17 00:00:00 2001 From: enes Date: Tue, 29 Oct 2024 09:39:30 +0100 Subject: [PATCH] refactor: use SearchInput, search params to q, kind --- src/pages/games.tsx | 38 +++++---------------- src/pages/mods.tsx | 39 +++++----------------- src/pages/search.tsx | 79 ++++++++++++++++++-------------------------- 3 files changed, 49 insertions(+), 107 deletions(-) diff --git a/src/pages/games.tsx b/src/pages/games.tsx index 08d576e..2b77349 100644 --- a/src/pages/games.tsx +++ b/src/pages/games.tsx @@ -9,6 +9,7 @@ import '../styles/styles.css' import { createSearchParams, useNavigate } from 'react-router-dom' import { appRoutes } from 'routes' import { scrollIntoView } from 'utils' +import { SearchInput } from 'components/SearchInput' export const GamesPage = () => { const scrollTargetRef = useRef(null) @@ -74,8 +75,8 @@ export const GamesPage = () => { const value = searchTermRef.current?.value || '' // Access the input value from the ref if (value !== '') { const searchParams = createSearchParams({ - searchTerm: value, - searching: 'Games' + q: value, + kind: 'Games' }) navigate({ pathname: appRoutes.search, search: `?${searchParams}` }) } @@ -100,34 +101,11 @@ export const GamesPage = () => {

Games

-
-
-
- - -
-
-
+
diff --git a/src/pages/mods.tsx b/src/pages/mods.tsx index bc217aa..a5115f7 100644 --- a/src/pages/mods.tsx +++ b/src/pages/mods.tsx @@ -25,6 +25,7 @@ import { SortBy } from '../types' import { scrollIntoView } from 'utils' +import { SearchInput } from 'components/SearchInput' export const ModsPage = () => { const scrollTargetRef = useRef(null) @@ -146,8 +147,8 @@ const PageTitleRow = React.memo(() => { const value = searchTermRef.current?.value || '' // Access the input value from the ref if (value !== '') { const searchParams = createSearchParams({ - searchTerm: value, - searching: 'Mods' + q: value, + kind: 'Mods' }) navigate({ pathname: appRoutes.search, search: `?${searchParams}` }) } @@ -166,35 +167,11 @@ const PageTitleRow = React.memo(() => {

Mods

-
-
-
- - - -
-
-
+
) diff --git a/src/pages/search.tsx b/src/pages/search.tsx index ca31395..47022d2 100644 --- a/src/pages/search.tsx +++ b/src/pages/search.tsx @@ -13,6 +13,7 @@ import { ModCard } from 'components/ModCard' import { ModFilter } from 'components/ModsFilter' import { Pagination } from 'components/Pagination' import { Profile } from 'components/ProfileSection' +import { SearchInput } from 'components/SearchInput' import { MAX_GAMES_PER_PAGE, MAX_MODS_PER_PAGE, @@ -59,15 +60,14 @@ enum SearchKindEnum { export const SearchPage = () => { const scrollTargetRef = useRef(null) - const [searchParams] = useSearchParams() + const [searchParams, setSearchParams] = useSearchParams() const muteLists = useMuteLists() const nsfwList = useNSFWList() const searchTermRef = useRef(null) - const [searchKind, setSearchKind] = useState( - (searchParams.get('searching') as SearchKindEnum) || SearchKindEnum.Mods - ) + const searchKind = + (searchParams.get('kind') as SearchKindEnum) || SearchKindEnum.Mods const [filterOptions, setFilterOptions] = useState({ sort: SortBy.Latest, @@ -76,13 +76,21 @@ export const SearchPage = () => { moderated: ModeratedFilter.Moderated }) - const [searchTerm, setSearchTerm] = useState( - searchParams.get('searchTerm') || '' - ) + const [searchTerm, setSearchTerm] = useState(searchParams.get('q') || '') const handleSearch = () => { const value = searchTermRef.current?.value || '' // Access the input value from the ref setSearchTerm(value) + + if (value) { + searchParams.set('q', value) + } else { + searchParams.delete('q') + } + + setSearchParams(searchParams, { + replace: true + }) } // Handle "Enter" key press inside the input @@ -109,41 +117,16 @@ export const SearchPage = () => { -
-
-
- - -
-
-
+ {searchKind === SearchKindEnum.Mods && ( { type FiltersProps = { filterOptions: FilterOptions setFilterOptions: Dispatch> - searchKind: SearchKindEnum - setSearchKind: Dispatch> } const Filters = React.memo( - ({ - filterOptions, - setFilterOptions, - searchKind, - setSearchKind - }: FiltersProps) => { + ({ filterOptions, setFilterOptions }: FiltersProps) => { const userState = useAppSelector((state) => state.user) + const [searchParams, setSearchParams] = useSearchParams() + const searchKind = + (searchParams.get('kind') as SearchKindEnum) || SearchKindEnum.Mods + const handleChangeSearchKind = (kind: SearchKindEnum) => { + searchParams.set('kind', kind) + setSearchParams(searchParams, { + replace: true + }) + } return (
@@ -252,7 +237,7 @@ const Filters = React.memo(
setSearchKind(item)} + onClick={() => handleChangeSearchKind(item)} > {item}
@@ -324,6 +309,7 @@ const ModsResult = ({ }, [searchTerm]) const filteredMods = useMemo(() => { + // Search page requires search term if (searchTerm === '') return [] const lowerCaseSearchTerm = searchTerm.toLowerCase() @@ -338,6 +324,7 @@ const ModsResult = ({ ) > -1 const filterSourceFn = (mod: ModDetails) => { + // Filter by source if selected if (filterOptions.source === window.location.host) { return mod.rTag === filterOptions.source }