feat(search): unified movie+series search, results ranked by rating
This commit is contained in:
1 parent
75880e5ac7
commit
6cff846f94
4 files changed
+22
-13
No files matched your search
@@ -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<HTMLDivElement>(null) as MutableRefObject<HTMLDivElement>;
|
||||
const inputRef = useRef<HTMLInputElement>(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<HTMLInputElement>) => setTitle(value);
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
+18
-6
@@ -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<TMDBPagination<Movie>>(`/search/${type}`, { params: { page: pageParam, query } })
|
||||
.then((response) => mapMoviesSeriesResponseToShows(response, type)),
|
||||
Promise.all([
|
||||
movieAdvisor.get<TMDBPagination<Movie>>('/search/movie', { params: { page: pageParam, query } }),
|
||||
movieAdvisor.get<TMDBPagination<Series>>('/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)
|
||||
});
|
||||
|
||||
|
||||
@@ -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 };
|
||||
|
||||
Reference in new issue
Block a user