From f22f3c45b4e787a201b1a73d4ea9061e5571e8a7 Mon Sep 17 00:00:00 2001 From: ner Date: Thu, 24 Sep 2026 15:53:54 +0300 Subject: [PATCH] feat(show-type): path-based /series filter and infinite trending galleries Persist the show-type filter in the path via a /series prefix: movies keep bare URLs, series use /series across random, popular, top, favorites and watchlist. The path is the single source of truth - the jotai atom is removed and the toggle navigates with router.replace so the server prefetches the target variant. Legacy ?type=tv deep links redirect to the prefixed URLs. Trending and similar/recommendations queries are now paginated and feed useInfiniteList like the main lists. Gallery wires fetchNextPage to a new ScrollNavigation onEndReached callback (fires within two slides of the end, once per approach), so all gallery tabs load more on scroll. The show page prefetches the first similar page with prefetchInfiniteQuery, and trending/similar/recommendations are added to the analytics ListName. --- .../components/UsersLists/index.tsx | 10 +-- app/(site)/(favorites)/favorites/page.tsx | 11 ++- app/(site)/(favorites)/watchlist/page.tsx | 11 ++- app/(site)/(root)/container.tsx | 24 +----- app/(site)/(root)/page.tsx | 11 +-- app/(site)/(show)/showPage.tsx | 2 +- app/(site)/popular/container.tsx | 6 +- app/(site)/popular/content.tsx | 30 +++++++ app/(site)/popular/page.tsx | 79 ++++++------------- app/(site)/popular/usePopular.ts | 10 +-- app/(site)/series/favorites/page.tsx | 19 +++++ app/(site)/series/page.tsx | 30 +++++++ app/(site)/series/popular/page.tsx | 31 ++++++++ app/(site)/series/top/page.tsx | 31 ++++++++ app/(site)/series/watchlist/page.tsx | 19 +++++ app/(site)/top/container.tsx | 6 +- app/(site)/top/content.tsx | 30 +++++++ app/(site)/top/page.tsx | 79 ++++++------------- app/(site)/top/useTop.ts | 14 +--- .../Header/components/Navigation/index.tsx | 23 +++--- components/Header/components/Search/index.tsx | 6 +- components/ScrollNavigation/index.tsx | 24 +++++- components/ShowTypeToggle/index.tsx | 29 +++---- .../Trending/components/Gallery/index.tsx | 5 +- components/Trending/index.tsx | 2 +- components/Trending/useSimilar.ts | 5 +- components/Trending/useTrending.ts | 6 +- data/queries/similar.ts | 7 +- data/queries/trending.ts | 16 ++-- lib/analytics/events.ts | 11 ++- lib/showType.ts | 14 +++- 31 files changed, 372 insertions(+), 229 deletions(-) create mode 100644 app/(site)/popular/content.tsx create mode 100644 app/(site)/series/favorites/page.tsx create mode 100644 app/(site)/series/page.tsx create mode 100644 app/(site)/series/popular/page.tsx create mode 100644 app/(site)/series/top/page.tsx create mode 100644 app/(site)/series/watchlist/page.tsx create mode 100644 app/(site)/top/content.tsx diff --git a/app/(site)/(favorites)/components/UsersLists/index.tsx b/app/(site)/(favorites)/components/UsersLists/index.tsx index ee1e234..5404158 100644 --- a/app/(site)/(favorites)/components/UsersLists/index.tsx +++ b/app/(site)/(favorites)/components/UsersLists/index.tsx @@ -1,26 +1,24 @@ 'use client'; import { FC } from 'react'; -import { useAtomValue } from 'jotai'; import List from '@/components/List'; -import { showTypeAtom } from '@/components/ShowTypeToggle'; import useUsersShows from '@/hooks/useUsersShows'; interface Props { list: 'favorite' | 'watchlist'; label: string; + type: Show['type']; session?: string; } -const UsersList: FC = ({ list, label, session }) => { - const showType = useAtomValue(showTypeAtom); - const { shows, fetchNextPage, isFetched } = useUsersShows({ list, showType, session, mode: 'default' }); +const UsersList: FC = ({ list, label, type, session }) => { + const { shows, fetchNextPage, isFetched } = useUsersShows({ list, showType: type, session, mode: 'default' }); if (isFetched && !shows.length) { return (
- There are no {showType === 'tv' ? 'series' : 'movies'} in your {label} + There are no {type === 'tv' ? 'series' : 'movies'} in your {label}
); } diff --git a/app/(site)/(favorites)/favorites/page.tsx b/app/(site)/(favorites)/favorites/page.tsx index 2e4e528..62bd63f 100644 --- a/app/(site)/(favorites)/favorites/page.tsx +++ b/app/(site)/(favorites)/favorites/page.tsx @@ -1,4 +1,5 @@ import { Metadata } from 'next'; +import { redirect } from 'next/navigation'; import { TITLE } from '@/env'; @@ -10,11 +11,17 @@ export const metadata: Metadata = { robots: { index: false } }; -const Favorites = () => { +interface SearchParams { + type?: string; +} + +const Favorites = async ({ searchParams }: { searchParams: SearchParams }) => { + if (searchParams.type === 'tv') redirect('/series/favorites'); + return ( <>

My Favorite Shows

- + ); }; diff --git a/app/(site)/(favorites)/watchlist/page.tsx b/app/(site)/(favorites)/watchlist/page.tsx index ef359a2..41677a2 100644 --- a/app/(site)/(favorites)/watchlist/page.tsx +++ b/app/(site)/(favorites)/watchlist/page.tsx @@ -1,4 +1,5 @@ import { Metadata } from 'next'; +import { redirect } from 'next/navigation'; import { TITLE } from '@/env'; @@ -10,11 +11,17 @@ export const metadata: Metadata = { robots: { index: false } }; -const WatchList = async () => { +interface SearchParams { + type?: string; +} + +const WatchList = async ({ searchParams }: { searchParams: SearchParams }) => { + if (searchParams.type === 'tv') redirect('/series/watchlist'); + return ( <>

My Watchlist

- + ); }; diff --git a/app/(site)/(root)/container.tsx b/app/(site)/(root)/container.tsx index bf5e127..309ab85 100644 --- a/app/(site)/(root)/container.tsx +++ b/app/(site)/(root)/container.tsx @@ -1,13 +1,10 @@ 'use client'; -import { FC, useRef } from 'react'; -import { useAtom } from 'jotai'; +import { FC } from 'react'; import Preview from '@/components/Preview'; -import { showTypeAtom } from '@/components/ShowTypeToggle'; import Trending from '@/components/Trending'; import { Skeleton } from '@/components/ui/skeleton'; -import { generatePage } from '@/data/queries/random'; import Carousel from './components/Carousel'; import useRandomMovie from './useRandomMovie'; @@ -18,20 +15,7 @@ interface Props { } const Container: FC = ({ page, type }) => { - const [showType] = useAtom(showTypeAtom); - const pagesRef = useRef>(); - - if (!pagesRef.current) { - pagesRef.current = { - movie: type === 'movie' ? page : generatePage('movie'), - tv: type === 'tv' ? page : generatePage('tv') - }; - } - - const { show, shows, onIndexChange, fetchNextPage, isLoading } = useRandomMovie({ - page: pagesRef.current[showType], - type: showType - }); + const { show, shows, onIndexChange, fetchNextPage } = useRandomMovie({ page, type }); return (
@@ -40,9 +24,7 @@ const Container: FC = ({ page, type }) => { - } + poster={} /> ) : ( diff --git a/app/(site)/(root)/page.tsx b/app/(site)/(root)/page.tsx index a2196b7..16c5219 100644 --- a/app/(site)/(root)/page.tsx +++ b/app/(site)/(root)/page.tsx @@ -1,12 +1,12 @@ import { dehydrate, HydrationBoundary } from '@tanstack/react-query'; import { Metadata } from 'next'; import Link from 'next/link'; +import { redirect } from 'next/navigation'; import JsonLd from '@/components/JsonLd'; import randomQuery, { generatePage } from '@/data/queries/random'; import { SITE_URL, TITLE } from '@/env'; import getQueryClient from '@/lib/queryClient'; -import { parseShowType } from '@/lib/showType'; import Container from './container'; @@ -19,18 +19,19 @@ export const metadata: Metadata = { }; const Random = async ({ searchParams }: { searchParams: SearchParams }) => { + if (searchParams.type === 'tv') redirect('/series'); + const queryClient = getQueryClient(); - const type = parseShowType(searchParams.type); - const page = generatePage(type); + const page = generatePage('movie'); - await queryClient.prefetchInfiniteQuery(randomQuery({ page, type })); + await queryClient.prefetchInfiniteQuery(randomQuery({ page, type: 'movie' })); return (

{TITLE} — Discover Your Next Favorite Movie

- +

Discover your next favorite movie

diff --git a/app/(site)/(show)/showPage.tsx b/app/(site)/(show)/showPage.tsx index 713d4cc..199333b 100644 --- a/app/(site)/(show)/showPage.tsx +++ b/app/(site)/(show)/showPage.tsx @@ -69,7 +69,7 @@ export const createShowPage = (showType: Show['type']) => { if (decodeURIComponent(`/${showType}/${id}`) !== path) permanentRedirect(path); - await queryClient.prefetchQuery(similarQuery({ showId, showType, type: 'similar' })); + await queryClient.prefetchInfiniteQuery(similarQuery({ showId, showType, type: 'similar' })); const jsonLd = { '@context': 'https://schema.org', diff --git a/app/(site)/popular/container.tsx b/app/(site)/popular/container.tsx index f885947..979cfb8 100644 --- a/app/(site)/popular/container.tsx +++ b/app/(site)/popular/container.tsx @@ -1,12 +1,14 @@ 'use client'; +import { FC } from 'react'; + import List from '@/components/List'; import NoResults from '@/components/NoResults'; import usePopular from './usePopular'; -const Container = () => { - const { shows, fetchNextPage, isFetched } = usePopular(); +const Container: FC<{ type: Show['type'] }> = ({ type }) => { + const { shows, fetchNextPage, isFetched } = usePopular(type); if (isFetched && !shows.length) return ; diff --git a/app/(site)/popular/content.tsx b/app/(site)/popular/content.tsx new file mode 100644 index 0000000..999406b --- /dev/null +++ b/app/(site)/popular/content.tsx @@ -0,0 +1,30 @@ +import { dehydrate, HydrationBoundary } from '@tanstack/react-query'; + +import JsonLd from '@/components/JsonLd'; +import popularQuery from '@/data/queries/popular'; +import { itemList } from '@/lib/jsonLd'; +import getQueryClient from '@/lib/queryClient'; + +import Container from './container'; + +const PopularContent = async ({ type }: { type: Show['type'] }) => { + const queryClient = getQueryClient(); + + const query = popularQuery({ type }); + + await queryClient.prefetchInfiniteQuery(query); + + const shows = queryClient + .getQueryData<{ pages: Pagination[] }>(query.queryKey) + ?.pages.flatMap(({ results }) => results); + + return ( + + {Boolean(shows?.length) && } +

{type === 'tv' ? 'Popular Series' : 'Popular Movies'}

+ + + ); +}; + +export default PopularContent; diff --git a/app/(site)/popular/page.tsx b/app/(site)/popular/page.tsx index aa4b84e..066505a 100644 --- a/app/(site)/popular/page.tsx +++ b/app/(site)/popular/page.tsx @@ -1,68 +1,39 @@ -import { dehydrate, HydrationBoundary } from '@tanstack/react-query'; import { Metadata } from 'next'; +import { redirect } from 'next/navigation'; -import JsonLd from '@/components/JsonLd'; -import popularQuery from '@/data/queries/popular'; import { TITLE } from '@/env'; -import { itemList } from '@/lib/jsonLd'; -import getQueryClient from '@/lib/queryClient'; -import { parseShowType } from '@/lib/showType'; -import Container from './container'; +import PopularContent from './content'; + +const HEADING = 'Popular Movies'; + +const DESCRIPTION = `Check out the most popular movies right now on ${TITLE}. See what’s trending and don’t miss out on the latest hits.`; + +export const metadata: Metadata = { + title: `${HEADING} | ${TITLE}`, + description: DESCRIPTION, + alternates: { canonical: '/popular' }, + openGraph: { + type: 'website', + siteName: TITLE, + title: HEADING, + description: DESCRIPTION, + url: '/popular' + }, + twitter: { + title: HEADING, + description: DESCRIPTION + } +}; interface SearchParams { type?: string; } -const heading = (type: Show['type']) => (type === 'tv' ? 'Popular Series' : 'Popular Movies'); - -const description = (type: Show['type']) => - type === 'tv' - ? `Check out the most popular series right now on ${TITLE}. See what’s trending and don’t miss out on the latest hits.` - : `Check out the most popular movies right now on ${TITLE}. See what’s trending and don’t miss out on the latest hits.`; - -export const generateMetadata = ({ searchParams }: { searchParams: SearchParams }): Metadata => { - const type = parseShowType(searchParams.type); - const title = heading(type); - const text = description(type); - - return { - title: `${title} | ${TITLE}`, - description: text, - alternates: { canonical: '/popular' }, - openGraph: { - type: 'website', - siteName: TITLE, - title, - description: text, - url: '/popular' - }, - twitter: { - title, - description: text - } - }; -}; - const Popular = async ({ searchParams }: { searchParams: SearchParams }) => { - const queryClient = getQueryClient(); + if (searchParams.type === 'tv') redirect('/series/popular'); - const type = parseShowType(searchParams.type); - const query = popularQuery({ type }); - - await queryClient.prefetchInfiniteQuery(query); - - const shows = queryClient - .getQueryData<{ pages: Pagination[] }>(query.queryKey) - ?.pages.flatMap(({ results }) => results); - - return ( - - {Boolean(shows?.length) && } -

{heading(type)}

- -
- ); + return ; }; export const revalidate = 3600; diff --git a/app/(site)/popular/usePopular.ts b/app/(site)/popular/usePopular.ts index adda42f..532ed45 100644 --- a/app/(site)/popular/usePopular.ts +++ b/app/(site)/popular/usePopular.ts @@ -1,15 +1,9 @@ 'use client'; -import { useAtomValue } from 'jotai'; - -import { showTypeAtom } from '@/components/ShowTypeToggle'; import popularQuery from '@/data/queries/popular'; import useInfiniteList from '@/hooks/useInfiniteList'; -const usePopular = () => { - const showType = useAtomValue(showTypeAtom); - - return useInfiniteList({ ...popularQuery({ type: showType }), mode: 'default', list: 'popular' }); -}; +const usePopular = (type: Show['type']) => + useInfiniteList({ ...popularQuery({ type }), mode: 'default', list: 'popular' }); export default usePopular; diff --git a/app/(site)/series/favorites/page.tsx b/app/(site)/series/favorites/page.tsx new file mode 100644 index 0000000..81066f1 --- /dev/null +++ b/app/(site)/series/favorites/page.tsx @@ -0,0 +1,19 @@ +import { Metadata } from 'next'; + +import UsersList from '@/app/(site)/(favorites)/components/UsersLists'; +import { TITLE } from '@/env'; + +export const metadata: Metadata = { + title: `My Favorite Shows | ${TITLE}`, + description: `View your favorite shows on ${TITLE}. Keep track of films you’ve added and revisit your top picks anytime.`, + robots: { index: false } +}; + +const Favorites = () => ( + <> +

My Favorite Shows

+ + +); + +export default Favorites; diff --git a/app/(site)/series/page.tsx b/app/(site)/series/page.tsx new file mode 100644 index 0000000..c4c5020 --- /dev/null +++ b/app/(site)/series/page.tsx @@ -0,0 +1,30 @@ +import { dehydrate, HydrationBoundary } from '@tanstack/react-query'; +import { Metadata } from 'next'; + +import Container from '@/app/(site)/(root)/container'; +import randomQuery, { generatePage } from '@/data/queries/random'; +import { TITLE } from '@/env'; +import getQueryClient from '@/lib/queryClient'; + +export const metadata: Metadata = { + alternates: { canonical: '/series' } +}; + +const Series = async () => { + const queryClient = getQueryClient(); + + const page = generatePage('tv'); + + await queryClient.prefetchInfiniteQuery(randomQuery({ page, type: 'tv' })); + + return ( + +

{TITLE} — Discover Your Next Favorite Series

+ +
+ ); +}; + +export const revalidate = 1; + +export default Series; diff --git a/app/(site)/series/popular/page.tsx b/app/(site)/series/popular/page.tsx new file mode 100644 index 0000000..4c3126d --- /dev/null +++ b/app/(site)/series/popular/page.tsx @@ -0,0 +1,31 @@ +import { Metadata } from 'next'; + +import PopularContent from '@/app/(site)/popular/content'; +import { TITLE } from '@/env'; + +const HEADING = 'Popular Series'; + +const DESCRIPTION = `Check out the most popular series right now on ${TITLE}. See what’s trending and don’t miss out on the latest hits.`; + +export const metadata: Metadata = { + title: `${HEADING} | ${TITLE}`, + description: DESCRIPTION, + alternates: { canonical: '/series/popular' }, + openGraph: { + type: 'website', + siteName: TITLE, + title: HEADING, + description: DESCRIPTION, + url: '/series/popular' + }, + twitter: { + title: HEADING, + description: DESCRIPTION + } +}; + +const Popular = () => ; + +export const revalidate = 3600; + +export default Popular; diff --git a/app/(site)/series/top/page.tsx b/app/(site)/series/top/page.tsx new file mode 100644 index 0000000..8be1e3f --- /dev/null +++ b/app/(site)/series/top/page.tsx @@ -0,0 +1,31 @@ +import { Metadata } from 'next'; + +import TopContent from '@/app/(site)/top/content'; +import { TITLE } from '@/env'; + +const HEADING = 'Top Rated Series'; + +const DESCRIPTION = `Discover the top-rated series on ${TITLE}. Find the highest-rated shows and make informed viewing choices.`; + +export const metadata: Metadata = { + title: `${HEADING} | ${TITLE}`, + description: DESCRIPTION, + alternates: { canonical: '/series/top' }, + openGraph: { + type: 'website', + siteName: TITLE, + title: HEADING, + description: DESCRIPTION, + url: '/series/top' + }, + twitter: { + title: HEADING, + description: DESCRIPTION + } +}; + +const Top = () => ; + +export const revalidate = 3600; + +export default Top; diff --git a/app/(site)/series/watchlist/page.tsx b/app/(site)/series/watchlist/page.tsx new file mode 100644 index 0000000..3546338 --- /dev/null +++ b/app/(site)/series/watchlist/page.tsx @@ -0,0 +1,19 @@ +import { Metadata } from 'next'; + +import UsersList from '@/app/(site)/(favorites)/components/UsersLists'; +import { TITLE } from '@/env'; + +export const metadata: Metadata = { + title: `My watchlist | ${TITLE}`, + description: `View your favorite shows on ${TITLE}. Keep track of films you’ve added and revisit your top picks anytime.`, + robots: { index: false } +}; + +const WatchList = () => ( + <> +

My Watchlist

+ + +); + +export default WatchList; diff --git a/app/(site)/top/container.tsx b/app/(site)/top/container.tsx index 6f102ac..a8559d8 100644 --- a/app/(site)/top/container.tsx +++ b/app/(site)/top/container.tsx @@ -1,12 +1,14 @@ 'use client'; +import { FC } from 'react'; + import List from '@/components/List'; import NoResults from '@/components/NoResults'; import useTop from './useTop'; -const Container = () => { - const { shows, fetchNextPage, isFetched } = useTop(); +const Container: FC<{ type: Show['type'] }> = ({ type }) => { + const { shows, fetchNextPage, isFetched } = useTop(type); if (isFetched && !shows.length) return ; diff --git a/app/(site)/top/content.tsx b/app/(site)/top/content.tsx new file mode 100644 index 0000000..79f0784 --- /dev/null +++ b/app/(site)/top/content.tsx @@ -0,0 +1,30 @@ +import { dehydrate, HydrationBoundary } from '@tanstack/react-query'; + +import JsonLd from '@/components/JsonLd'; +import popularQuery from '@/data/queries/popular'; +import { itemList } from '@/lib/jsonLd'; +import getQueryClient from '@/lib/queryClient'; + +import Container from './container'; + +const TopContent = async ({ type }: { type: Show['type'] }) => { + const queryClient = getQueryClient(); + + const query = popularQuery({ sortBy: 'vote_average.desc', type }); + + await queryClient.prefetchInfiniteQuery(query); + + const shows = queryClient + .getQueryData<{ pages: Pagination[] }>(query.queryKey) + ?.pages.flatMap(({ results }) => results); + + return ( + + {Boolean(shows?.length) && } +

{type === 'tv' ? 'Top Rated Series' : 'Top Rated Movies'}

+ +
+ ); +}; + +export default TopContent; diff --git a/app/(site)/top/page.tsx b/app/(site)/top/page.tsx index 18f9383..df67a70 100644 --- a/app/(site)/top/page.tsx +++ b/app/(site)/top/page.tsx @@ -1,68 +1,39 @@ -import { dehydrate, HydrationBoundary } from '@tanstack/react-query'; import { Metadata } from 'next'; +import { redirect } from 'next/navigation'; -import JsonLd from '@/components/JsonLd'; -import popularQuery from '@/data/queries/popular'; import { TITLE } from '@/env'; -import { itemList } from '@/lib/jsonLd'; -import getQueryClient from '@/lib/queryClient'; -import { parseShowType } from '@/lib/showType'; -import Container from './container'; +import TopContent from './content'; + +const HEADING = 'Top Rated Movies'; + +const DESCRIPTION = `Discover the top-rated movies on ${TITLE}. Find the highest-rated films and make informed viewing choices.`; + +export const metadata: Metadata = { + title: `${HEADING} | ${TITLE}`, + description: DESCRIPTION, + alternates: { canonical: '/top' }, + openGraph: { + type: 'website', + siteName: TITLE, + title: HEADING, + description: DESCRIPTION, + url: '/top' + }, + twitter: { + title: HEADING, + description: DESCRIPTION + } +}; interface SearchParams { type?: string; } -const heading = (type: Show['type']) => (type === 'tv' ? 'Top Rated Series' : 'Top Rated Movies'); - -const description = (type: Show['type']) => - type === 'tv' - ? `Discover the top-rated series on ${TITLE}. Find the highest-rated shows and make informed viewing choices.` - : `Discover the top-rated movies on ${TITLE}. Find the highest-rated films and make informed viewing choices.`; - -export const generateMetadata = ({ searchParams }: { searchParams: SearchParams }): Metadata => { - const type = parseShowType(searchParams.type); - const title = heading(type); - const text = description(type); - - return { - title: `${title} | ${TITLE}`, - description: text, - alternates: { canonical: '/top' }, - openGraph: { - type: 'website', - siteName: TITLE, - title, - description: text, - url: '/top' - }, - twitter: { - title, - description: text - } - }; -}; - const Top = async ({ searchParams }: { searchParams: SearchParams }) => { - const queryClient = getQueryClient(); + if (searchParams.type === 'tv') redirect('/series/top'); - const type = parseShowType(searchParams.type); - const query = popularQuery({ sortBy: 'vote_average.desc', type }); - - await queryClient.prefetchInfiniteQuery(query); - - const shows = queryClient - .getQueryData<{ pages: Pagination[] }>(query.queryKey) - ?.pages.flatMap(({ results }) => results); - - return ( - - {Boolean(shows?.length) && } -

{heading(type)}

- -
- ); + return ; }; export const revalidate = 3600; diff --git a/app/(site)/top/useTop.ts b/app/(site)/top/useTop.ts index 38764b8..63b0045 100644 --- a/app/(site)/top/useTop.ts +++ b/app/(site)/top/useTop.ts @@ -1,19 +1,9 @@ 'use client'; -import { useAtomValue } from 'jotai'; - -import { showTypeAtom } from '@/components/ShowTypeToggle'; import popularQuery from '@/data/queries/popular'; import useInfiniteList from '@/hooks/useInfiniteList'; -const useTop = () => { - const showType = useAtomValue(showTypeAtom); - - return useInfiniteList({ - ...popularQuery({ sortBy: 'vote_average.desc', type: showType }), - mode: 'default', - list: 'top' - }); -}; +const useTop = (type: Show['type']) => + useInfiniteList({ ...popularQuery({ sortBy: 'vote_average.desc', type }), mode: 'default', list: 'top' }); export default useTop; diff --git a/components/Header/components/Navigation/index.tsx b/components/Header/components/Navigation/index.tsx index 89f6535..1ad5478 100644 --- a/components/Header/components/Navigation/index.tsx +++ b/components/Header/components/Navigation/index.tsx @@ -1,27 +1,21 @@ 'use client'; import { DropdownMenuItem } from '@radix-ui/react-dropdown-menu'; -import { useAtomValue } from 'jotai'; import { Menu } from 'lucide-react'; import Link from 'next/link'; import { usePathname } from 'next/navigation'; -import { showTypeAtom } from '@/components/ShowTypeToggle'; import { Button } from '@/components/ui/button'; import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger } from '@/components/ui/dropdown-menu'; import Nav from '@/components/ui/nav'; +import { showTypeFromPath, toShowTypePath } from '@/lib/showType'; import { cn } from '@/lib/utils'; import { paths } from './constants'; -const TYPE_AWARE_PATHS = ['/', '/popular', '/top', '/favorites', '/watchlist']; - -const withShowType = (path: string, type: Show['type']) => - type === 'tv' && TYPE_AWARE_PATHS.includes(path) ? `${path}?type=tv` : path; - export const MobileNavigation = () => { const currentPath = usePathname(); - const showType = useAtomValue(showTypeAtom); + const showType = showTypeFromPath(currentPath); return (
@@ -34,9 +28,12 @@ export const MobileNavigation = () => { {paths.map(({ path, title }) => ( - +
-
@@ -51,13 +48,13 @@ export const MobileNavigation = () => { export const DesktopNavigation = () => { const currentPath = usePathname(); - const showType = useAtomValue(showTypeAtom); - const active = paths.find(({ path }) => path === currentPath); + const showType = showTypeFromPath(currentPath); + const active = paths.find(({ path }) => toShowTypePath(path, showType) === currentPath); return (
) ); diff --git a/components/Trending/useSimilar.ts b/components/Trending/useSimilar.ts index 8b07906..89f184b 100644 --- a/components/Trending/useSimilar.ts +++ b/components/Trending/useSimilar.ts @@ -1,8 +1,7 @@ 'use client'; -import { useQuery } from '@tanstack/react-query'; - import similarQuery from '@/data/queries/similar'; +import useInfiniteList from '@/hooks/useInfiniteList'; interface Props { showId: Show['id']; @@ -12,6 +11,6 @@ interface Props { } const useSimilar = ({ showId, showType = 'movie', type = 'similar', enabled }: Props) => - useQuery({ ...similarQuery({ showId, showType, type }), enabled }); + useInfiniteList({ ...similarQuery({ showId, showType, type }), mode: 'default', list: type, enabled }); export default useSimilar; diff --git a/components/Trending/useTrending.ts b/components/Trending/useTrending.ts index f5622fd..6e2b20d 100644 --- a/components/Trending/useTrending.ts +++ b/components/Trending/useTrending.ts @@ -1,7 +1,7 @@ -import { useQuery } from '@tanstack/react-query'; - import trendingQuery, { TrendingQueryProps } from '@/data/queries/trending'; +import useInfiniteList from '@/hooks/useInfiniteList'; -const useTrending = ({ type, enabled }: TrendingQueryProps) => useQuery(trendingQuery({ type, enabled })); +const useTrending = ({ type, enabled }: TrendingQueryProps) => + useInfiniteList({ ...trendingQuery({ type, enabled }), mode: 'default', list: 'trending' }); export default useTrending; diff --git a/data/queries/similar.ts b/data/queries/similar.ts index 377bcf4..4b11e57 100644 --- a/data/queries/similar.ts +++ b/data/queries/similar.ts @@ -9,14 +9,15 @@ export type SimilarQueryProps = { const similarQuery = ({ showId, showType, type }: SimilarQueryProps) => ({ queryKey: [type, showId, showType], - queryFn: () => + initialPageParam: '1', + queryFn: ({ pageParam = '1' }: { pageParam?: string }) => movieAdvisor .get | TMDBPagination>(`/${showType}/${showId}/${type}`, { params: { - page: 1 + page: pageParam } }) - .then((response) => mapMoviesSeriesResponseToShows(response, showType).results) + .then((response) => mapMoviesSeriesResponseToShows(response, showType)) }); export default similarQuery; diff --git a/data/queries/trending.ts b/data/queries/trending.ts index 001fc47..cff1622 100644 --- a/data/queries/trending.ts +++ b/data/queries/trending.ts @@ -15,22 +15,26 @@ const TV_WATCH_PROVIDERS: Partial> = const trendingQuery = ({ type, enabled }: TrendingQueryProps) => ({ enabled, queryKey: ['trending', `type-${type}`], - queryFn: () => { + initialPageParam: '1', + queryFn: ({ pageParam = '1' }: { pageParam?: string }) => { const provider = TV_WATCH_PROVIDERS[type]; if (provider) { return movieAdvisor .get>('/discover/tv', { params: { + page: pageParam, watch_region: 'US', with_watch_monetization_types: 'flatrate', with_watch_providers: provider } }) - .then((response) => mapMoviesSeriesResponseToShows(response, 'tv').results); + .then((response) => mapMoviesSeriesResponseToShows(response, 'tv')); } - const params: Record = {}; + const params: Record = { + page: pageParam + }; if (type !== 'trending') { params['watch_region'] = 'US'; @@ -48,11 +52,7 @@ const trendingQuery = ({ type, enabled }: TrendingQueryProps) => ({ .get>(type === 'trending' ? '/trending/movie/day' : '/discover/movie', { params }) - .then((response) => { - const data = mapMoviesSeriesResponseToShows(response); - - return data.results; - }); + .then((response) => mapMoviesSeriesResponseToShows(response)); } }); diff --git a/lib/analytics/events.ts b/lib/analytics/events.ts index b54c7ef..4deac73 100644 --- a/lib/analytics/events.ts +++ b/lib/analytics/events.ts @@ -1,6 +1,15 @@ type ShowRef = { showId: Show['id']; showType: Show['type']; showTitle: string }; -export type ListName = 'popular' | 'top' | 'search' | 'favorite' | 'watchlist' | 'random'; +export type ListName = + | 'popular' + | 'top' + | 'search' + | 'favorite' + | 'watchlist' + | 'random' + | 'trending' + | 'similar' + | 'recommendations'; export type AnalyticsEvents = { show_viewed: ShowRef & { surface: 'page' | 'modal' }; diff --git a/lib/showType.ts b/lib/showType.ts index c1b6201..d4a3651 100644 --- a/lib/showType.ts +++ b/lib/showType.ts @@ -1,2 +1,12 @@ -export const parseShowType = (value: string | string[] | null | undefined): Show['type'] => - value === 'tv' ? 'tv' : 'movie'; +export const SERIES_PREFIX = '/series'; + +export const isSeriesPath = (pathname: string) => + pathname === SERIES_PREFIX || pathname.startsWith(`${SERIES_PREFIX}/`); + +export const showTypeFromPath = (pathname: string): Show['type'] => (isSeriesPath(pathname) ? 'tv' : 'movie'); + +export const toShowTypePath = (pathname: string, type: Show['type']) => { + const base = isSeriesPath(pathname) ? pathname.slice(SERIES_PREFIX.length) || '/' : pathname; + + return type === 'tv' ? (base === '/' ? SERIES_PREFIX : `${SERIES_PREFIX}${base}`) : base; +};