feat(show-type): global movie/series toggle, type-aware pages and search, ?type=tv in URL
This commit is contained in:
1 parent
5b41dbca10
commit
0824090c6d
21 files changed
+415
-174
No files matched your search
+34
-16
@@ -3,29 +3,47 @@ import { createUniqueRandomGenerator } from '@/lib/utils';
|
||||
import movieAdvisor from '../clients/movieAdvisor';
|
||||
import mapMoviesSeriesResponseToShows from '../dto/Show';
|
||||
|
||||
const TOTAL_PAGES = 400;
|
||||
// Pools are bounded by the discover API total_pages (movie: ~500+, tv: ~77).
|
||||
const TOTAL_PAGES: Record<Show['type'], number> = { movie: 400, tv: 70 };
|
||||
|
||||
export const generatePage = createUniqueRandomGenerator(TOTAL_PAGES);
|
||||
const generators = {} as Record<Show['type'], () => number>;
|
||||
|
||||
const getGenerator = (type: Show['type']) => (generators[type] ??= createUniqueRandomGenerator(TOTAL_PAGES[type]));
|
||||
|
||||
export const generatePage = (type: Show['type'] = 'movie') => getGenerator(type)();
|
||||
|
||||
export type RandomQueryProps = {
|
||||
page: number;
|
||||
type: Show['type'];
|
||||
};
|
||||
|
||||
const randomQuery = ({ page }: RandomQueryProps) => ({
|
||||
queryKey: ['random-movie'],
|
||||
const randomQuery = ({ page, type }: RandomQueryProps) => ({
|
||||
queryKey: ['random-movie', type],
|
||||
initialPageParam: page.toString(),
|
||||
getNextPageParam: () => generatePage().toString(),
|
||||
queryFn: ({ pageParam }: { pageParam: string }) =>
|
||||
movieAdvisor
|
||||
.get<TMDBPagination<Movie>>('/discover/movie', {
|
||||
params: {
|
||||
'vote_count.gte': 300,
|
||||
sort_by: 'popularity.desc',
|
||||
with_origin_country: 'UA|GB|JP|AU|US|IT|DE|FR',
|
||||
page: pageParam
|
||||
}
|
||||
})
|
||||
.then(mapMoviesSeriesResponseToShows)
|
||||
getNextPageParam: () => generatePage(type).toString(),
|
||||
queryFn: async ({ pageParam }: { pageParam: string }) => {
|
||||
const params = {
|
||||
'vote_count.gte': 300,
|
||||
sort_by: 'popularity.desc',
|
||||
with_origin_country: 'UA|GB|JP|AU|US|IT|DE|FR',
|
||||
page: pageParam
|
||||
};
|
||||
|
||||
const data = await movieAdvisor.get<TMDBPagination<Movie> | TMDBPagination<Series>>(`/discover/${type}`, {
|
||||
params
|
||||
});
|
||||
|
||||
if (data.results.length || !data.total_pages) {
|
||||
return mapMoviesSeriesResponseToShows(data, type);
|
||||
}
|
||||
|
||||
// Out-of-range random page: TMDB returns empty results, retry with a valid page
|
||||
const fallback = await movieAdvisor.get<TMDBPagination<Movie> | TMDBPagination<Series>>(`/discover/${type}`, {
|
||||
params: { ...params, page: Math.floor(Math.random() * data.total_pages) + 1 }
|
||||
});
|
||||
|
||||
return mapMoviesSeriesResponseToShows(fallback, type);
|
||||
}
|
||||
});
|
||||
|
||||
export default randomQuery;
|
||||
Reference in new issue
Block a user