feat: new nav

This commit is contained in:
ner committed 2024-08-29 12:58:49 +03:00
1 parent 5733bc0af0
commit 750d5b12c8
25 files changed
+386 -325

No files matched your search

+12 -23
View File
@@ -1,12 +1,13 @@
import { FC } from 'react';
import useFavorites, { useFavoritesMutation } from '@/app/(site)/favorites/useFavorites';
import Poster from '@/components/Movie/components/Poster';
import Rating from '@/components/Movie/components/Rating';
import Title from '@/components/Movie/components/Title';
import { AspectRatio } from '@/components/ui/aspect-ratio';
import { cn } from '@/lib/utils';
import Favorite from './components/Favorite';
import Handlers from './components/Handlers';
interface Props {
movie?: Movie;
onClick?: () => void;
@@ -14,43 +15,31 @@ interface Props {
}
const Card: FC<Props> = ({ movie, onClick, className }) => {
const { add, remove } = useFavoritesMutation();
const { map } = useFavorites();
if (!movie) return null;
const isFavorite = map.has(movie.id);
const handleToggleFavorite = () => (isFavorite ? remove(movie.id) : add(movie));
return (
<div className='xs:w-[300px] xs:h-[450px] w-full h-full shrink-0'>
<AspectRatio ratio={2 / 3}>
<div
onClick={onClick}
onDoubleClick={handleToggleFavorite}
className={cn(
'h-full relative bg-card text-card-foreground shadow-sm transition-shadow hover:shadow-lg overflow-hidden rounded-lg',
className
)}
>
<div className='flex w-full h-full'>
<Poster title={movie?.name || movie.title} poster={movie.poster_path} />
<Poster title={movie.name || movie.title} poster={movie.poster_path} />
</div>
<div className='text-white absolute w-full h-full top-0 left-0 rounded-md flex flex-col justify-between p-2 card-gradient'>
<div className='grid items-center grid-cols-[1fr_50px] mt-[-5px] opacity-75'>
<Title key={movie?.name || movie.title} title={movie?.name || movie.title} />
{movie.release_date && (
<span className='flex justify-end text-lg'>{new Date(movie.release_date).getFullYear()}</span>
)}
<Handlers movie={movie} onClick={onClick} />
<div className='flex items-start justify-between text-lg opacity-75'>
<span>{movie.name || movie.title}</span>
{Boolean(movie.release_date) && <span>{new Date(movie.release_date).getFullYear()}</span>}
</div>
<div className='opacity-75 mb-[-5px]'>
<Rating
isFavorite={isFavorite}
toggleFavorite={handleToggleFavorite}
rating={movie.vote_average}
iconsFillColor='fill-white'
/>
<span className='select-none flex w-full items-center ml-auto gap-1 mt-[-8px] justify-between z-[1]'>
<Favorite movie={movie} />
<Rating rating={movie.vote_average} />
</span>
</div>
</div>
</div>
+28 -25
View File
@@ -1,5 +1,6 @@
import { FC, forwardRef, useMemo } from 'react';
import { FC, useMemo, useRef } from 'react';
import { VirtuosoGrid, VirtuosoGridProps } from 'react-virtuoso';
import { motion } from 'framer-motion';
import Card from '@/components/Movie/Card';
import { showPreviewModal } from '@/components/Movie/Preview';
@@ -9,20 +10,11 @@ interface Props {
pages: Array<MovieDBResponse>;
hasNextPage?: boolean;
fetchNextPage?: () => void;
withBottomGap?: boolean;
onPreviewClose?: () => void;
type?: ShowType;
customScrollParent?: HTMLElement;
}
const container = {
hidden: {},
visible: {
transition: {
staggerChildren: 0.15
}
}
};
const item = {
hidden: { y: -20, opacity: 0 },
visible: {
@@ -31,26 +23,31 @@ const item = {
}
};
const components: VirtuosoGridProps<Movie, { shows: Movie[]; onClick: (movie: Movie) => void }>['components'] = {
List: forwardRef(function List(props, ref) {
return <div {...props} className='flex gap-3 flex-wrap justify-center grow empty:hidden' ref={ref} />;
}),
const components: VirtuosoGridProps<
Movie,
{ shows: Movie[]; initial: boolean; onClick: (movie: Movie) => void }
>['components'] = {
Item: ({ context, 'data-index': index }) => {
const { shows, onClick } = context || {};
const movie = shows?.[index];
return <Card movie={movie} onClick={() => onClick?.(movie!)} />;
return (
<motion.div
variants={item}
initial='hidden'
animate='visible'
transition={context?.initial ? { delay: 0.15 * index } : undefined}
className='xs:w-[300px] xs:h-[450px] w-full h-full shrink-0'
>
<Card movie={movie} onClick={() => onClick?.(movie!)} />
</motion.div>
);
}
};
const List: FC<Props> = ({
pages,
hasNextPage,
fetchNextPage,
withBottomGap = true,
onPreviewClose,
type = 'movie'
}) => {
const List: FC<Props> = ({ pages, hasNextPage, fetchNextPage, onPreviewClose, type = 'movie', customScrollParent }) => {
const initial = useRef(true);
const shows = useMemo(
() =>
pages?.reduce((shows, page) => {
@@ -64,11 +61,13 @@ const List: FC<Props> = ({
const context = {
shows,
initial: initial.current,
onClick: (movie: Movie) => showPreviewModal({ movie, onClose: onPreviewClose, type })
};
const handleFetchMore = () => {
if (!hasNextPage) return;
initial.current = false;
fetchNextPage?.();
};
@@ -76,13 +75,17 @@ const List: FC<Props> = ({
<>
<VirtuosoGrid
useWindowScroll
listClassName='flex gap-2 flex-wrap justify-center grow empty:hidden'
customScrollParent={customScrollParent}
totalCount={shows.length}
initialItemCount={20}
overscan={4}
endReached={handleFetchMore}
data={shows}
context={context}
components={components}
/>
{withBottomGap && <div className='h-2 w-full' />}
<div className='h-2 w-full' />
</>
);
};
@@ -0,0 +1,23 @@
'use client';
import { FC } from 'react';
import { Heart } from 'lucide-react';
import { useFavoriteToggle } from '@/app/(site)/favorites/useFavorites';
import { cn } from '@/lib/utils';
interface Props {
movie: Movie;
}
const Favorite: FC<Props> = ({ movie }) => {
const { isFavorite, toggle } = useFavoriteToggle(movie);
return (
<span onClick={toggle} className='cursor-pointer' title={isFavorite ? 'Remove from favorites' : 'Add to favorite'}>
<Heart className={cn(isFavorite && 'fill-white')} size={20} />
</span>
);
};
export default Favorite;
@@ -0,0 +1,18 @@
'use client';
import { FC } from 'react';
import { useFavoriteToggle } from '@/app/(site)/favorites/useFavorites';
interface Props {
movie: Movie;
onClick?: () => void;
}
const Handlers: FC<Props> = ({ onClick, movie }) => {
const { toggle } = useFavoriteToggle(movie);
return <div onClick={onClick} onDoubleClick={toggle} className='absolute top-0 left-0 w-full h-full' />;
};
export default Handlers;
+1 -1
View File
@@ -14,7 +14,7 @@ const Poster: FC<Props> = ({ title, poster }) => {
alt={title}
/>
) : (
<div className={'w-full flex items-center justify-center rounded-lg grow'}>
<div className='w-full flex items-center justify-center rounded-lg grow'>
<ImageOff size={100} strokeWidth={1} color='hsl(215.4 16.3% 46.9%)' />
</div>
);
+7 -27
View File
@@ -1,35 +1,15 @@
import { FC } from 'react';
import { motion } from 'framer-motion';
import { Heart, Star } from 'lucide-react';
import { Star } from 'lucide-react';
interface Props {
isFavorite: boolean;
toggleFavorite: () => void;
iconsFillColor?: string;
rating?: number;
}
const Rating: FC<Props> = ({ isFavorite, rating, toggleFavorite, iconsFillColor = 'fill-accent-foreground' }) => {
return (
<span className='select-none flex w-full items-center ml-auto gap-1 mt-[-8px] justify-between'>
<motion.span
onTap={(event) => {
event.preventDefault();
event.stopPropagation();
toggleFavorite();
}}
className='z-[2] cursor-pointer'
title={isFavorite ? 'Remove from favorites' : 'Add to favorite'}
>
<Heart className={isFavorite ? iconsFillColor : ''} size={20} />
</motion.span>
<span className='flex items-center'>
<Star size={18} className={`mr-1 ${iconsFillColor}`} />
<p className='mt-[3px]'>{rating?.toFixed(1)}</p>
</span>
</span>
);
};
const Rating: FC<Props> = ({ rating }) => (
<span className='flex items-center'>
<Star size={18} className='mr-1 fill-white' />
<p className='mt-[3px]'>{rating?.toFixed(1)}</p>
</span>
);
export default Rating;
@@ -1,33 +0,0 @@
'use client';
import { FC, useState } from 'react';
import Marquee from 'react-fast-marquee';
interface Props {
title: string;
className?: string;
}
const Title: FC<Props> = ({ title, className }) => {
const [isScrollableTitle, setIsScrollableTitle] = useState(false);
return (
<Marquee
play={isScrollableTitle}
gradientWidth={10}
className={`movie-title overflow-hidden h-[30px] text-lg font-semibold ${className}`}
>
<div
ref={(element) => {
const header = element?.closest('.movie-title') as HTMLDivElement;
const isScrollableTitle = (header?.offsetWidth || 0) < (element?.offsetWidth || 0);
isScrollableTitle && setIsScrollableTitle(isScrollableTitle);
}}
>
{title}&nbsp;
</div>
</Marquee>
);
};
export default Title;