feat: animations

This commit is contained in:
ner committed 2023-12-29 17:18:06 +02:00
1 parent e4788ae384
commit 7b55619cf5
16 files changed
+643 -320

No files matched your search

+45 -22
View File
@@ -1,8 +1,9 @@
import { FC, Fragment, useLayoutEffect, useState } from 'react';
import { motion } from 'framer-motion';
import Card from '@/components/Movie/Card';
import Preview from '@/components/Movie/Preview';
import { Dialog, DialogContent } from '@/components/ui/dialog';
import { Modal } from '@/components/ui/dialog';
import filterUnknownMovies from '@/lib/filterUnknownMovies';
interface Props {
@@ -13,6 +14,23 @@ interface Props {
onPreviewClose?: () => void;
}
const container = {
hidden: {},
visible: {
transition: {
staggerChildren: 0.15
}
}
};
const item = {
hidden: { y: -20, opacity: 0 },
visible: {
y: 0,
opacity: 1
}
};
const List: FC<Props> = ({ pages, hasNextPage, fetchNextPage, withBottomGap = true, onPreviewClose }) => {
const [movie, setMovie] = useState<Movie | null>(null);
const [loader, setLoader] = useState<HTMLLIElement>();
@@ -38,39 +56,44 @@ const List: FC<Props> = ({ pages, hasNextPage, fetchNextPage, withBottomGap = tr
return (
<>
<ul className='flex gap-3 flex-wrap justify-center grow'>
<motion.ul
className='flex gap-3 flex-wrap justify-center grow'
variants={container}
initial='hidden'
animate='visible'
layout
>
{pages.map((page) => (
<Fragment key={page.page}>
{filterUnknownMovies(page.results)?.map((movie, index, array) => (
<li
<motion.li
key={movie.id}
variants={item}
className='flex basis-[300px] cursor-pointer'
onClick={() => setMovie(movie)}
onTap={() => setMovie(movie)}
ref={array.length / 2 !== index ? undefined : (element) => setLoader(element!)}
>
<Card movie={movie} className='grow flex flex-col' />
</li>
</motion.li>
))}
</Fragment>
))}
</ul>
</motion.ul>
{withBottomGap && <div className='h-4 w-full' />}
{movie && (
<Dialog defaultOpen={true} onOpenChange={(isOpen) => !isOpen && setMovie(null)}>
<DialogContent className='block p-0' onClose={() => setMovie(null)}>
<Preview
key={movie.id}
movie={movie}
className='border-none'
onClose={() => {
setMovie(null);
window.scrollTo({ top: 0, behavior: 'smooth' });
onPreviewClose?.();
}}
/>
</DialogContent>
</Dialog>
)}
<Modal isOpen={Boolean(movie)} onClose={() => setMovie(null)} className='block p-0'>
{movie && (
<Preview
key={movie.id}
movie={movie}
className='border-none'
onClose={() => {
setMovie(null);
window.scrollTo({ top: 0, behavior: 'smooth' });
onPreviewClose?.();
}}
/>
)}
</Modal>
</>
);
};
+70 -41
View File
@@ -1,70 +1,99 @@
import { FC, useState } from 'react';
import { FC } from 'react';
import { AnimatePresence, motion } from 'framer-motion';
import { Play } from 'lucide-react';
import { useRouter } from 'next/navigation';
import useTrailer from '@/app/(site)/useTrailer';
import Credits from '@/components/Movie/components/Credits';
import Trailer from '@/components/Movie/components/Trailer';
import { showTrailerModal } from '@/components/Movie/components/Trailer';
import { Separator } from '@/components/ui/separator';
import { Button } from '../ui/button';
import Torrents from './components/Torrents';
import { showTorrentsModal } from './components/Torrents';
import Card from './Card';
interface Props {
movie?: Movie;
className?: string;
direction?: number;
onClose?: () => void;
}
const Preview: FC<Props> = ({ movie, className, onClose }) => {
const [isShowTrailer, setIsShowTrailer] = useState(false);
const [isShowTorrents, setIsShowTorrents] = useState(false);
const variants = {
enter: (direction: number) => ({
scale: 0.8,
opacity: 0,
[direction === 0 ? 'y' : 'x']: direction ? direction * -320 : -100
}),
center: { x: 0, opacity: 1, y: 0, scale: 1 },
exit: (direction: number) => ({ scale: 0.8, opacity: 1, [direction === 0 ? 'y' : 'x']: (direction || -1) * 320 })
};
const Preview: FC<Props> = ({ movie, className, onClose, direction }) => {
const { data: trailer } = useTrailer({ movieId: movie?.id });
const router = useRouter();
if (!movie) return null;
return (
<>
<div className={`p-3 rounded-xl border bg-card text-card-foreground shadow-sm ${className}`}>
<div className='flex flex-col lg:flex-row max-w-[100%] gap-3'>
<Card movie={movie} className='max-w-[300px] min-w-[300px] mx-auto w-full'>
{trailer && (
<div
onClick={() => setIsShowTrailer(true)}
title='Play trailer'
className='absolute w-full h-full left-0 top-0 bg-black cursor-pointer overflow-hidden rounded-lg transition-opacity opacity-0 hover:opacity-70 flex items-center justify-center'
>
<Play size={100} className='fill-white' />
</div>
)}
</Card>
<div className='flex flex-col grow'>
<div className='flex w-full gap-2'>
<Button className='h-8 grow' onClick={() => setIsShowTorrents(true)}>
Torrents
</Button>
<Button
className='h-8 grow'
onClick={() => {
router.push(`/top?similar=${movie.id}&title=${encodeURIComponent(movie.title)}`);
onClose?.();
}}
>
Similar
</Button>
</div>
<Separator className='my-2' />
<p className='leading-7'>{movie.overview}</p>
<div className={`rounded-xl bg-card text-card-foreground ${className} ${onClose ? 'p-2' : ''}`}>
<div className='flex flex-col lg:flex-row max-w-[100%] gap-3'>
<div className='max-w-[300px] min-w-[300px] h-[450px] mx-auto w-[100%] relative overflow-hidden rounded-lg'>
<AnimatePresence custom={direction}>
<motion.div
className='absolute'
initial='enter'
animate='center'
exit='exit'
key={movie?.id}
variants={variants}
custom={direction}
transition={{ type: 'tween' }}
>
<Card movie={movie} className='max-w-[300px] min-w-[300px] mx-auto w-full'>
{trailer && (
<motion.div
onTap={() => showTrailerModal({ trailerKey: trailer?.key })}
title='Play trailer'
className='absolute w-full h-full left-0 top-0 bg-black cursor-pointer overflow-hidden rounded-lg transition-opacity opacity-0 hover:opacity-70 flex items-center justify-center'
>
<Play size={100} className='fill-white' />
</motion.div>
)}
</Card>
</motion.div>
</AnimatePresence>
</div>
<div className='flex flex-col grow'>
<div className='flex w-full gap-2'>
<Button className='h-8 grow' onClick={() => showTorrentsModal({ title: movie.title })}>
Torrents
</Button>
<Button
className='h-8 grow'
onClick={() => {
router.push(`/top?similar=${movie.id}&title=${encodeURIComponent(movie.title)}`);
onClose?.();
}}
>
Similar
</Button>
</div>
<Separator className='my-2' />
<motion.p
key={movie.id}
className='leading-7'
initial={{ opacity: 0, y: -100 }}
animate={{ y: 0, opacity: 1 }}
exit={{ y: 100, opacity: 0 }}
>
{movie.overview}
</motion.p>
</div>
<Credits movieId={movie.id} onPersonClick={() => onClose?.()} />
</div>
{isShowTrailer && <Trailer trailerKey={trailer?.key} onClose={() => setIsShowTrailer(false)} />}
{isShowTorrents && <Torrents title={movie.title} onClose={() => setIsShowTorrents(false)} />}
</>
<Credits movieId={movie.id} onPersonClick={() => onClose?.()} />
</div>
);
};
+24 -5
View File
@@ -1,4 +1,5 @@
import { FC } from 'react';
import { motion } from 'framer-motion';
import { User2 } from 'lucide-react';
import Image from 'next/image';
import { useRouter } from 'next/navigation';
@@ -10,6 +11,23 @@ interface Props {
onPersonClick?: (id: string) => void;
}
const container = {
hidden: {},
visible: {
transition: {
staggerChildren: 0.15
}
}
};
const item = {
hidden: { x: -20, opacity: 0 },
visible: {
x: 0,
opacity: 1
}
};
const Credits: FC<Props> = ({ movieId, onPersonClick }) => {
const router = useRouter();
const { data: credits } = useCredits({ movieId });
@@ -17,12 +35,13 @@ const Credits: FC<Props> = ({ movieId, onPersonClick }) => {
return (
Boolean(credits?.length) && (
<div className='overflow-auto max-w-[100%] py-3 mb-[-0.75rem]'>
<ul className='flex gap-3'>
<motion.ul className='flex gap-3' variants={container} initial='hidden' animate='visible' layout>
{credits?.map((actor) => (
<li
<motion.li
variants={item}
key={`${actor.id}_${actor.cast_id}`}
title={actor.name}
onClick={() => {
onTap={() => {
router.push(`/top?starring=${actor.id}&title=${encodeURIComponent(actor.name)}`);
onPersonClick?.(actor.id.toString());
}}
@@ -47,9 +66,9 @@ const Credits: FC<Props> = ({ movieId, onPersonClick }) => {
)}
</div>
<p className='pt-2 overflow-ellipsis whitespace-nowrap overflow-hidden'>{actor.name}</p>
</li>
</motion.li>
))}
</ul>
</motion.ul>
</div>
)
);
+4 -3
View File
@@ -1,4 +1,5 @@
import { FC } from 'react';
import { motion } from 'framer-motion';
import { Heart, Star } from 'lucide-react';
import useFavorites, { useFavoritesMutation } from '@/app/(site)/favorites/useFavorites';
@@ -16,8 +17,8 @@ const Rating: FC<Props> = ({ movie, iconsFillColor = 'fill-accent-foreground' })
return (
<span className='select-none flex w-full items-center ml-auto gap-1 mt-[-8px] justify-between'>
<span
onClick={(event) => {
<motion.span
onTap={(event) => {
event.stopPropagation();
isFavorite ? remove(movie.id) : add(movie);
@@ -26,7 +27,7 @@ const Rating: FC<Props> = ({ movie, iconsFillColor = 'fill-accent-foreground' })
title={isFavorite ? 'Remove from favorites' : 'Add to favorite'}
>
<Heart className={isFavorite ? iconsFillColor : ''} size={20} />
</span>
</motion.span>
<span className='flex items-center'>
<Star size={18} className={`mr-1 ${iconsFillColor}`} />
<span className='font-semibold'>{movie.vote_average?.toFixed(1)}</span>
+80 -79
View File
@@ -1,18 +1,18 @@
import React, { Dispatch, FC, SetStateAction, useState } from 'react';
import { create, InstanceProps } from 'react-modal-promise';
import { ArrowDownIcon, CaretSortIcon } from '@radix-ui/react-icons';
import { useAtom } from 'jotai';
import { useAtom, useAtomValue } from 'jotai';
import { atomWithStorage } from 'jotai/utils';
import { Cast, Loader, Magnet } from 'lucide-react';
import useTorrents from '@/app/(site)/useTorrents';
import { ORDER } from '@/app/api/torrents/parsers/pirate-bay';
import { Dialog, DialogContent } from '@/components/ui/dialog';
import { Modal } from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
interface Props {
interface Props extends InstanceProps<void> {
title: string;
onClose: () => void;
}
type Sorting = {
@@ -49,9 +49,23 @@ const TableHeadSortable = ({
const hostAtom = atomWithStorage<string>('magnet-host', '');
const Torrents: FC<Props> = ({ title, onClose }) => {
const showHostManagerModal = create(({ onResolve, isOpen }) => {
const [host, setHost] = useAtom(hostAtom);
const [isShowHostInput, setIsShowHostInput] = useState(false);
return (
<Modal className='p-3 pt-0 max-w-[200px] m-auto' isOpen={isOpen} onClose={() => onResolve()}>
<Input
autoFocus
placeholder='Enter host of Elementum'
value={host}
onChange={({ target: { value } }) => setHost(value)}
onKeyDown={({ key }) => key === 'Enter' && onResolve()}
/>
</Modal>
);
});
const Torrents: FC<Props> = ({ title, isOpen, onResolve }) => {
const host = useAtomValue(hostAtom);
const [sorting, setSorting] = useState<Sorting>({ by: ORDER.seeders });
const { data: torrents, isLoading, isFetched } = useTorrents({ query: title, order: sorting.by });
@@ -64,81 +78,68 @@ const Torrents: FC<Props> = ({ title, onClose }) => {
};
return (
<Dialog defaultOpen={true} onOpenChange={(isOpen) => !isOpen && onClose()}>
<DialogContent className='p-0' onClose={onClose}>
<Table className='rounded-xl overflow-hidden'>
<TableHeader>
<TableRow>
<TableHead className='px-2'>Title</TableHead>
<TableHeadSortable title='Size' by={ORDER.size} value={sorting} onChange={setSorting} />
<TableHeadSortable title='Seeders' by={ORDER.seeders} value={sorting} onChange={setSorting} />
<TableHead className='px-2 text-center' onClick={() => setIsShowHostInput(true)}>
Magnet
</TableHead>
<Modal className='p-0' isOpen={isOpen} onClose={() => onResolve()}>
<Table className='rounded-xl overflow-hidden'>
<TableHeader>
<TableRow>
<TableHead className='px-2'>Title</TableHead>
<TableHeadSortable title='Size' by={ORDER.size} value={sorting} onChange={setSorting} />
<TableHeadSortable title='Seeders' by={ORDER.seeders} value={sorting} onChange={setSorting} />
<TableHead className='px-2 text-center cursor-pointer select-none' onClick={() => showHostManagerModal()}>
Magnet
</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{torrents?.map((torrent) => (
<TableRow key={torrent.magnet + torrent.id}>
<TableCell className='break-all p-2'>{torrent.title}</TableCell>
<TableCell className='p-2'>
<div className='flex items-center gap-1 shrink-0 justify-center'>{torrent.size}</div>
</TableCell>
<TableCell className='text-center p-2'>{torrent.seeders}</TableCell>
<TableCell className='text-center p-1'>
<div className='flex gap-2 m-auto justify-center'>
{host ? (
<a
onClick={handleCast(torrent.magnet)}
className='flex items-center justify-center h-8 w-8 border rounded-lg'
>
<Cast size={20} />
</a>
) : (
<a href={torrent.magnet} className='flex items-center justify-center h-8 w-8 border rounded-lg'>
<Magnet size={20} />
</a>
)}
</div>
</TableCell>
</TableRow>
</TableHeader>
<TableBody>
{torrents?.map((torrent) => (
<TableRow key={torrent.magnet + torrent.id}>
<TableCell className='break-all p-2'>{torrent.title}</TableCell>
<TableCell className='p-2'>
<div className='flex items-center gap-1 shrink-0 justify-center'>{torrent.size}</div>
</TableCell>
<TableCell className='text-center p-2'>{torrent.seeders}</TableCell>
<TableCell className='text-center p-1'>
<div className='flex gap-2 m-auto justify-center'>
{host ? (
<a
onClick={handleCast(torrent.magnet)}
className='flex items-center justify-center h-8 w-8 border rounded-lg'
>
<Cast size={20} />
</a>
) : (
<a href={torrent.magnet} className='flex items-center justify-center h-8 w-8 border rounded-lg'>
<Magnet size={20} />
</a>
)}
</div>
</TableCell>
</TableRow>
))}
{isLoading && (
<TableRow>
<TableCell colSpan={4}>
<div className='h-40 w-full flex items-center justify-center'>
<Loader className='animate-spin' />
</div>
</TableCell>
</TableRow>
)}
{isFetched && !torrents?.length && (
<TableRow>
<TableCell colSpan={4}>
<div className='h-40 w-full flex items-center justify-center text-xl text-muted-foreground'>
Nothing was found
</div>
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
{isShowHostInput && (
<Dialog defaultOpen={true} onOpenChange={() => setIsShowHostInput(false)}>
<DialogContent className='flex p-3 max-w-[300px] gap-2' onClose={() => setIsShowHostInput(false)}>
<Input
autoFocus
placeholder='Enter host of Elementum'
value={host}
onChange={({ target: { value } }) => setHost(value)}
onKeyDown={({ key }) => key === 'Enter' && setIsShowHostInput(false)}
/>
</DialogContent>
</Dialog>
)}
</DialogContent>
</Dialog>
))}
{isLoading && (
<TableRow>
<TableCell colSpan={4}>
<div className='h-40 w-full flex items-center justify-center'>
<Loader className='animate-spin' />
</div>
</TableCell>
</TableRow>
)}
{isFetched && !torrents?.length && (
<TableRow>
<TableCell colSpan={4}>
<div className='h-40 w-full flex items-center justify-center text-xl text-muted-foreground'>
Nothing was found
</div>
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</Modal>
);
};
export const showTorrentsModal = create(Torrents);
export default Torrents;
+22 -18
View File
@@ -1,29 +1,33 @@
import { FC } from 'react';
import { create, InstanceProps } from 'react-modal-promise';
import { Dialog, DialogContent } from '@/components/ui/dialog';
import { Modal } from '@/components/ui/dialog';
interface Props {
interface Props extends InstanceProps<void> {
trailerKey?: string;
onClose: () => void;
}
const Trailer: FC<Props> = ({ trailerKey, onClose }) => {
const Trailer: FC<Props> = ({ trailerKey, onResolve, isOpen }) => {
if (!trailerKey) return;
return (
trailerKey && (
<Dialog defaultOpen={true} onOpenChange={(isOpen) => !isOpen && onClose()}>
<DialogContent className='block p-0 min-h-[400px] border-none aspect-w-16 aspect-h-9' onClose={onClose}>
<iframe
allow='autoplay'
className='border-none rounded-lg'
width='100%'
height='100%'
src={`//www.youtube.com/embed/${trailerKey}?autoplay=1`}
allowFullScreen
/>
</DialogContent>
</Dialog>
)
<Modal
className='block p-0 min-h-[400px] border-none aspect-w-16 aspect-h-9'
onClose={() => onResolve()}
isOpen={isOpen}
>
<iframe
allow='autoplay'
className='border-none rounded-lg'
width='100%'
height='100%'
src={`//www.youtube.com/embed/${trailerKey}?autoplay=1`}
allowFullScreen
/>
</Modal>
);
};
export const showTrailerModal = create(Trailer);
export default Trailer;