feat: new nav
This commit is contained in:
1 parent
5733bc0af0
commit
750d5b12c8
25 files changed
+386
-325
No files matched your search
@@ -0,0 +1,80 @@
|
||||
'use client';
|
||||
|
||||
import { forwardRef, ReactNode, startTransition, useEffect, useRef, useState } from 'react';
|
||||
import { StateSnapshot, Virtuoso, VirtuosoHandle, VirtuosoProps } from 'react-virtuoso';
|
||||
|
||||
import Card from '@/components/Movie/Card';
|
||||
|
||||
const components: VirtuosoProps<Movie, number>['components'] = {
|
||||
Item: (props) => <div {...props} className='shrink-0' style={{ ...props.style, width: props.context }} />,
|
||||
List: forwardRef(function List(props, ref) {
|
||||
return (
|
||||
<div {...props} ref={ref} className='gap-2' style={{ ...props.style, display: 'flex', width: props.context }} />
|
||||
);
|
||||
}),
|
||||
Scroller: forwardRef(function Scroller(props, ref) {
|
||||
return <div {...props} ref={ref} className='overflow-hidden no-scrollbar snap-mandatory snap-x rounded-lg' />;
|
||||
})
|
||||
};
|
||||
|
||||
export const Measurer = ({ children }: { children: (width: number) => ReactNode }) => {
|
||||
const [ref, setRef] = useState<HTMLDivElement | null>(null);
|
||||
const width = ref?.offsetWidth;
|
||||
|
||||
return (
|
||||
<>
|
||||
<div ref={setRef} />
|
||||
{children(width || 0)}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
let snapshot: StateSnapshot;
|
||||
|
||||
export const Carousel = ({
|
||||
movies,
|
||||
width: initialWidth,
|
||||
onIndexChange
|
||||
}: {
|
||||
movies?: Movie[];
|
||||
width: number;
|
||||
onIndexChange: (index: number) => void;
|
||||
}) => {
|
||||
const timeout = useRef<NodeJS.Timeout>();
|
||||
const isMobile = matchMedia('(max-width: 500px)').matches;
|
||||
const ref = useRef<VirtuosoHandle>(null);
|
||||
|
||||
const width = isMobile ? initialWidth : 300;
|
||||
const height = isMobile ? (width * 3) / 2 : 450;
|
||||
|
||||
useEffect(() => {
|
||||
const virtuoso = ref.current;
|
||||
return () => virtuoso?.getState((state) => (snapshot = state));
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<Virtuoso
|
||||
ref={ref}
|
||||
data={movies}
|
||||
defaultItemHeight={height}
|
||||
style={{ width, height, flexShrink: 0, margin: '0 auto' }}
|
||||
totalCount={movies?.length}
|
||||
horizontalDirection
|
||||
context={width}
|
||||
increaseViewportBy={154}
|
||||
restoreStateFrom={snapshot}
|
||||
components={components}
|
||||
itemContent={(index) => {
|
||||
clearTimeout(timeout.current);
|
||||
|
||||
timeout.current = setTimeout(() => {
|
||||
const currentIndex = index - 1;
|
||||
|
||||
startTransition(() => onIndexChange(currentIndex));
|
||||
}, 50);
|
||||
|
||||
return <Card className='snap-start' movie={movies?.[index]} />;
|
||||
}}
|
||||
/>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,85 @@
|
||||
'use client';
|
||||
|
||||
import { Dispatch, SetStateAction, useRef, useState } from 'react';
|
||||
|
||||
import useFilters from '@/app/(site)/useFilters';
|
||||
import { Card } from '@/components/ui/card';
|
||||
import { Slider } from '@/components/ui/slider';
|
||||
import { Toggle } from '@/components/ui/toggle';
|
||||
import useUrlToMagnet from '@/hooks/useUrlToMagnet';
|
||||
|
||||
import useGenres from './useGenres';
|
||||
|
||||
export default function Filter() {
|
||||
useUrlToMagnet();
|
||||
|
||||
const { filters, setFilters } = useFilters();
|
||||
const { data: genres, isFetched } = useGenres();
|
||||
|
||||
const [years, setYears] = useState(filters.year || ['1990', new Date().getFullYear().toString()]);
|
||||
const [scores, setScores] = useState(filters.score || ['0', '10']);
|
||||
|
||||
const timeout = useRef<NodeJS.Timeout>();
|
||||
|
||||
const handleChangeGenre = (id: number) => () => {
|
||||
const genreId = id.toString();
|
||||
|
||||
setFilters({
|
||||
name: 'genres',
|
||||
value: filters.genres?.includes(genreId)
|
||||
? filters.genres.filter((id) => id !== genreId)
|
||||
: [...(filters.genres || []), genreId]
|
||||
});
|
||||
};
|
||||
|
||||
const handleDebouncedChange =
|
||||
(setter: Dispatch<SetStateAction<Array<string>>>, key: keyof Filters) => (value: Array<number>) => {
|
||||
clearTimeout(timeout.current);
|
||||
|
||||
timeout.current = setTimeout(() => setFilters({ name: key, value: value.map((value) => value.toString()) }), 300);
|
||||
|
||||
setter(value.map((value) => value.toString()));
|
||||
};
|
||||
|
||||
if (!isFetched) return;
|
||||
|
||||
return (
|
||||
<>
|
||||
<Card className='mb-2'>
|
||||
<ul className='flex gap-2 overflow-auto no-scrollbar'>
|
||||
{genres?.map((genre) => (
|
||||
<li key={genre.id}>
|
||||
<Toggle
|
||||
data-state={filters.genres?.includes(genre.id.toString()) ? 'on' : 'off'}
|
||||
onClick={handleChangeGenre(genre.id)}
|
||||
className='whitespace-nowrap'
|
||||
>
|
||||
{genre.name}
|
||||
</Toggle>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</Card>
|
||||
<div className='flex w-full my-4 gap-6 flex-wrap'>
|
||||
<div className='grow flex items-center basis-[250px]'>
|
||||
<Slider
|
||||
onValueChange={handleDebouncedChange(setYears, 'year')}
|
||||
value={years.map((value) => parseInt(value))}
|
||||
min={1960}
|
||||
max={new Date().getFullYear()}
|
||||
step={1}
|
||||
/>
|
||||
</div>
|
||||
<div className='grow flex items-center basis-[250px]'>
|
||||
<Slider
|
||||
onValueChange={handleDebouncedChange(setScores, 'score')}
|
||||
value={scores.map((value) => parseInt(value))}
|
||||
min={0}
|
||||
max={10}
|
||||
step={1}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -1,82 +1,45 @@
|
||||
'use client';
|
||||
|
||||
import { Dispatch, SetStateAction, useRef, useState } from 'react';
|
||||
import { useEffect } from 'react';
|
||||
import { createPortal } from 'react-dom';
|
||||
import { useAtom } from 'jotai';
|
||||
import { atomWithStorage } from 'jotai/utils';
|
||||
import { SlidersHorizontal } from 'lucide-react';
|
||||
|
||||
import useFilters from '@/app/(site)/useFilters';
|
||||
import { Card } from '@/components/ui/card';
|
||||
import { Slider } from '@/components/ui/slider';
|
||||
import { Toggle } from '@/components/ui/toggle';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import useMounted from '@/hooks/useMounted';
|
||||
import { genres } from '@/lib/api';
|
||||
import getQueryClient from '@/lib/queryClient';
|
||||
|
||||
import useGenres from './useGenres';
|
||||
import Filter from './Filter';
|
||||
|
||||
export default function Filter() {
|
||||
const { filters, setFilters } = useFilters();
|
||||
const { data: genres, isFetched } = useGenres();
|
||||
const isShowFilterAtom = atomWithStorage('is-show-filters', false);
|
||||
|
||||
const [years, setYears] = useState(filters.year || ['1990', new Date().getFullYear().toString()]);
|
||||
const [scores, setScores] = useState(filters.score || ['0', '10']);
|
||||
export default function FilterToggle() {
|
||||
const [isShowFilter, setIsShowFilter] = useAtom(isShowFilterAtom);
|
||||
const isMounted = useMounted();
|
||||
|
||||
const timeout = useRef<NodeJS.Timeout>();
|
||||
useEffect(() => {
|
||||
const queryClient = getQueryClient();
|
||||
|
||||
const handleChangeGenre = (id: number) => () => {
|
||||
const genreId = id.toString();
|
||||
|
||||
setFilters({
|
||||
name: 'genres',
|
||||
value: filters.genres?.includes(genreId)
|
||||
? filters.genres.filter((id) => id !== genreId)
|
||||
: [...(filters.genres || []), genreId]
|
||||
});
|
||||
};
|
||||
|
||||
const handleDebouncedChange =
|
||||
(setter: Dispatch<SetStateAction<Array<string>>>, key: keyof Filters) => (value: Array<number>) => {
|
||||
clearTimeout(timeout.current);
|
||||
|
||||
timeout.current = setTimeout(() => setFilters({ name: key, value: value.map((value) => value.toString()) }), 300);
|
||||
|
||||
setter(value.map((value) => value.toString()));
|
||||
};
|
||||
|
||||
if (!isFetched) return;
|
||||
queryClient.prefetchQuery({ queryKey: ['genres'], queryFn: () => genres() });
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Card className='mb-2'>
|
||||
<ul className='flex gap-2 overflow-auto no-scrollbar'>
|
||||
{genres?.map((genre) => (
|
||||
<li key={genre.id}>
|
||||
<Toggle
|
||||
data-state={filters.genres?.includes(genre.id.toString()) ? 'on' : 'off'}
|
||||
onClick={handleChangeGenre(genre.id)}
|
||||
className='whitespace-nowrap'
|
||||
>
|
||||
{genre.name}
|
||||
</Toggle>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</Card>
|
||||
<div className='flex w-full my-4 gap-6 flex-wrap'>
|
||||
<div className='grow flex items-center basis-[250px]'>
|
||||
<Slider
|
||||
onValueChange={handleDebouncedChange(setYears, 'year')}
|
||||
value={years.map((value) => parseInt(value))}
|
||||
min={1960}
|
||||
max={new Date().getFullYear()}
|
||||
step={1}
|
||||
/>
|
||||
</div>
|
||||
<div className='grow flex items-center basis-[250px]'>
|
||||
<Slider
|
||||
onValueChange={handleDebouncedChange(setScores, 'score')}
|
||||
value={scores.map((value) => parseInt(value))}
|
||||
min={0}
|
||||
max={10}
|
||||
step={1}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
{isShowFilter && <Filter />}
|
||||
{isMounted &&
|
||||
createPortal(
|
||||
<Button
|
||||
className='p-2'
|
||||
onClick={() => setIsShowFilter((isShow) => !isShow)}
|
||||
title={isShowFilter ? 'Hide filters' : 'Show filters'}
|
||||
variant='ghost'
|
||||
>
|
||||
<SlidersHorizontal size={19} />
|
||||
</Button>,
|
||||
document.getElementById('actions')!
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
+4
-120
@@ -1,139 +1,23 @@
|
||||
'use client';
|
||||
|
||||
import { forwardRef, ReactNode, UIEvent, useEffect, useRef, useState } from 'react';
|
||||
import { createPortal } from 'react-dom';
|
||||
import { Virtuoso, VirtuosoProps } from 'react-virtuoso';
|
||||
import { useAtom } from 'jotai';
|
||||
import { atomWithStorage } from 'jotai/utils';
|
||||
import { SlidersHorizontal } from 'lucide-react';
|
||||
|
||||
import Card from '@/components/Movie/Card';
|
||||
import Preview from '@/components/Movie/Preview';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import useMounted from '@/hooks/useMounted';
|
||||
import useUrlToMagnet from '@/hooks/useUrlToMagnet';
|
||||
import { genres } from '@/lib/api';
|
||||
import getQueryClient from '@/lib/queryClient';
|
||||
|
||||
import { Carousel, Measurer } from './components/Carousel';
|
||||
import Filter from './components/Filter';
|
||||
import useRandomMovie from './useRandomMovie';
|
||||
|
||||
const isShowFilterAtom = atomWithStorage('is-show-filters', false);
|
||||
|
||||
const components: VirtuosoProps<Movie, number>['components'] = {
|
||||
Item: (props) => <div {...props} className='shrink-0' style={{ ...props.style, width: props.context }} />,
|
||||
List: forwardRef(function List(props, ref) {
|
||||
return (
|
||||
<div {...props} ref={ref} className='gap-2' style={{ ...props.style, display: 'flex', width: props.context }} />
|
||||
);
|
||||
}),
|
||||
Scroller: forwardRef(function Scroller(props, ref) {
|
||||
return <div {...props} ref={ref} className='overflow-hidden no-scrollbar snap-mandatory snap-x rounded-lg' />;
|
||||
})
|
||||
};
|
||||
|
||||
const Measurer = ({ children }: { children: (width: number) => ReactNode }) => {
|
||||
const [ref, setRef] = useState<HTMLDivElement | null>(null);
|
||||
const width = ref?.offsetWidth;
|
||||
|
||||
return (
|
||||
<>
|
||||
<div ref={setRef} />
|
||||
{children(width || 0)}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
const Carousel = ({
|
||||
movies,
|
||||
index = 0,
|
||||
width: initialWidth,
|
||||
onIndexChange
|
||||
}: {
|
||||
movies?: Movie[];
|
||||
width: number;
|
||||
index: number;
|
||||
onIndexChange: (index: number) => void;
|
||||
}) => {
|
||||
const timeout = useRef<NodeJS.Timeout>();
|
||||
const isMobile = matchMedia('(max-width: 500px)').matches;
|
||||
|
||||
const width = isMobile ? initialWidth : 300;
|
||||
const height = isMobile ? (width * 3) / 2 : 450;
|
||||
|
||||
const handleScroll = (event: UIEvent) => {
|
||||
clearTimeout(timeout.current);
|
||||
|
||||
timeout.current = setTimeout(() => {
|
||||
const gap = 8;
|
||||
const item = event.target as HTMLDivElement;
|
||||
const itemWidth = item?.clientWidth + gap;
|
||||
const scrollPosition = item?.scrollLeft;
|
||||
|
||||
const index = Math.round(scrollPosition / itemWidth);
|
||||
|
||||
onIndexChange(index);
|
||||
}, 50);
|
||||
};
|
||||
|
||||
return (
|
||||
<Virtuoso
|
||||
data={movies}
|
||||
firstItemIndex={index}
|
||||
style={{ width, height, flexShrink: 0 }}
|
||||
totalCount={movies?.length}
|
||||
horizontalDirection
|
||||
context={width}
|
||||
// onScroll={handleScroll}
|
||||
components={components}
|
||||
itemContent={(index) => {
|
||||
clearTimeout(timeout.current);
|
||||
|
||||
timeout.current = setTimeout(() => {
|
||||
console.log(index);
|
||||
// onIndexChange(index);
|
||||
}, 100);
|
||||
return <Card className='snap-start' movie={movies?.[index]} />;
|
||||
}}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
export default function Container() {
|
||||
const [isShowFilter, setIsShowFilter] = useAtom(isShowFilterAtom);
|
||||
const { movies, movie, index, onIndexChange } = useRandomMovie();
|
||||
const isMounted = useMounted();
|
||||
const initialIndex = useRef(index);
|
||||
|
||||
useUrlToMagnet();
|
||||
|
||||
useEffect(() => {
|
||||
const queryClient = getQueryClient();
|
||||
|
||||
queryClient.prefetchQuery({ queryKey: ['genres'], queryFn: () => genres() });
|
||||
}, []);
|
||||
const { movies, movie, onIndexChange } = useRandomMovie();
|
||||
|
||||
return (
|
||||
<>
|
||||
{isShowFilter && <Filter />}
|
||||
{isMounted &&
|
||||
createPortal(
|
||||
<Button
|
||||
className='p-2'
|
||||
onClick={() => setIsShowFilter((isShow) => !isShow)}
|
||||
title={isShowFilter ? 'Hide filters' : 'Show filters'}
|
||||
variant='ghost'
|
||||
>
|
||||
<SlidersHorizontal size={19} />
|
||||
</Button>,
|
||||
document.getElementById('actions')!
|
||||
)}
|
||||
<Filter />
|
||||
<Measurer>
|
||||
{(width) => (
|
||||
<Preview
|
||||
movie={movie}
|
||||
className='bg-background rounded-md'
|
||||
card={<Carousel index={initialIndex.current} width={width} movies={movies} onIndexChange={onIndexChange} />}
|
||||
card={<Carousel width={width} movies={movies} onIndexChange={onIndexChange} />}
|
||||
/>
|
||||
)}
|
||||
</Measurer>
|
||||
|
||||
@@ -6,5 +6,12 @@ import List from '@/components/Movie/List';
|
||||
export default function Container() {
|
||||
const { pages } = useFavorites();
|
||||
|
||||
if (!pages[0]?.results?.length)
|
||||
return (
|
||||
<div className='h-40 w-full flex items-center justify-center text-xl text-muted-foreground'>
|
||||
There are no items in your favorites list
|
||||
</div>
|
||||
);
|
||||
|
||||
return <List pages={pages} />;
|
||||
}
|
||||
@@ -1,5 +1,13 @@
|
||||
import { Metadata } from 'next';
|
||||
|
||||
import Container from './container';
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: 'My Favorite Shows - Movie Advisor',
|
||||
description:
|
||||
'View your favorite shows on Movie Advisor. Keep track of films you’ve added and revisit your top picks anytime.'
|
||||
};
|
||||
|
||||
export default function Favorites() {
|
||||
return <Container />;
|
||||
}
|
||||
@@ -43,4 +43,15 @@ export const useFavoritesMutation = () => {
|
||||
};
|
||||
};
|
||||
|
||||
export const useFavoriteToggle = (movie: Movie) => {
|
||||
const { add, remove } = useFavoritesMutation();
|
||||
const { map } = useFavorites();
|
||||
|
||||
const isFavorite = map.has(movie.id);
|
||||
|
||||
const toggle = () => (isFavorite ? remove(movie.id) : add(movie));
|
||||
|
||||
return { isFavorite, toggle };
|
||||
};
|
||||
|
||||
export default useFavorites;
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
import { dehydrate, HydrationBoundary } from '@tanstack/react-query';
|
||||
|
||||
import { randomMovies } from '@/lib/api';
|
||||
import { randomMovies, trendingMovies } from '@/lib/api';
|
||||
import getQueryClient from '@/lib/queryClient';
|
||||
|
||||
import Container from './container';
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
import { dehydrate, HydrationBoundary } from '@tanstack/react-query';
|
||||
import { Metadata } from 'next';
|
||||
|
||||
import { popularMovies } from '@/lib/api';
|
||||
import getQueryClient from '@/lib/queryClient';
|
||||
|
||||
import Container from './container';
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: 'Popular Movies | Movie Advisor',
|
||||
description:
|
||||
'Check out the most popular movies right now on Movie Advisor. See what’s trending and don’t miss out on the latest hits.'
|
||||
};
|
||||
|
||||
export default async function Popular() {
|
||||
const queryClient = getQueryClient();
|
||||
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
import { dehydrate, HydrationBoundary } from '@tanstack/react-query';
|
||||
import { Metadata } from 'next';
|
||||
|
||||
import { topMovies } from '@/lib/api';
|
||||
import getQueryClient from '@/lib/queryClient';
|
||||
|
||||
import Container from './container';
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: 'Top Rated Movies | Movie Advisor',
|
||||
description:
|
||||
'Discover the top-rated movies on Movie Advisor. Find the highest-rated films and make informed viewing choices.'
|
||||
};
|
||||
|
||||
export default async function Top() {
|
||||
const queryClient = getQueryClient();
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { startTransition, useCallback, useEffect, useState } from 'react';
|
||||
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
|
||||
import useFilters from '@/app/(site)/useFilters';
|
||||
@@ -49,7 +49,7 @@ const useRandomMovie = () => {
|
||||
refetch();
|
||||
}
|
||||
previousIndexes[JSON.stringify(filters)] = index;
|
||||
setIndex(index);
|
||||
startTransition(() => setIndex(index));
|
||||
},
|
||||
[movies, filters]
|
||||
),
|
||||
|
||||
+4
-3
@@ -8,8 +8,9 @@ import Providers from '@/providers';
|
||||
import '@/styles/index.css';
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: 'Movie advisor',
|
||||
description: 'It is going to help you find a movie for evening'
|
||||
title: 'Movie advisor | Discover Your Next Favorite Movie',
|
||||
description:
|
||||
'Explore a diverse selection of movies with Movie Advisor. Get random movie recommendations and find out top-rated, popular, and user-favorite films.'
|
||||
};
|
||||
|
||||
export const viewport: Viewport = {
|
||||
@@ -36,7 +37,7 @@ export default function RootLayout({ children }: { children: ReactNode }) {
|
||||
<div className='sticky top-0 z-20'>
|
||||
<Header />
|
||||
</div>
|
||||
<main className='px-2 max-w-[1252px] mx-auto'>{children}</main>
|
||||
<main className='px-2 max-w-[1240px] mx-auto'>{children}</main>
|
||||
</Providers>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in new issue
Block a user