From 0824090c6d76553ec449c33880071cb03294aed5 Mon Sep 17 00:00:00 2001 From: ner Date: Thu, 24 Sep 2026 15:40:01 +0300 Subject: [PATCH] feat(show-type): global movie/series toggle, type-aware pages and search, ?type=tv in URL --- .../components/UsersLists/index.tsx | 41 ++----- app/(site)/(root)/container.tsx | 42 +++++-- app/(site)/(root)/page.tsx | 18 ++- app/(site)/(root)/useRandomMovie.ts | 26 +++-- app/(site)/popular/page.tsx | 55 +++++---- app/(site)/popular/usePopular.ts | 9 +- app/(site)/top/page.tsx | 55 +++++---- app/(site)/top/useTop.ts | 14 ++- components/Header/components/Brand/index.tsx | 9 ++ .../Header/components/Navigation/index.tsx | 13 ++- components/Header/components/Search/index.tsx | 53 ++------- components/Header/index.tsx | 8 +- components/ShowTypeToggle/index.tsx | 110 ++++++++++++++++++ components/Trending/constants.ts | 23 ++++ components/Trending/index.tsx | 27 ++++- components/ui/hint.tsx | 2 +- data/queries/popular.ts | 9 +- data/queries/random.ts | 50 +++++--- data/queries/trending.ts | 22 +++- lib/analytics/events.ts | 1 + lib/showType.ts | 2 + 21 files changed, 415 insertions(+), 174 deletions(-) create mode 100644 components/Header/components/Brand/index.tsx create mode 100644 components/ShowTypeToggle/index.tsx create mode 100644 lib/showType.ts diff --git a/app/(site)/(favorites)/components/UsersLists/index.tsx b/app/(site)/(favorites)/components/UsersLists/index.tsx index af3a6b5..ee1e234 100644 --- a/app/(site)/(favorites)/components/UsersLists/index.tsx +++ b/app/(site)/(favorites)/components/UsersLists/index.tsx @@ -1,9 +1,10 @@ 'use client'; import { FC } from 'react'; +import { useAtomValue } from 'jotai'; import List from '@/components/List'; -import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; +import { showTypeAtom } from '@/components/ShowTypeToggle'; import useUsersShows from '@/hooks/useUsersShows'; interface Props { @@ -13,36 +14,18 @@ interface Props { } const UsersList: FC = ({ list, label, session }) => { - const movies = useUsersShows({ list, showType: 'movie', session, mode: 'default' }); - const series = useUsersShows({ list, showType: 'tv', session, mode: 'default' }); + const showType = useAtomValue(showTypeAtom); + const { shows, fetchNextPage, isFetched } = useUsersShows({ list, showType, session, mode: 'default' }); - const tabs = [ - { query: movies, title: 'Movies', key: 'movie' as const }, - { query: series, title: 'Series', key: 'tv' as const } - ]; + if (isFetched && !shows.length) { + return ( +
+ There are no {showType === 'tv' ? 'series' : 'movies'} in your {label} +
+ ); + } - return ( - - - {tabs.map(({ title, key }) => ( - - {title} - - ))} - - {tabs.map(({ query, key, title }) => ( - - {query.isFetched && !query.shows.length ? ( -
- There are no {title.toLowerCase()} in your {label} -
- ) : ( - - )} -
- ))} -
- ); + return ; }; export default UsersList; diff --git a/app/(site)/(root)/container.tsx b/app/(site)/(root)/container.tsx index 28a0542..bf5e127 100644 --- a/app/(site)/(root)/container.tsx +++ b/app/(site)/(root)/container.tsx @@ -1,32 +1,56 @@ 'use client'; -import { FC } from 'react'; +import { FC, useRef } from 'react'; +import { useAtom } from 'jotai'; 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'; interface Props { page: number; + type: Show['type']; } -const Container: FC = ({ page }) => { - const { movie, movies, onIndexChange, fetchNextPage } = useRandomMovie({ page }); +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 + }); return (
- } - /> + {show ? ( + + } + /> + ) : ( + + )}
- +
diff --git a/app/(site)/(root)/page.tsx b/app/(site)/(root)/page.tsx index 1d802a8..a2196b7 100644 --- a/app/(site)/(root)/page.tsx +++ b/app/(site)/(root)/page.tsx @@ -6,30 +6,36 @@ 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'; +interface SearchParams { + type?: string; +} + export const metadata: Metadata = { alternates: { canonical: '/' } }; -const Random = async () => { +const Random = async ({ searchParams }: { searchParams: SearchParams }) => { const queryClient = getQueryClient(); - const page = generatePage(); + const type = parseShowType(searchParams.type); + const page = generatePage(type); - await queryClient.prefetchInfiniteQuery(randomQuery({ page })); + await queryClient.prefetchInfiniteQuery(randomQuery({ page, type })); return (

