diff --git a/api/index.ts b/api/index.ts index 2f54618..10bc2f3 100644 --- a/api/index.ts +++ b/api/index.ts @@ -30,15 +30,44 @@ export const account = async () => { }); }; +export const accountStates = async ({ showId, showType }: { showId: Show['id']; showType: Show['type'] }) => { + return http.get(`/${showType}/${showId}/account_states`, { + params: { session_id: cookies().get('session')?.value }, + preventCache: true + }); +}; + +export const updateAccountStates = async ({ + showId, + showType, + list, + value +}: { + showId: Show['id']; + showType: Show['type']; + list: 'favorite' | 'watchlist'; + value: boolean; +}) => { + http.post( + `/account/null/${list}`, + { params: { session_id: cookies().get('session')?.value }, preventCache: true }, + { + media_id: showId, + media_type: showType, + [list]: value + } + ); +}; + export const usersShows = async ({ showType, - listType + list }: { showType: Show['type']; - listType: 'favorites' | 'watchlist'; + list: 'favorite' | 'watchlist'; }): Promise> => { return http - .get>(`/account/null/${listType}/${showType === 'tv' ? 'tv' : 'movies'}`, { + .get>(`/account/null/${list}/${showType === 'tv' ? 'tv' : 'movies'}`, { params: { session_id: cookies().get('session')?.value }, preventCache: true }) diff --git a/app/(site)/favorites/container.tsx b/app/(site)/favorites/container.tsx index 02f562b..b966ff2 100644 --- a/app/(site)/favorites/container.tsx +++ b/app/(site)/favorites/container.tsx @@ -1,22 +1,7 @@ 'use client'; -import List from '@/components/List'; -import useMounted from '@/hooks/useMounted'; +import UsersList from '@/components/UsersLists'; -import useFavorites from './useFavorites'; +const Container = () => ; -export default function Container() { - const { shows } = useFavorites(); - const isMounted = useMounted(); - - if (!isMounted) return null; - - if (!shows.length) - return ( -
- There are no items in your favorites list -
- ); - - return ; -} +export default Container; diff --git a/app/(site)/favorites/page.tsx b/app/(site)/favorites/page.tsx index a1b475c..1c71ccd 100644 --- a/app/(site)/favorites/page.tsx +++ b/app/(site)/favorites/page.tsx @@ -1,5 +1,9 @@ +import { dehydrate, HydrationBoundary } from '@tanstack/react-query'; import { Metadata } from 'next'; +import { usersShows } from '@/api'; +import getQueryClient from '@/lib/queryClient'; + import Container from './container'; export const metadata: Metadata = { @@ -8,8 +12,23 @@ export const metadata: Metadata = { 'View your favorite shows on Movie Advisor. Keep track of films you’ve added and revisit your top picks anytime.' }; -const Favorites = () => { - return ; +const Favorites = async () => { + const queryClient = getQueryClient(); + + const list = 'favorite'; + const showType = 'movie'; + + await queryClient.prefetchInfiniteQuery({ + queryKey: [`users-${list}`, showType], + queryFn: () => usersShows({ showType, list }), + initialPageParam: '1' + }); + + return ( + + + + ); }; export default Favorites; diff --git a/app/(site)/favorites/useFavorites.ts b/app/(site)/favorites/useFavorites.ts deleted file mode 100644 index 28a6c7c..0000000 --- a/app/(site)/favorites/useFavorites.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { atom, useAtomValue, useSetAtom } from 'jotai'; -import { atomWithStorage } from 'jotai/utils'; - -const favoritesAtom = atomWithStorage('favorite', [], undefined, { - unstable_getOnInit: true -}); -const favoritesMapAtom = atom((get) => { - const map = new Map(); - const favorites = get(favoritesAtom); - - favorites.forEach((show) => map.set(show.id, show)); - - return map as Map; -}); - -const useFavorites = () => { - return { - shows: useAtomValue(favoritesAtom), - map: useAtomValue(favoritesMapAtom) - }; -}; - -export const useFavoritesMutation = () => { - const setFavorites = useSetAtom(favoritesAtom); - - const add = (show: Show) => setFavorites((favorites) => [show, ...favorites]); - const remove = (showId: number) => setFavorites((favorites) => favorites.filter(({ id }) => id !== showId)); - - return { - add, - remove - }; -}; - -export const useFavoriteToggle = (show: Show) => { - const { add, remove } = useFavoritesMutation(); - const { map } = useFavorites(); - - const isFavorite = map.has(show.id); - - const toggle = () => (isFavorite ? remove(show.id) : add(show)); - - return { isFavorite, toggle }; -}; - -export default useFavorites; diff --git a/app/(site)/watch-list/container.tsx b/app/(site)/watch-list/container.tsx new file mode 100644 index 0000000..eb063a5 --- /dev/null +++ b/app/(site)/watch-list/container.tsx @@ -0,0 +1,7 @@ +'use client'; + +import UsersList from '@/components/UsersLists'; + +const Container = () => ; + +export default Container; diff --git a/app/(site)/watch-list/page.tsx b/app/(site)/watch-list/page.tsx new file mode 100644 index 0000000..56f672a --- /dev/null +++ b/app/(site)/watch-list/page.tsx @@ -0,0 +1,34 @@ +import { dehydrate, HydrationBoundary } from '@tanstack/react-query'; +import { Metadata } from 'next'; + +import { usersShows } from '@/api'; +import getQueryClient from '@/lib/queryClient'; + +import Container from './container'; + +export const metadata: Metadata = { + title: 'My watch list - Movie Advisor', + description: + 'View your favorite shows on Movie Advisor. Keep track of films you’ve added and revisit your top picks anytime.' +}; + +const WatchList = async () => { + const queryClient = getQueryClient(); + + const list = 'watchlist'; + const showType = 'movie'; + + await queryClient.prefetchInfiniteQuery({ + queryKey: [`users-${list}`, showType], + queryFn: () => usersShows({ showType, list }), + initialPageParam: '1' + }); + + return ( + + + + ); +}; + +export default WatchList; diff --git a/app/layout.tsx b/app/layout.tsx index 9b8fef9..7af2a39 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -15,7 +15,6 @@ export const metadata: Metadata = { }; export const viewport: Viewport = { - themeColor: 'black', width: 'device-width', initialScale: 1, maximumScale: 1, @@ -30,7 +29,7 @@ const RootLayout = ({ children }: { children: ReactNode }) => { return ( - + diff --git a/components/Header/components/Navigation/constants.ts b/components/Header/components/Navigation/constants.ts index 6d1e09d..dfce09a 100644 --- a/components/Header/components/Navigation/constants.ts +++ b/components/Header/components/Navigation/constants.ts @@ -2,5 +2,6 @@ export const paths = [ { path: '/', title: 'Random' }, { path: '/popular', title: 'Popular' }, { path: '/top', title: 'Top rated' }, - { path: '/favorites', title: 'Favorites' } + { path: '/favorites', title: 'Favorites' }, + { path: '/watch-list', title: 'Watch list' } ]; diff --git a/components/Header/components/User/index.tsx b/components/Header/components/User/index.tsx index 712dab7..593f9d6 100644 --- a/components/Header/components/User/index.tsx +++ b/components/Header/components/User/index.tsx @@ -1,6 +1,6 @@ 'use client'; -import { LogIn,LogOut, User as UserIcon, UserRound } from 'lucide-react'; +import { LogIn, LogOut, User as UserIcon, UserRound } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { @@ -11,8 +11,8 @@ import { DropdownMenuSeparator, DropdownMenuTrigger } from '@/components/ui/dropdown-menu'; -import useAccount from '@/hooks/useAccount'; +import useAccount from './useAccount'; import useAuth from './useAuth'; import useLogOut from './useLogOut'; diff --git a/hooks/useAccount.ts b/components/Header/components/User/useAccount.ts similarity index 92% rename from hooks/useAccount.ts rename to components/Header/components/User/useAccount.ts index 0a4e5f8..97514fd 100644 --- a/hooks/useAccount.ts +++ b/components/Header/components/User/useAccount.ts @@ -1,6 +1,7 @@ -import { account } from '@/api'; import { useQuery, useQueryClient } from '@tanstack/react-query'; -import { useSession } from './useSession'; + +import { account } from '@/api'; +import { useSession } from '@/hooks/useSession'; const KEY = ['account']; diff --git a/components/Header/index.tsx b/components/Header/index.tsx index 9ff865a..ed702f2 100644 --- a/components/Header/index.tsx +++ b/components/Header/index.tsx @@ -16,7 +16,7 @@ interface Props { const Header: FC = ({ className }) => (
diff --git a/components/List/index.tsx b/components/List/index.tsx index 3554238..4d3076c 100644 --- a/components/List/index.tsx +++ b/components/List/index.tsx @@ -1,8 +1,8 @@ import { FC } from 'react'; import { VirtuosoGrid, VirtuosoGridProps } from 'react-virtuoso'; +import Poster from '@/components/Poster'; import { showPreviewModal } from '@/components/Preview'; -import Card from '@/components/Show'; interface Props { shows: Show[]; @@ -18,7 +18,7 @@ const components: VirtuosoGridProps onClick?.(show)} />; + return onClick?.(show)} />; } }; diff --git a/components/Poster/components/Handlers/index.tsx b/components/Poster/components/Handlers/index.tsx new file mode 100644 index 0000000..241fbf3 --- /dev/null +++ b/components/Poster/components/Handlers/index.tsx @@ -0,0 +1,24 @@ +'use client'; + +import { FC } from 'react'; + +import { useMutateShowState } from '@/hooks/useShowState'; + +interface Props { + show: Show; + onClick?: () => void; +} + +const Handlers: FC = ({ onClick, show }) => { + const { mutate } = useMutateShowState(show); + + return ( +
mutate({ list: 'favorite', value: true })} + className='absolute left-0 top-0 size-full' + /> + ); +}; + +export default Handlers; diff --git a/components/Show/components/Trailer/index.tsx b/components/Poster/components/Trailer/index.tsx similarity index 100% rename from components/Show/components/Trailer/index.tsx rename to components/Poster/components/Trailer/index.tsx diff --git a/components/Show/components/Trailer/useTrailer.ts b/components/Poster/components/Trailer/useTrailer.ts similarity index 100% rename from components/Show/components/Trailer/useTrailer.ts rename to components/Poster/components/Trailer/useTrailer.ts diff --git a/components/Poster/index.tsx b/components/Poster/index.tsx new file mode 100644 index 0000000..78e697b --- /dev/null +++ b/components/Poster/index.tsx @@ -0,0 +1,42 @@ +import { DetailedHTMLProps, FC, HTMLAttributes } from 'react'; + +import { cn } from '@/lib/utils'; + +import Handlers from './components/Handlers'; + +interface Props { + show?: Show; + onClick?: () => void; + className?: string; + rounded?: string; + containerProps?: DetailedHTMLProps, HTMLDivElement>; +} + +const Poster: FC = ({ show, onClick, className, rounded, containerProps }) => { + if (!show) return null; + + return ( +
+
+ {show.title} +
+
+ +
+
+ ); +}; + +export default Poster; diff --git a/components/Preview/components/Actions.tsx b/components/Preview/components/Actions.tsx deleted file mode 100644 index 13f88c0..0000000 --- a/components/Preview/components/Actions.tsx +++ /dev/null @@ -1,46 +0,0 @@ -'use client'; - -import Link from 'next/link'; - -import showTorrentsModal from '@/components/Show/components/Torrents'; -import ToggleTrailer from '@/components/Show/components/Trailer'; -import { Button } from '@/components/ui/button'; - -interface Props { - show: Show & Partial
; - onClose?: () => void; -} - -const Actions = ({ onClose, show }: Props) => ( - <> - - {({ onPlay, disabled }) => ( - - )} - - - - - - -); - -export default Actions; diff --git a/components/Preview/components/Actions/index.tsx b/components/Preview/components/Actions/index.tsx new file mode 100644 index 0000000..7aadcbf --- /dev/null +++ b/components/Preview/components/Actions/index.tsx @@ -0,0 +1,84 @@ +'use client'; + +import { Bookmark, Copy, Heart, Play, Star } from 'lucide-react'; +import Link from 'next/link'; + +import showTorrentsModal from '@/components/Preview/components/Torrents'; +import { Button } from '@/components/ui/button'; +import useShowState, { useMutateShowState } from '@/hooks/useShowState'; +import { cn } from '@/lib/utils'; + +interface Props { + show: Show & Partial
; + className: string; + onClose?: () => void; +} + +const Actions = ({ onClose, show, className }: Props) => { + const state = useShowState({ showId: show.id, showType: show.type }); + const toggle = useMutateShowState(show); + + const groupClassName = 'flex bg-input gap-[1px] rounded-md md:flex-grow-0 flex-grow'; + const leftClassName = 'h-8 gap-1 flex-grow border-r-0 rounded-none rounded-l-md md:px-3 px-2'; + const rightClassName = 'h-8 gap-1 flex-grow border-l-0 rounded-none rounded-r-md md:px-3 px-2'; + + return ( +
+
+ + +
+
+ + +
+
+ + + + +
+
+ ); +}; + +export default Actions; diff --git a/components/Show/components/Credits/index.tsx b/components/Preview/components/Credits/index.tsx similarity index 100% rename from components/Show/components/Credits/index.tsx rename to components/Preview/components/Credits/index.tsx diff --git a/components/Show/components/Credits/useCredits.ts b/components/Preview/components/Credits/useCredits.ts similarity index 100% rename from components/Show/components/Credits/useCredits.ts rename to components/Preview/components/Credits/useCredits.ts diff --git a/components/Show/components/Torrents/components/HostManager/index.tsx b/components/Preview/components/Torrents/components/HostManager/index.tsx similarity index 91% rename from components/Show/components/Torrents/components/HostManager/index.tsx rename to components/Preview/components/Torrents/components/HostManager/index.tsx index bc5b739..d745355 100644 --- a/components/Show/components/Torrents/components/HostManager/index.tsx +++ b/components/Preview/components/Torrents/components/HostManager/index.tsx @@ -2,7 +2,8 @@ import { create, InstanceProps } from 'react-modal-promise'; import { Modal } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; -import { usePrefix, useSetPrefix } from '@/hooks/useMagnetHosts'; + +import { usePrefix, useSetPrefix } from '../../hooks/useMagnetHosts'; const showHostManagerModal = create>(({ onResolve, onReject }) => { const prefix = usePrefix(); diff --git a/components/Show/components/Torrents/components/Player/Player.tsx b/components/Preview/components/Torrents/components/Player/Player.tsx similarity index 100% rename from components/Show/components/Torrents/components/Player/Player.tsx rename to components/Preview/components/Torrents/components/Player/Player.tsx diff --git a/components/Show/components/Torrents/components/Player/components/CaptionsMenu/index.tsx b/components/Preview/components/Torrents/components/Player/components/CaptionsMenu/index.tsx similarity index 100% rename from components/Show/components/Torrents/components/Player/components/CaptionsMenu/index.tsx rename to components/Preview/components/Torrents/components/Player/components/CaptionsMenu/index.tsx diff --git a/components/Show/components/Torrents/components/Player/components/PlayerMenu/index.tsx b/components/Preview/components/Torrents/components/Player/components/PlayerMenu/index.tsx similarity index 100% rename from components/Show/components/Torrents/components/Player/components/PlayerMenu/index.tsx rename to components/Preview/components/Torrents/components/Player/components/PlayerMenu/index.tsx diff --git a/components/Show/components/Torrents/components/Player/components/VideosMenu/index.tsx b/components/Preview/components/Torrents/components/Player/components/VideosMenu/index.tsx similarity index 100% rename from components/Show/components/Torrents/components/Player/components/VideosMenu/index.tsx rename to components/Preview/components/Torrents/components/Player/components/VideosMenu/index.tsx diff --git a/components/Show/components/Torrents/components/Player/index.tsx b/components/Preview/components/Torrents/components/Player/index.tsx similarity index 95% rename from components/Show/components/Torrents/components/Player/index.tsx rename to components/Preview/components/Torrents/components/Player/index.tsx index 79317bb..18ae88d 100644 --- a/components/Show/components/Torrents/components/Player/index.tsx +++ b/components/Preview/components/Torrents/components/Player/index.tsx @@ -35,7 +35,7 @@ const showPlayer = create(({ onResolve, magnet, backdrop, title }: Props) => { } if (isFetched && !videos.length) { - toast({ description: 'This torrent doesn\'t contain any compatible video files' }); + toast({ description: 'This torrent doesn’t contain any compatible video files' }); onResolve(); } diff --git a/components/Show/components/Torrents/components/Player/useFiles.ts b/components/Preview/components/Torrents/components/Player/useFiles.ts similarity index 100% rename from components/Show/components/Torrents/components/Player/useFiles.ts rename to components/Preview/components/Torrents/components/Player/useFiles.ts diff --git a/components/Show/components/Torrents/components/TableHeadSortable/index.tsx b/components/Preview/components/Torrents/components/TableHeadSortable/index.tsx similarity index 100% rename from components/Show/components/Torrents/components/TableHeadSortable/index.tsx rename to components/Preview/components/Torrents/components/TableHeadSortable/index.tsx diff --git a/components/Show/components/Torrents/components/TorrentsTable/index.tsx b/components/Preview/components/Torrents/components/TorrentsTable/index.tsx similarity index 98% rename from components/Show/components/Torrents/components/TorrentsTable/index.tsx rename to components/Preview/components/Torrents/components/TorrentsTable/index.tsx index b1c798f..09a3153 100644 --- a/components/Show/components/Torrents/components/TorrentsTable/index.tsx +++ b/components/Preview/components/Torrents/components/TorrentsTable/index.tsx @@ -4,9 +4,9 @@ import { Cast, Copy, ListVideo, Magnet, Play } from 'lucide-react'; import { Sort } from '@/api/parsers'; import { Quality } from '@/api/parsers/yts/models'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table'; -import { useCastMagnet, usePrefix } from '@/hooks/useMagnetHosts'; import { providers } from '../../constants'; +import { useCastMagnet, usePrefix } from '../../hooks/useMagnetHosts'; import showHostManagerModal from '../HostManager'; import showPlayer from '../Player'; import TableHeadSortable from '../TableHeadSortable'; diff --git a/components/Show/components/Torrents/constants.ts b/components/Preview/components/Torrents/constants.ts similarity index 100% rename from components/Show/components/Torrents/constants.ts rename to components/Preview/components/Torrents/constants.ts diff --git a/hooks/useMagnetHosts.ts b/components/Preview/components/Torrents/hooks/useMagnetHosts.ts similarity index 89% rename from hooks/useMagnetHosts.ts rename to components/Preview/components/Torrents/hooks/useMagnetHosts.ts index c839e36..cd01f28 100644 --- a/hooks/useMagnetHosts.ts +++ b/components/Preview/components/Torrents/hooks/useMagnetHosts.ts @@ -2,7 +2,7 @@ import { useCallback } from 'react'; import { useAtomValue, useSetAtom } from 'jotai'; import { atomWithStorage } from 'jotai/utils'; -import showHostManagerModal from '@/components/Show/components/Torrents/components/HostManager'; +import showHostManagerModal from '@/components/Preview/components/Torrents/components/HostManager'; const magnetPrefixAtom = atomWithStorage('magnet-prefix', 'http://{host}:65220/playuri?uri=', undefined, { unstable_getOnInit: true diff --git a/components/Show/components/Torrents/index.tsx b/components/Preview/components/Torrents/index.tsx similarity index 98% rename from components/Show/components/Torrents/index.tsx rename to components/Preview/components/Torrents/index.tsx index 20611d3..5184e94 100644 --- a/components/Show/components/Torrents/index.tsx +++ b/components/Preview/components/Torrents/index.tsx @@ -60,7 +60,7 @@ const showTorrentsModal = create(({ title, year, imdbID, backdrop, onResolve }:
{title} -
diff --git a/components/Show/components/Torrents/useTorrents.ts b/components/Preview/components/Torrents/useTorrents.ts similarity index 100% rename from components/Show/components/Torrents/useTorrents.ts rename to components/Preview/components/Torrents/useTorrents.ts diff --git a/components/Preview/index.tsx b/components/Preview/index.tsx index dc0262f..6a2e6b6 100644 --- a/components/Preview/index.tsx +++ b/components/Preview/index.tsx @@ -3,11 +3,10 @@ import { FC, ReactNode } from 'react'; import { create, InstanceProps } from 'react-modal-promise'; +import Poster from '@/components/Poster'; +import Credits from '@/components/Preview/components/Credits'; import useDetails from '@/components/Preview/useDetails'; -import Card from '@/components/Show'; -import Credits from '@/components/Show/components/Credits'; import { Modal } from '@/components/ui/dialog'; -import { Separator } from '@/components/ui/separator'; import { cn } from '@/lib/utils'; import Actions from './components/Actions'; @@ -16,30 +15,47 @@ interface Props { show?: Show; className?: string; onClose?: () => void; - children?: ReactNode; - card?: ReactNode; + poster?: ReactNode; } -const Preview: FC = ({ show: baseShow, className, onClose, children, card }) => { +const Preview: FC = ({ show: baseShow, className, onClose, poster }) => { const { data: detailedShow } = useDetails({ showId: baseShow?.id, showType: baseShow?.type }); if (!baseShow) return null; const show: (Show & Partial
) | undefined = detailedShow || baseShow; + const isModal = Boolean(onClose); + return ( -
- {card || } -
- {children &&
{children}
} -
- +
+ {poster || } +
+ {show.title} + +
+ {new Date(show.release).getFullYear()} + {show.runtime && {show.runtime} minutes} + + {show.genres + ?.slice(0, 3) + .map(({ name }) => name) + .join(' | ')} +
- -

+ + + +

{show.overview}

-
+
@@ -48,10 +64,9 @@ const Preview: FC = ({ show: baseShow, className, onClose, children, card }; export const showPreviewModal = create(({ onResolve, onClose, show }: Props & InstanceProps) => ( - + { window.scrollTo({ top: 0, behavior: 'smooth' }); onClose?.(); diff --git a/components/Random/components/Carousel/index.tsx b/components/Random/components/Carousel/index.tsx index 79a9b7b..e9ebae6 100644 --- a/components/Random/components/Carousel/index.tsx +++ b/components/Random/components/Carousel/index.tsx @@ -3,8 +3,8 @@ import { FC, ForwardedRef, forwardRef, useLayoutEffect, useRef } from 'react'; import { Virtuoso, VirtuosoProps } from 'react-virtuoso'; +import Poster from '@/components/Poster'; import ScrollNavigation from '@/components/ScrollNavigation'; -import Card from '@/components/Show'; import { cn } from '@/lib/utils'; const components: VirtuosoProps void>['components'] = { @@ -36,7 +36,7 @@ const components: VirtuosoProps void>['components'] = { return (
- +
); }, diff --git a/components/Random/index.tsx b/components/Random/index.tsx index 8841887..b5be8be 100644 --- a/components/Random/index.tsx +++ b/components/Random/index.tsx @@ -12,7 +12,7 @@ const Random = () => { } + poster={} /> ); }; diff --git a/components/Show/components/Favorite/index.tsx b/components/Show/components/Favorite/index.tsx deleted file mode 100644 index fa44570..0000000 --- a/components/Show/components/Favorite/index.tsx +++ /dev/null @@ -1,29 +0,0 @@ -'use client'; - -import { FC } from 'react'; -import { Heart } from 'lucide-react'; - -import { useFavoriteToggle } from '@/app/(site)/favorites/useFavorites'; -import useMounted from '@/hooks/useMounted'; -import { cn } from '@/lib/utils'; - -interface Props { - show: Show; -} - -const Favorite: FC = ({ show }) => { - const isMounted = useMounted(); - const { isFavorite, toggle } = useFavoriteToggle(show); - - return ( - - - - ); -}; - -export default Favorite; diff --git a/components/Show/components/Handlers/index.tsx b/components/Show/components/Handlers/index.tsx deleted file mode 100644 index 6e9a451..0000000 --- a/components/Show/components/Handlers/index.tsx +++ /dev/null @@ -1,18 +0,0 @@ -'use client'; - -import { FC } from 'react'; - -import { useFavoriteToggle } from '@/app/(site)/favorites/useFavorites'; - -interface Props { - show: Show; - onClick?: () => void; -} - -const Handlers: FC = ({ onClick, show }) => { - const { toggle } = useFavoriteToggle(show); - - return
; -}; - -export default Handlers; diff --git a/components/Show/components/Rating/index.tsx b/components/Show/components/Rating/index.tsx deleted file mode 100644 index a577825..0000000 --- a/components/Show/components/Rating/index.tsx +++ /dev/null @@ -1,15 +0,0 @@ -import { FC } from 'react'; -import { Star } from 'lucide-react'; - -interface Props { - rating?: number; -} - -const Rating: FC = ({ rating }) => ( - - -

{rating?.toFixed(1)}

-
-); - -export default Rating; diff --git a/components/Show/index.tsx b/components/Show/index.tsx deleted file mode 100644 index f220654..0000000 --- a/components/Show/index.tsx +++ /dev/null @@ -1,48 +0,0 @@ -import { DetailedHTMLProps, FC, HTMLAttributes } from 'react'; -import Image from 'next/image'; - -import Rating from '@/components/Show/components/Rating'; -import { cn } from '@/lib/utils'; - -import Favorite from './components/Favorite'; -import Handlers from './components/Handlers'; - -interface Props { - show?: Show; - onClick?: () => void; - className?: string; - containerProps?: DetailedHTMLProps, HTMLDivElement>; -} - -const Show: FC = ({ show, onClick, className, containerProps }) => { - if (!show) return null; - - return ( -
-
- {show.title} -
-
- -
- {show.title} - {Boolean(show.release) && {new Date(show.release).getFullYear()}} -
-
- - - - -
-
-
- ); -}; - -export default Show; diff --git a/components/Trending/components/Gallery/index.tsx b/components/Trending/components/Gallery/index.tsx index 3c182ac..b762ed6 100644 --- a/components/Trending/components/Gallery/index.tsx +++ b/components/Trending/components/Gallery/index.tsx @@ -1,8 +1,8 @@ import { FC } from 'react'; +import Poster from '@/components/Poster'; import { showPreviewModal } from '@/components/Preview'; import ScrollNavigation from '@/components/ScrollNavigation'; -import Card from '@/components/Show'; interface Props { shows: Show[]; @@ -17,7 +17,7 @@ const Gallery: FC = ({ shows }) => ( > {shows.map((show) => (
  • - showPreviewModal({ show })} /> + showPreviewModal({ show })} />
  • ))} diff --git a/components/Trending/constants.ts b/components/Trending/constants.ts index 6fea173..ed9f981 100644 --- a/components/Trending/constants.ts +++ b/components/Trending/constants.ts @@ -1,12 +1,12 @@ export const tabs = [ - { - title: 'Trending', - type: 'trending' - }, { title: 'Streaming', type: 'streaming' }, + { + title: 'Trending', + type: 'trending' + }, { title: 'In theaters', type: 'theater' diff --git a/components/Trending/index.tsx b/components/Trending/index.tsx index d6247a8..225e127 100644 --- a/components/Trending/index.tsx +++ b/components/Trending/index.tsx @@ -11,8 +11,8 @@ import useTrending from './useTrending'; const Trending: FC = () => { const [active, setActive] = useState(tabs[0]); - const { data: trending, isFetched } = useTrending({ type: 'trending' }); - const { data: streaming } = useTrending({ type: 'streaming' }); + const { data: streaming, isFetched } = useTrending({ type: 'streaming' }); + const { data: trending } = useTrending({ type: 'trending' }); const { data: theater } = useTrending({ type: 'theater' }); const shows = { trending, streaming, theater }[active.type] || []; diff --git a/components/UsersLists/index.tsx b/components/UsersLists/index.tsx new file mode 100644 index 0000000..bf4cc26 --- /dev/null +++ b/components/UsersLists/index.tsx @@ -0,0 +1,53 @@ +'use client'; + +import { FC } from 'react'; + +import List from '@/components/List'; +import useMounted from '@/hooks/useMounted'; +import useUsersShows from '@/hooks/useUsersShows'; + +import { Tabs, TabsContent, TabsList, TabsTrigger } from '../ui/tabs'; + +interface Props { + list: 'favorite' | 'watchlist'; + label: string; +} + +const UsersList: FC = ({ list, label }) => { + const isMounted = useMounted(); + + const movies = useUsersShows({ list, showType: 'movie' }); + const series = useUsersShows({ list, showType: 'tv' }); + + const tabs = [ + { query: movies, title: 'Movies', key: 'movie' as const }, + { query: series, title: 'Series', key: 'tv' as const } + ]; + + if (!isMounted) return null; + + return ( + + + {tabs.map(({ title, key }) => ( + + {title} + + ))} + + {tabs.map(({ query, key, title }) => ( + + {query.isFetched && !query.shows.length ? ( +
    + There are no {title.toLowerCase()} in your {label} list +
    + ) : ( + + )} +
    + ))} +
    + ); +}; + +export default UsersList; diff --git a/hooks/useFavorites.ts b/hooks/useFavorites.ts deleted file mode 100644 index 13250e7..0000000 --- a/hooks/useFavorites.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { usersShows } from '@/api'; -import useInfiniteList from './useInfiniteList'; -import { useSession } from './useSession'; - -const useFavorites = ({ - showType, - listType = 'favorites' -}: { - showType: Show['type']; - listType: 'favorites' | 'watchlist'; -}) => { - const session = useSession(); - - return useInfiniteList({ - queryKey: [`users-${listType}`, showType], - queryFn: () => usersShows({ showType, listType }), - enabled: Boolean(session) - }); -}; - -export default useFavorites; diff --git a/hooks/useLocalUsersLists.ts b/hooks/useLocalUsersLists.ts new file mode 100644 index 0000000..685e5f4 --- /dev/null +++ b/hooks/useLocalUsersLists.ts @@ -0,0 +1,78 @@ +import { Atom, atom, PrimitiveAtom, useAtomValue, useSetAtom } from 'jotai'; +import { atomWithStorage } from 'jotai/utils'; +import { useCallback } from 'react'; + +type MapShow = Map; + +const createStorage = (key: string) => { + const listAtom = atomWithStorage(key, [], undefined, { + unstable_getOnInit: true + }); + + const mapAtom = atom((get) => { + const map = new Map(); + const list = get(listAtom); + + list.forEach((show) => map.set(show.id, show)); + + return map; + }); + + return { listAtom, mapAtom }; +}; + +const createUseShows = + ({ listAtom, mapAtom }: { listAtom: PrimitiveAtom; mapAtom: Atom }) => + () => ({ + shows: useAtomValue(listAtom), + map: useAtomValue(mapAtom) + }); + +const createUseShowsMutation = + ({ listAtom }: { listAtom: PrimitiveAtom }) => + () => { + const setShows = useSetAtom(listAtom); + + const add = (show: Show) => setShows((shows) => [show, ...shows]); + const remove = (showId: number) => setShows((shows) => shows.filter(({ id }) => id !== showId)); + + return { + add, + remove + }; + }; + +const createUseStateToggle = + ({ + useShows, + useShowsMutation + }: { + useShows: ReturnType; + useShowsMutation: ReturnType; + }) => + (show: Show) => { + const { add, remove } = useShowsMutation(); + const { map } = useShows(); + + const active = map.has(show.id); + + const toggle = useCallback(() => (active ? remove(show.id) : add(show)), [add, remove, show]); + + return { active, toggle }; + }; + +const createStorageHooks = (key: string) => { + const storage = createStorage(key); + const useShows = createUseShows(storage); + const useShowsMutation = createUseShowsMutation(storage); + const useStateToggle = createUseStateToggle({ + useShows, + useShowsMutation + }); + return { useShows, useShowsMutation, useStateToggle }; +}; + +export const { useShows: useFavorites, useStateToggle: useFavoritesStateToggle } = + createStorageHooks('users-favorites'); +export const { useShows: useWatchList, useStateToggle: useWatchListStateToggle } = + createStorageHooks('users-watch-list'); diff --git a/hooks/useShowState.ts b/hooks/useShowState.ts new file mode 100644 index 0000000..85d6d9e --- /dev/null +++ b/hooks/useShowState.ts @@ -0,0 +1,69 @@ +import { accountStates, updateAccountStates } from '@/api'; +import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; +import { useFavorites, useFavoritesStateToggle, useWatchList, useWatchListStateToggle } from './useLocalUsersLists'; +import { useSession } from './useSession'; + +import { KEY as USERS_SHOWS_KEY } from './useUsersShows'; + +const KEY = ['show-state']; + +const useShowState = ({ showId, showType = 'movie' }: { showId?: Show['id']; showType?: Show['type'] }) => { + const session = useSession(); + + const { map: favorites } = useFavorites(); + const { map: watchList } = useWatchList(); + + const query = useQuery({ + enabled: Boolean(session && showId && showType), + queryKey: KEY.concat(showId!.toString(), showType), + queryFn: () => accountStates({ showId: showId!, showType }), + notifyOnChangeProps: ['data'], + staleTime: Infinity + }); + + return session ? query.data : { id: showId, watchlist: watchList.has(showId!), favorite: favorites.has(showId!) }; +}; + +export const useMutateShowState = (show: Show) => { + const session = useSession(); + + const { toggle: toggleFavorite } = useFavoritesStateToggle(show); + const { toggle: toggleWatchList } = useWatchListStateToggle(show); + + const queryClient = useQueryClient(); + + const mutation = useMutation({ + mutationFn: updateAccountStates, + onMutate({ showId, showType, list, value }) { + const key = KEY.concat(showId.toString(), showType); + + const snapshot = queryClient.getQueryData(key); + + queryClient.setQueryData(key, (state) => ({ ...state, [list]: value }) as ShowState); + + return snapshot; + }, + onError(_, { showId, showType }, snapshot) { + const key = KEY.concat(showId.toString(), showType); + + queryClient.setQueryData(key, snapshot); + }, + onSuccess(_, { list, showType }) { + queryClient.invalidateQueries({ queryKey: USERS_SHOWS_KEY({ list, showType }) }); + } + }); + + return { + list: mutation.variables?.list, + isPending: session ? mutation.isPending : false, + mutate: ({ list, value }: { list: 'favorite' | 'watchlist'; value: boolean }) => { + if (session) return mutation.mutate({ showId: show.id, showType: show.type, list, value }); + + if (list === 'favorite') return toggleFavorite(); + + return toggleWatchList(); + } + }; +}; + +export default useShowState; diff --git a/hooks/useUsersShows.ts b/hooks/useUsersShows.ts new file mode 100644 index 0000000..153555c --- /dev/null +++ b/hooks/useUsersShows.ts @@ -0,0 +1,36 @@ +import { usersShows } from '@/api'; +import useInfiniteList from './useInfiniteList'; +import { useSession } from './useSession'; +import { useFavorites, useWatchList } from './useLocalUsersLists'; + +export const KEY = ({ list, showType }: { showType: Show['type']; list: 'favorite' | 'watchlist' }) => [ + `users-${list}`, + showType +]; + +const useUsersShows = ({ showType, list = 'favorite' }: { showType: Show['type']; list: 'favorite' | 'watchlist' }) => { + const session = useSession(); + + const { shows: favorite } = useFavorites(); + const { shows: watchlist } = useWatchList(); + + const query = useInfiniteList({ + queryKey: KEY({ list, showType }), + queryFn: () => usersShows({ showType, list }), + enabled: Boolean(session) + }); + + return session + ? query + : { + fetchNextPage: undefined, + isLoading: false, + isFetched: true, + shows: { + favorite: favorite.filter((show) => show.type === showType), + watchlist: watchlist.filter((show) => show.type === showType) + }[list] + }; +}; + +export default useUsersShows; diff --git a/providers/Theme.tsx b/providers/Theme.tsx index 40fc20b..d87d91c 100644 --- a/providers/Theme.tsx +++ b/providers/Theme.tsx @@ -22,6 +22,9 @@ const Provider: FC = ({ children, theme: initialTheme }) => { } document.documentElement.setAttribute('data-mode', theme); + document + .querySelector('meta[name="theme-color"]') + ?.setAttribute('content', theme === 'dark' ? 'hsl(222.2 84% 4.9%)' : 'white'); Cookies.set('theme', theme); }, [theme, setTheme]); diff --git a/tailwind.config.js b/tailwind.config.js index ba14aa3..7724701 100644 --- a/tailwind.config.js +++ b/tailwind.config.js @@ -70,15 +70,22 @@ module.exports = { fadeAsideSlideIn: { '0%': { opacity: 0, transform: 'translate3d(-20px, 0, 0)' }, '100%': { opacity: 1, transform: 'translate3d(0, 0, 0)' } + }, + glow: { + '0%': { backgroundPosition: '0 0' }, + '50%': { backgroundPosition: '100% 0' }, + '100%': { backgroundPosition: '0 0' } } }, animation: { + glow: 'glow 20s linear infinite', 'accordion-down': 'accordion-down 0.2s ease-out', 'accordion-up': 'accordion-up 0.2s ease-out', 'fade-in-slide-in': 'fadeInSlideIn 0.2s ease-in-out forwards', 'fade-aside-slide-in': 'fadeAsideSlideIn 0.2s ease-in-out forwards' }, backgroundImage: { + glow: 'linear-gradient(45deg,rgba(255, 255, 255, 0.2),rgba(255, 255, 255, 0.2),rgba(255, 255, 255, 0.2),rgba(0, 0, 0, 0.4),rgba(0, 0, 0, 0.4),rgba(255, 255, 255, 0.2),rgba(255, 255, 255, 0.2),rgba(255, 255, 255, 0.2))', vignette: 'linear-gradient(rgba(0, 0, 0, 0.4) 0%,rgba(0, 0, 0, 0.396) 2.83%,rgba(0, 0, 0, 0.384) 5.67%,rgba(0, 0, 0, 0.37) 8.5%,rgba(0, 0, 0, 0.34) 11.33%,rgba(0, 0, 0, 0.306) 14.17%,rgba(0, 0, 0, 0.267) 17%,rgba(0, 0, 0, 0.224) 19.83%,rgba(0, 0, 0, 0.176) 22.67%,rgba(0, 0, 0, 0.133) 25.5%,rgba(0, 0, 0, 0.094) 28.33%,rgba(0, 0, 0, 0.06) 31.17%,rgba(0, 0, 0, 0.03) 34%,rgba(0, 0, 0, 0.016) 36.83%,rgba(0, 0, 0, 0.004) 39.67%,rgba(0, 0, 0, 0) 42.5%,rgba(0, 0, 0, 0) 57.5%,rgba(0, 0, 0, 0.004) 60.33%,rgba(0, 0, 0, 0.016) 63.17%,rgba(0, 0, 0, 0.03) 66%,rgba(0, 0, 0, 0.06) 68.83%,rgba(0, 0, 0, 0.094) 71.67%,rgba(0, 0, 0, 0.133) 74.5%,rgba(0, 0, 0, 0.176) 77.33%,rgba(0, 0, 0, 0.224) 80.17%,rgba(0, 0, 0, 0.267) 83%,rgba(0, 0, 0, 0.306) 85.83%,rgba(0, 0, 0, 0.34) 88.67%,rgba(0, 0, 0, 0.37) 91.5%,rgba(0, 0, 0, 0.384) 94.33%,rgba(0, 0, 0, 0.396) 97.17%,rgba(0, 0, 0, 0.4) 100%)' } diff --git a/types.d.ts b/types.d.ts index 3387196..a89205b 100644 --- a/types.d.ts +++ b/types.d.ts @@ -1,4 +1,4 @@ -type Theme = 'light' | 'dark' | 'system'; +type Theme = 'light' | 'dark'; interface TMDBPagination { page: number; @@ -66,6 +66,13 @@ interface Show { votes: number; } +interface ShowState { + id: number; + favorite: boolean; + rated: boolean; + watchlist: boolean; +} + interface Person { adult: boolean; gender: number;