diff --git a/components/Header/components/Search/index.tsx b/components/Header/components/Search/index.tsx index d3cb0f2..3771328 100644 --- a/components/Header/components/Search/index.tsx +++ b/components/Header/components/Search/index.tsx @@ -3,7 +3,6 @@ import { ChangeEvent, MutableRefObject, useRef, useState } from 'react'; import { create } from 'react-modal-promise'; import { Search as SearchIcon, X } from 'lucide-react'; -import { usePathname } from 'next/navigation'; import List from '@/components/List'; import { Button } from '@/components/ui/button'; @@ -11,7 +10,6 @@ import ButtonsGroup from '@/components/ui/buttons-group'; import { Modal } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import useDistinctUntilChanged from '@/hooks/useDistinctUntilChanged'; -import { showTypeFromPath } from '@/lib/showType'; import useSearch from './useSearch'; @@ -20,13 +18,12 @@ export const showSearchModal = create(({ onResolve }) => { const scrollRef = useRef(null) as MutableRefObject; const inputRef = useRef(null); const isFetchedRef = useRef(false); - const showType = showTypeFromPath(usePathname()); const query = useDistinctUntilChanged(title); const settledTitle = useDistinctUntilChanged(title, 1000); const settled = title === settledTitle; - const search = useSearch({ query, type: showType, settled }); + const search = useSearch({ query, settled }); const handleChangeTitle = ({ target: { value } }: ChangeEvent) => setTitle(value); diff --git a/components/Header/components/Search/useSearch.ts b/components/Header/components/Search/useSearch.ts index 725ee80..fa0dca6 100644 --- a/components/Header/components/Search/useSearch.ts +++ b/components/Header/components/Search/useSearch.ts @@ -12,8 +12,8 @@ const useSearch = (props: SearchQueryProps & { settled: boolean }) => { useTrackOnce( analytics.searchPerformed, - props.query && props.settled && infiniteQuery.isFetched ? `${props.type}:${props.query}` : undefined, - () => ({ query: props.query, showType: props.type, results: infiniteQuery.shows.length }) + props.query && props.settled && infiniteQuery.isFetched ? props.query : undefined, + () => ({ query: props.query, showType: 'all' as const, results: infiniteQuery.shows.length }) ); if (infiniteQuery.isFetched) { diff --git a/data/queries/search.ts b/data/queries/search.ts index b5f609f..b87baf4 100644 --- a/data/queries/search.ts +++ b/data/queries/search.ts @@ -1,14 +1,26 @@ import movieAdvisor from '../clients/movieAdvisor'; import mapMoviesSeriesResponseToShows from '../dto/Show'; -export type SearchQueryProps = { query: string; type: Show['type'] }; +export type SearchQueryProps = { query: string }; -const searchQuery = ({ query, type }: SearchQueryProps) => ({ - queryKey: ['search', query, type], +const byRating = (a: Show, b: Show) => b.rating - a.rating || b.votes - a.votes || a.title.localeCompare(b.title); + +const searchQuery = ({ query }: SearchQueryProps) => ({ + queryKey: ['search', query], queryFn: ({ pageParam }: { pageParam: string }) => - movieAdvisor - .get>(`/search/${type}`, { params: { page: pageParam, query } }) - .then((response) => mapMoviesSeriesResponseToShows(response, type)), + Promise.all([ + movieAdvisor.get>('/search/movie', { params: { page: pageParam, query } }), + movieAdvisor.get>('/search/tv', { params: { page: pageParam, query } }) + ]).then(([movies, series]) => { + const moviesPage = mapMoviesSeriesResponseToShows(movies, 'movie'); + const seriesPage = mapMoviesSeriesResponseToShows(series, 'tv'); + + return { + page: Math.max(moviesPage.page, seriesPage.page), + total: Math.max(moviesPage.total, seriesPage.total), + results: [...moviesPage.results, ...seriesPage.results].sort(byRating) + }; + }), enabled: Boolean(query) }); diff --git a/lib/analytics/events.ts b/lib/analytics/events.ts index 4deac73..7a5bb84 100644 --- a/lib/analytics/events.ts +++ b/lib/analytics/events.ts @@ -18,7 +18,7 @@ export type AnalyticsEvents = { random_shuffled: { index: number; showTitle: string }; random_type_changed: { type: Show['type'] }; - search_performed: { query: string; showType: Show['type']; results: number }; + search_performed: { query: string; showType: Show['type'] | 'all'; results: number }; gallery_tab_changed: { tab: string }; library_toggled: ShowRef & { list: 'favorite' | 'watchlist'; value: boolean };