feat(search): unified movie+series search, results ranked by rating

This commit is contained in:
ner committed 2026-10-23 21:01:35 +03:00
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 { ChangeEvent, MutableRefObject, useRef, useState } from 'react';
import { create } from 'react-modal-promise'; import { create } from 'react-modal-promise';
import { Search as SearchIcon, X } from 'lucide-react'; import { Search as SearchIcon, X } from 'lucide-react';
import { usePathname } from 'next/navigation';
import List from '@/components/List'; import List from '@/components/List';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
@@ -11,7 +10,6 @@ import ButtonsGroup from '@/components/ui/buttons-group';
import { Modal } from '@/components/ui/dialog'; import { Modal } from '@/components/ui/dialog';
import { Input } from '@/components/ui/input'; import { Input } from '@/components/ui/input';
import useDistinctUntilChanged from '@/hooks/useDistinctUntilChanged'; import useDistinctUntilChanged from '@/hooks/useDistinctUntilChanged';
import { showTypeFromPath } from '@/lib/showType';
import useSearch from './useSearch'; import useSearch from './useSearch';
@@ -20,13 +18,12 @@ export const showSearchModal = create(({ onResolve }) => {
const scrollRef = useRef<HTMLDivElement>(null) as MutableRefObject<HTMLDivElement>; const scrollRef = useRef<HTMLDivElement>(null) as MutableRefObject<HTMLDivElement>;
const inputRef = useRef<HTMLInputElement>(null); const inputRef = useRef<HTMLInputElement>(null);
const isFetchedRef = useRef(false); const isFetchedRef = useRef(false);
const showType = showTypeFromPath(usePathname());
const query = useDistinctUntilChanged(title); const query = useDistinctUntilChanged(title);
const settledTitle = useDistinctUntilChanged(title, 1000); const settledTitle = useDistinctUntilChanged(title, 1000);
const settled = title === settledTitle; const settled = title === settledTitle;
const search = useSearch({ query, type: showType, settled }); const search = useSearch({ query, settled });
const handleChangeTitle = ({ target: { value } }: ChangeEvent<HTMLInputElement>) => setTitle(value); const handleChangeTitle = ({ target: { value } }: ChangeEvent<HTMLInputElement>) => setTitle(value);
@@ -12,8 +12,8 @@ const useSearch = (props: SearchQueryProps & { settled: boolean }) => {
useTrackOnce( useTrackOnce(
analytics.searchPerformed, analytics.searchPerformed,
props.query && props.settled && infiniteQuery.isFetched ? `${props.type}:${props.query}` : undefined, props.query && props.settled && infiniteQuery.isFetched ? props.query : undefined,
() => ({ query: props.query, showType: props.type, results: infiniteQuery.shows.length }) () => ({ query: props.query, showType: 'all' as const, results: infiniteQuery.shows.length })
); );
if (infiniteQuery.isFetched) { if (infiniteQuery.isFetched) {
+18 -6
View File
@@ -1,14 +1,26 @@
import movieAdvisor from '../clients/movieAdvisor'; import movieAdvisor from '../clients/movieAdvisor';
import mapMoviesSeriesResponseToShows from '../dto/Show'; import mapMoviesSeriesResponseToShows from '../dto/Show';
export type SearchQueryProps = { query: string; type: Show['type'] }; export type SearchQueryProps = { query: string };
const searchQuery = ({ query, type }: SearchQueryProps) => ({ const byRating = (a: Show, b: Show) => b.rating - a.rating || b.votes - a.votes || a.title.localeCompare(b.title);
queryKey: ['search', query, type],
const searchQuery = ({ query }: SearchQueryProps) => ({
queryKey: ['search', query],
queryFn: ({ pageParam }: { pageParam: string }) => queryFn: ({ pageParam }: { pageParam: string }) =>
movieAdvisor Promise.all([
.get<TMDBPagination<Movie>>(`/search/${type}`, { params: { page: pageParam, query } }) movieAdvisor.get<TMDBPagination<Movie>>('/search/movie', { params: { page: pageParam, query } }),
.then((response) => mapMoviesSeriesResponseToShows(response, type)), 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) enabled: Boolean(query)
}); });
+1 -1
View File
@@ -18,7 +18,7 @@ export type AnalyticsEvents = {
random_shuffled: { index: number; showTitle: string }; random_shuffled: { index: number; showTitle: string };
random_type_changed: { type: Show['type'] }; 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 }; gallery_tab_changed: { tab: string };
library_toggled: ShowRef & { list: 'favorite' | 'watchlist'; value: boolean }; library_toggled: ShowRef & { list: 'favorite' | 'watchlist'; value: boolean };