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
@@ -3,14 +3,15 @@ import mapMoviesSeriesResponseToShows from '../dto/Show';
|
||||
|
||||
export type PopularQueryProps = {
|
||||
sortBy?: string;
|
||||
type?: Show['type'];
|
||||
};
|
||||
|
||||
const popularQuery = ({ sortBy = 'popularity.desc' }: PopularQueryProps = {}) => ({
|
||||
queryKey: ['popular', sortBy],
|
||||
const popularQuery = ({ sortBy = 'popularity.desc', type = 'movie' }: PopularQueryProps = {}) => ({
|
||||
queryKey: ['popular', type, sortBy],
|
||||
initialPageParam: '1',
|
||||
queryFn: ({ pageParam = '1' }: { pageParam?: string }) =>
|
||||
movieAdvisor
|
||||
.get<TMDBPagination<Movie>>('/discover/movie', {
|
||||
.get<TMDBPagination<Movie> | TMDBPagination<Series>>(`/discover/${type}`, {
|
||||
params: {
|
||||
page: pageParam,
|
||||
sort_by: sortBy,
|
||||
@@ -19,7 +20,7 @@ const popularQuery = ({ sortBy = 'popularity.desc' }: PopularQueryProps = {}) =>
|
||||
'vote_average.gte': 5
|
||||
}
|
||||
})
|
||||
.then(mapMoviesSeriesResponseToShows)
|
||||
.then((response) => mapMoviesSeriesResponseToShows(response, type))
|
||||
});
|
||||
|
||||
export default popularQuery;
|
||||
+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;
|
||||
@@ -2,14 +2,34 @@ import movieAdvisor from '../clients/movieAdvisor';
|
||||
import mapMoviesSeriesResponseToShows from '../dto/Show';
|
||||
|
||||
export type TrendingQueryProps = {
|
||||
type: 'trending' | 'streaming' | 'theater';
|
||||
type: 'trending' | 'streaming' | 'theater' | 'apple' | 'netflix' | 'hbo';
|
||||
enabled?: boolean;
|
||||
};
|
||||
|
||||
const TV_WATCH_PROVIDERS: Partial<Record<TrendingQueryProps['type'], number>> = {
|
||||
apple: 350,
|
||||
netflix: 8,
|
||||
hbo: 1899
|
||||
};
|
||||
|
||||
const trendingQuery = ({ type, enabled }: TrendingQueryProps) => ({
|
||||
enabled,
|
||||
queryKey: ['trending', `type-${type}`],
|
||||
queryFn: () => {
|
||||
const provider = TV_WATCH_PROVIDERS[type];
|
||||
|
||||
if (provider) {
|
||||
return movieAdvisor
|
||||
.get<TMDBPagination<Series>>('/discover/tv', {
|
||||
params: {
|
||||
watch_region: 'US',
|
||||
with_watch_monetization_types: 'flatrate',
|
||||
with_watch_providers: provider
|
||||
}
|
||||
})
|
||||
.then((response) => mapMoviesSeriesResponseToShows(response, 'tv').results);
|
||||
}
|
||||
|
||||
const params: Record<string, string | number> = {};
|
||||
|
||||
if (type !== 'trending') {
|
||||
|
||||
Reference in new issue
Block a user