feat: upgrade nextjs
This commit is contained in:
1 parent
750d5b12c8
commit
29f6de300b
16 files changed
+365
-344
No files matched your search
@@ -1,9 +1,10 @@
|
||||
'use client';
|
||||
|
||||
import { forwardRef, ReactNode, startTransition, useEffect, useRef, useState } from 'react';
|
||||
import { StateSnapshot, Virtuoso, VirtuosoHandle, VirtuosoProps } from 'react-virtuoso';
|
||||
import { forwardRef, ReactNode, startTransition, useEffect, useLayoutEffect, useRef, useState } from 'react';
|
||||
import { Virtuoso, VirtuosoHandle, VirtuosoProps } from 'react-virtuoso';
|
||||
|
||||
import Card from '@/components/Movie/Card';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
const components: VirtuosoProps<Movie, number>['components'] = {
|
||||
Item: (props) => <div {...props} className='shrink-0' style={{ ...props.style, width: props.context }} />,
|
||||
@@ -29,29 +30,55 @@ export const Measurer = ({ children }: { children: (width: number) => ReactNode
|
||||
);
|
||||
};
|
||||
|
||||
let snapshot: StateSnapshot;
|
||||
|
||||
export const Carousel = ({
|
||||
movies,
|
||||
width: initialWidth,
|
||||
index,
|
||||
onIndexChange
|
||||
}: {
|
||||
movies?: Movie[];
|
||||
width: number;
|
||||
index: number;
|
||||
onIndexChange: (index: number) => void;
|
||||
}) => {
|
||||
const timeout = useRef<NodeJS.Timeout>();
|
||||
const isMobile = matchMedia('(max-width: 500px)').matches;
|
||||
const ref = useRef<VirtuosoHandle>(null);
|
||||
const initialIndex = useRef(index);
|
||||
const [snap, setSnap] = useState(true);
|
||||
|
||||
const width = isMobile ? initialWidth : 300;
|
||||
const height = isMobile ? (width * 3) / 2 : 450;
|
||||
|
||||
useEffect(() => {
|
||||
const virtuoso = ref.current;
|
||||
return () => virtuoso?.getState((state) => (snapshot = state));
|
||||
useLayoutEffect(() => {
|
||||
if (!ref.current) return;
|
||||
|
||||
ref.current.scrollIntoView({ index: initialIndex.current });
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const scrollTo = (event: globalThis.KeyboardEvent) => {
|
||||
if (event.key === 'ArrowLeft') {
|
||||
if (index <= 0) return;
|
||||
setSnap(false);
|
||||
ref.current?.scrollIntoView({ index: index - 1, behavior: 'smooth' });
|
||||
}
|
||||
|
||||
if (event.key === 'ArrowRight') {
|
||||
if (index >= (movies?.length || 0) - 1) return;
|
||||
setSnap(false);
|
||||
ref.current?.scrollIntoView({ index: index + 1, behavior: 'smooth' });
|
||||
}
|
||||
};
|
||||
|
||||
document.addEventListener('keydown', scrollTo);
|
||||
|
||||
return () => {
|
||||
document.removeEventListener('keydown', scrollTo);
|
||||
setTimeout(() => setSnap(true), 100);
|
||||
};
|
||||
}, [index, movies?.length]);
|
||||
|
||||
return (
|
||||
<Virtuoso
|
||||
ref={ref}
|
||||
@@ -62,7 +89,6 @@ export const Carousel = ({
|
||||
horizontalDirection
|
||||
context={width}
|
||||
increaseViewportBy={154}
|
||||
restoreStateFrom={snapshot}
|
||||
components={components}
|
||||
itemContent={(index) => {
|
||||
clearTimeout(timeout.current);
|
||||
@@ -73,7 +99,7 @@ export const Carousel = ({
|
||||
startTransition(() => onIndexChange(currentIndex));
|
||||
}, 50);
|
||||
|
||||
return <Card className='snap-start' movie={movies?.[index]} />;
|
||||
return <Card className={cn(snap && 'snap-start')} movie={movies?.[index]} />;
|
||||
}}
|
||||
/>
|
||||
);
|
||||
|
||||
+22
-11
@@ -1,27 +1,38 @@
|
||||
'use client';
|
||||
|
||||
import { motion, Variants } from 'framer-motion';
|
||||
|
||||
import Preview from '@/components/Movie/Preview';
|
||||
|
||||
import { Carousel, Measurer } from './components/Carousel';
|
||||
import Filter from './components/Filter';
|
||||
import useRandomMovie from './useRandomMovie';
|
||||
|
||||
const container: Variants = {
|
||||
hidden: { y: -20, opacity: 0 },
|
||||
visible: {
|
||||
y: 0,
|
||||
opacity: 1,
|
||||
transition: {
|
||||
delay: 0.1
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default function Container() {
|
||||
const { movies, movie, onIndexChange } = useRandomMovie();
|
||||
const { movies, movie, index, onIndexChange } = useRandomMovie();
|
||||
|
||||
return (
|
||||
<>
|
||||
<Filter />
|
||||
<Measurer>
|
||||
{(width) => (
|
||||
<Measurer>
|
||||
{(width) => (
|
||||
<motion.div variants={container} initial='hidden' animate='visible'>
|
||||
<Preview
|
||||
movie={movie}
|
||||
className='bg-background rounded-md'
|
||||
card={<Carousel width={width} movies={movies} onIndexChange={onIndexChange} />}
|
||||
card={<Carousel index={index} width={width} movies={movies} onIndexChange={onIndexChange} />}
|
||||
/>
|
||||
)}
|
||||
</Measurer>
|
||||
<div className='w-full h-2 max-sm:h-12' />
|
||||
</>
|
||||
<div className='w-full h-2 max-sm:h-12' />
|
||||
</motion.div>
|
||||
)}
|
||||
</Measurer>
|
||||
);
|
||||
}
|
||||
@@ -2,13 +2,15 @@
|
||||
|
||||
import useFavorites from '@/app/(site)/favorites/useFavorites';
|
||||
import List from '@/components/Movie/List';
|
||||
import useMounted from '@/hooks/useMounted';
|
||||
|
||||
export default function Container() {
|
||||
const { pages } = useFavorites();
|
||||
const isMounted = useMounted();
|
||||
|
||||
if (!pages[0]?.results?.length)
|
||||
if (isMounted && !pages[0]?.results?.length)
|
||||
return (
|
||||
<div className='h-40 w-full flex items-center justify-center text-xl text-muted-foreground'>
|
||||
<div className='h-40 w-full flex items-center text-center justify-center text-xl text-muted-foreground'>
|
||||
There are no items in your favorites list
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -8,6 +8,8 @@ export const metadata: Metadata = {
|
||||
'View your favorite shows on Movie Advisor. Keep track of films you’ve added and revisit your top picks anytime.'
|
||||
};
|
||||
|
||||
export const revalidate = 3600;
|
||||
|
||||
export default function Favorites() {
|
||||
return <Container />;
|
||||
}
|
||||
+3
-6
@@ -1,17 +1,14 @@
|
||||
import { dehydrate, HydrationBoundary } from '@tanstack/react-query';
|
||||
|
||||
import { randomMovies, trendingMovies } from '@/lib/api';
|
||||
import { randomMovies } from '@/lib/api';
|
||||
import getQueryClient from '@/lib/queryClient';
|
||||
|
||||
import Container from './container';
|
||||
import { mapFilters } from './useFilters';
|
||||
|
||||
export default async function Random({ searchParams }: { searchParams: Record<string, string> }) {
|
||||
export default async function Random() {
|
||||
const queryClient = getQueryClient();
|
||||
|
||||
const filters = mapFilters(searchParams || {});
|
||||
|
||||
await queryClient.prefetchQuery({ queryKey: ['random-movie', filters], queryFn: () => randomMovies({ filters }) });
|
||||
await queryClient.prefetchQuery({ queryKey: ['random-movie'], queryFn: () => randomMovies({ filters: {} }) });
|
||||
|
||||
return (
|
||||
<HydrationBoundary state={dehydrate(queryClient)}>
|
||||
|
||||
@@ -12,6 +12,8 @@ export const metadata: Metadata = {
|
||||
'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 const revalidate = 3600;
|
||||
|
||||
export default async function Popular() {
|
||||
const queryClient = getQueryClient();
|
||||
|
||||
|
||||
@@ -12,6 +12,8 @@ export const metadata: Metadata = {
|
||||
'Discover the top-rated movies on Movie Advisor. Find the highest-rated films and make informed viewing choices.'
|
||||
};
|
||||
|
||||
export const revalidate = 3600;
|
||||
|
||||
export default async function Top() {
|
||||
const queryClient = getQueryClient();
|
||||
|
||||
|
||||
@@ -1,16 +1,9 @@
|
||||
'use client';
|
||||
|
||||
import { useMemo } from 'react';
|
||||
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
|
||||
|
||||
export const mapFilters = (filters: Record<string, string>) => {
|
||||
try {
|
||||
return Object.entries(filters).reduce((filters, [key, value]) => {
|
||||
filters[key as keyof Filters] = value.split(',');
|
||||
return filters;
|
||||
}, {} as Partial<Filters>);
|
||||
} catch (e) {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
import { mapFilters } from '@/lib/utils';
|
||||
|
||||
const useFilters = () => {
|
||||
const router = useRouter();
|
||||
|
||||
Reference in new issue
Block a user