{TITLE} — Discover Your Next Favorite Movie

- +

Discover your next favorite movie

- {TITLE} helps you decide what to watch: spin through random movie recommendations right on this page, browse - the{' '} + {TITLE} helps you decide what to watch: spin through random movie and series recommendations right on this + page, browse the{' '} most popular movies {' '} diff --git a/app/(site)/(root)/useRandomMovie.ts b/app/(site)/(root)/useRandomMovie.ts index 3ba097d..eb24e73 100644 --- a/app/(site)/(root)/useRandomMovie.ts +++ b/app/(site)/(root)/useRandomMovie.ts @@ -1,6 +1,6 @@ 'use client'; -import { startTransition, useCallback, useRef } from 'react'; +import { startTransition, useCallback, useEffect, useRef } from 'react'; import { atom, getDefaultStore, useAtom } from 'jotai'; import randomQuery from '@/data/queries/random'; @@ -13,32 +13,38 @@ export const useSilentIndex = () => getDefaultStore().get(indexAtom); interface Props { page: number; + type: Show['type']; } -const useRandomMovie = ({ page }: Props) => { +const useRandomMovie = ({ page, type }: Props) => { const [index, setIndex] = useAtom(indexAtom); - const { shows: movies, fetchNextPage } = useInfiniteList({ - ...randomQuery({ page }), + useEffect(() => { + setIndex(0); + }, [type, setIndex]); + + const { shows, fetchNextPage, isLoading } = useInfiniteList({ + ...randomQuery({ page, type }), mode: 'default', list: 'random' }); - const moviesRef = useRef(movies); + const showsRef = useRef(shows); - moviesRef.current = movies; + showsRef.current = shows; - if (movies.length && !movies[index]) { + if (shows.length && !shows[index]) { setIndex(0); } return { - movie: movies[index], - movies, + show: shows[index], + shows, + isLoading, fetchNextPage, onIndexChange: useCallback( (index: number) => { - analytics.randomShuffled({ index, showTitle: moviesRef.current[index]?.title ?? '' }); + analytics.randomShuffled({ index, showTitle: showsRef.current[index]?.title ?? '' }); startTransition(() => setIndex(index)); }, [setIndex] diff --git a/app/(site)/popular/page.tsx b/app/(site)/popular/page.tsx index 0d1dd29..aa4b84e 100644 --- a/app/(site)/popular/page.tsx +++ b/app/(site)/popular/page.tsx @@ -6,34 +6,49 @@ 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'; -const HEADING = 'Popular Movies'; +interface SearchParams { + type?: string; +} -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.`; +const heading = (type: Show['type']) => (type === 'tv' ? 'Popular Series' : 'Popular Movies'); -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 - } +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 () => { +const Popular = async ({ searchParams }: { searchParams: SearchParams }) => { const queryClient = getQueryClient(); - const query = popularQuery(); + const type = parseShowType(searchParams.type); + const query = popularQuery({ type }); await queryClient.prefetchInfiniteQuery(query); @@ -44,7 +59,7 @@ const Popular = async () => { return ( {Boolean(shows?.length) && } -

{HEADING}

+

{heading(type)}

); diff --git a/app/(site)/popular/usePopular.ts b/app/(site)/popular/usePopular.ts index 64b7e55..adda42f 100644 --- a/app/(site)/popular/usePopular.ts +++ b/app/(site)/popular/usePopular.ts @@ -1,8 +1,15 @@ 'use client'; +import { useAtomValue } from 'jotai'; + +import { showTypeAtom } from '@/components/ShowTypeToggle'; import popularQuery from '@/data/queries/popular'; import useInfiniteList from '@/hooks/useInfiniteList'; -const usePopular = () => useInfiniteList({ ...popularQuery(), mode: 'default', list: 'popular' }); +const usePopular = () => { + const showType = useAtomValue(showTypeAtom); + + return useInfiniteList({ ...popularQuery({ type: showType }), mode: 'default', list: 'popular' }); +}; export default usePopular; diff --git a/app/(site)/top/page.tsx b/app/(site)/top/page.tsx index 7d2af21..18f9383 100644 --- a/app/(site)/top/page.tsx +++ b/app/(site)/top/page.tsx @@ -6,34 +6,49 @@ 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'; -const HEADING = 'Top Rated Movies'; +interface SearchParams { + type?: string; +} -const DESCRIPTION = `Discover the top-rated movies on ${TITLE}. Find the highest-rated films and make informed viewing choices.`; +const heading = (type: Show['type']) => (type === 'tv' ? 'Top Rated Series' : 'Top Rated Movies'); -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 - } +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 () => { +const Top = async ({ searchParams }: { searchParams: SearchParams }) => { const queryClient = getQueryClient(); - const query = popularQuery({ sortBy: 'vote_average.desc' }); + const type = parseShowType(searchParams.type); + const query = popularQuery({ sortBy: 'vote_average.desc', type }); await queryClient.prefetchInfiniteQuery(query); @@ -44,7 +59,7 @@ const Top = async () => { return ( {Boolean(shows?.length) && } -

{HEADING}

+

{heading(type)}

); diff --git a/app/(site)/top/useTop.ts b/app/(site)/top/useTop.ts index 9f28160..38764b8 100644 --- a/app/(site)/top/useTop.ts +++ b/app/(site)/top/useTop.ts @@ -1,9 +1,19 @@ 'use client'; +import { useAtomValue } from 'jotai'; + +import { showTypeAtom } from '@/components/ShowTypeToggle'; import popularQuery from '@/data/queries/popular'; import useInfiniteList from '@/hooks/useInfiniteList'; -const useTop = () => - useInfiniteList({ ...popularQuery({ sortBy: 'vote_average.desc' }), mode: 'default', list: 'top' }); +const useTop = () => { + const showType = useAtomValue(showTypeAtom); + + return useInfiniteList({ + ...popularQuery({ sortBy: 'vote_average.desc', type: showType }), + mode: 'default', + list: 'top' + }); +}; export default useTop; diff --git a/components/Header/components/Brand/index.tsx b/components/Header/components/Brand/index.tsx new file mode 100644 index 0000000..555a867 --- /dev/null +++ b/components/Header/components/Brand/index.tsx @@ -0,0 +1,9 @@ +'use client'; + +import { FC } from 'react'; + +import ShowTypeToggle from '@/components/ShowTypeToggle'; + +const Brand: FC = () => ; + +export default Brand; diff --git a/components/Header/components/Navigation/index.tsx b/components/Header/components/Navigation/index.tsx index 60e8d0a..89f6535 100644 --- a/components/Header/components/Navigation/index.tsx +++ b/components/Header/components/Navigation/index.tsx @@ -1,10 +1,12 @@ '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'; @@ -12,8 +14,14 @@ 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); return (
@@ -26,7 +34,7 @@ export const MobileNavigation = () => { {paths.map(({ path, title }) => ( - +
- {isFetchedRef.current && ( + {isFetchedRef.current && search.shows.length > 0 && (
- {tabs.length > 1 ? ( - - - {tabs.map(({ type, title }) => ( - - {title} - - ))} - - {tabs.map(({ data: { shows, fetchNextPage }, type }) => ( - - - - ))} - - ) : ( - tabs.map(({ data: { shows, fetchNextPage }, type }) => ( - - )) - )} +
)} diff --git a/components/Header/index.tsx b/components/Header/index.tsx index 6b10e8a..306b700 100644 --- a/components/Header/index.tsx +++ b/components/Header/index.tsx @@ -1,10 +1,8 @@ import { FC } from 'react'; -import Link from 'next/link'; -import { Badge } from '@/components/ui/badge'; -import { TITLE } from '@/env'; import { cn } from '@/lib/utils'; +import Brand from './components/Brand'; import { DesktopNavigation, MobileNavigation } from './components/Navigation'; import ToggleSearch from './components/Search'; import ToggleTheme from './components/ThemeToggle'; @@ -23,9 +21,7 @@ const Header: FC = ({ className }) => ( >
- - {TITLE} - +
diff --git a/components/ShowTypeToggle/index.tsx b/components/ShowTypeToggle/index.tsx new file mode 100644 index 0000000..032b8d9 --- /dev/null +++ b/components/ShowTypeToggle/index.tsx @@ -0,0 +1,110 @@ +'use client'; + +import { FC, useEffect } from 'react'; +import * as DropdownMenuPrimitive from '@radix-ui/react-dropdown-menu'; +import { atom, useAtom } from 'jotai'; +import { Check, ChevronDown, Clapperboard, LucideIcon, Tv } from 'lucide-react'; +import Link from 'next/link'; + +import analytics from '@/lib/analytics'; +import { parseShowType } from '@/lib/showType'; +import { cn } from '@/lib/utils'; + +export const showTypeAtom = atom('movie'); + +interface Option { + value: Show['type']; + label: string; + icon: LucideIcon; +} + +const options: Option[] = [ + { value: 'movie', label: 'Movie', icon: Clapperboard }, + { value: 'tv', label: 'Series', icon: Tv } +]; + +interface Props { + className?: string; +} + +const ShowTypeToggle: FC = ({ className }) => { + const [value, setValue] = useAtom(showTypeAtom); + + useEffect(() => { + const urlType = parseShowType(new URLSearchParams(window.location.search).get('type')); + + if (urlType !== value) setValue(urlType); + }, [value, setValue]); + + const current = options.find(({ value: optionValue }) => optionValue === value) ?? options[0]; + + const handleChange = (next: Show['type']) => { + analytics.randomTypeChanged({ type: next }); + setValue(next); + + const url = new URL(window.location.href); + if (next === 'tv') url.searchParams.set('type', 'tv'); + else url.searchParams.delete('type'); + window.history.replaceState(null, '', url); + }; + + return ( + + + + event.stopPropagation()} + onClick={(event) => event.stopPropagation()} + className='pl-2.5 pr-1.5 text-[15px] font-normal leading-[18px]' + > + Watch Next + + + + {current.label} + + + + + + + handleChange(next as Show['type'])}> + {options.map(({ value: optionValue, label, icon: Icon }) => ( + + + {label} + + + + + ))} + + + + + ); +}; + +export default ShowTypeToggle; diff --git a/components/Trending/constants.ts b/components/Trending/constants.ts index e4e5071..2262dd6 100644 --- a/components/Trending/constants.ts +++ b/components/Trending/constants.ts @@ -20,3 +20,26 @@ export const tabs = [ type: 'recommendations' } ]; + +export const seriesTabs = [ + { + title: 'Apple', + type: 'apple' + }, + { + title: 'Netflix', + type: 'netflix' + }, + { + title: 'HBO', + type: 'hbo' + }, + { + title: 'Similar', + type: 'similar' + }, + { + title: 'Recommendations', + type: 'recommendations' + } +]; diff --git a/components/Trending/index.tsx b/components/Trending/index.tsx index c4f30e6..acbb53d 100644 --- a/components/Trending/index.tsx +++ b/components/Trending/index.tsx @@ -1,13 +1,13 @@ 'use client'; -import { FC, useState } from 'react'; +import { FC, useEffect, useMemo, useState } from 'react'; import useSimilar from '@/components/Trending/useSimilar'; import Nav from '@/components/ui/nav'; import analytics from '@/lib/analytics'; import Gallery from './components/Gallery'; -import { tabs as initialTabs } from './constants'; +import { seriesTabs, tabs as movieTabs } from './constants'; import useTrending from './useTrending'; interface Props { @@ -17,13 +17,28 @@ interface Props { } const Trending: FC = ({ showId, showType, preview }) => { - const tabs = preview ? initialTabs.filter(({ type }) => ['similar', 'recommendations'].includes(type)) : initialTabs; + const isSeries = showType === 'tv'; + + const tabs = useMemo( + () => + (isSeries ? seriesTabs : movieTabs).filter(({ type }) => + preview ? ['similar', 'recommendations'].includes(type) : true + ), + [isSeries, preview] + ); const [active, setActive] = useState(tabs[0]); - const streaming = useTrending({ type: 'streaming', enabled: !preview }); + useEffect(() => { + setActive(tabs[0]); + }, [tabs]); + + const streaming = useTrending({ type: 'streaming', enabled: !preview && !isSeries }); const trending = useTrending({ type: 'trending', enabled: active.type === 'trending' }); const theater = useTrending({ type: 'theater', enabled: active.type === 'theater' }); + const apple = useTrending({ type: 'apple', enabled: isSeries && !preview }); + const netflix = useTrending({ type: 'netflix', enabled: isSeries && active.type === 'netflix' }); + const hbo = useTrending({ type: 'hbo', enabled: isSeries && active.type === 'hbo' }); const similar = useSimilar({ showId, showType, @@ -37,10 +52,10 @@ const Trending: FC = ({ showId, showType, preview }) => { enabled: active.type === 'recommendations' }); - const tab = { trending, streaming, theater, similar, recommendations }[active.type]!; + const tab = { streaming, trending, theater, apple, netflix, hbo, similar, recommendations }[active.type]!; return ( - (streaming.isFetched || similar.isFetched) && ( + (streaming.isFetched || apple.isFetched || similar.isFetched) && (