diff --git a/app/(site)/components/Carousel/index.tsx b/app/(site)/components/Carousel/index.tsx index 84b7100..8962f24 100644 --- a/app/(site)/components/Carousel/index.tsx +++ b/app/(site)/components/Carousel/index.tsx @@ -1,70 +1,33 @@ 'use client'; -import { forwardRef, ReactNode, startTransition, useEffect, useLayoutEffect, useRef, useState } from 'react'; -import { Virtuoso, VirtuosoHandle, VirtuosoProps } from 'react-virtuoso'; +import { forwardRef, useEffect, useLayoutEffect, useRef } from 'react'; +import { StateSnapshot, Virtuoso, VirtuosoHandle, VirtuosoProps } from 'react-virtuoso'; import Card from '@/components/Movie/Card'; -import { cn } from '@/lib/utils'; -const components: VirtuosoProps['components'] = { - Item: (props) =>
, - List: forwardRef(function List(props, ref) { - return ( -
- ); - }), - Scroller: forwardRef(function Scroller(props, ref) { - return
; - }) -}; +let snapshot: StateSnapshot; -export const Measurer = ({ children }: { children: (width: number) => ReactNode }) => { - const [ref, setRef] = useState(null); - const width = ref?.offsetWidth; - - return ( - <> -
- {children(width || 0)} - - ); -}; - -export const Carousel = ({ +const Carousel = ({ index, movies, - width: initialWidth, onIndexChange }: { index: number; movies?: Movie[]; - width: number; onIndexChange: (index: number) => void; }) => { - const timeout = useRef(); - const isMobile = matchMedia('(max-width: 500px)').matches; const ref = useRef(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; - useLayoutEffect(() => { - if (!ref.current) return; + if (!virtuoso) return; - ref.current.scrollIntoView({ index: initialIndex.current }); + return () => virtuoso.getState((state) => (snapshot = state)); }, []); useEffect(() => { - let pressed = false; - const scrollTo = ({ key }: globalThis.KeyboardEvent) => { - if (['ArrowLeft', 'ArrowRight'].includes(key)) { - pressed = true; - setSnap(false); - } - if (key === 'ArrowLeft') { if (index <= 0) return; ref.current?.scrollIntoView({ index: index - 1, behavior: 'smooth' }); @@ -80,32 +43,66 @@ export const Carousel = ({ return () => { document.removeEventListener('keydown', scrollTo); - pressed && setTimeout(() => setSnap(true), 100); }; }, [index, movies?.length]); return ( - { - clearTimeout(timeout.current); - - timeout.current = setTimeout(() => { - const currentIndex = index - 1; - - startTransition(() => onIndexChange(currentIndex)); - }, 50); - - return ; - }} - /> +
+ +
); }; + +const components: VirtuosoProps void>['components'] = { + Item: ({ item, children, context: onIndexChange, ...props }) => { + const ref = useRef(null); + const index = props['data-index']; + + useLayoutEffect(() => { + const container = ref.current; + + if (!container) return; + + const observer = new IntersectionObserver( + (entries) => + entries.forEach((entry) => { + if (!entry.isIntersecting) return; + + onIndexChange?.(index); + }), + { + threshold: 0.7 + } + ); + + observer.observe(container); + + return () => observer.disconnect(); + }, [onIndexChange, index]); + + return ( +
+ +
+ ); + }, + List: forwardRef(function List({ context, ...props }, ref) { + return
; + }), + Scroller: forwardRef(function Scroller({ context, ...props }, ref) { + return ( +
+ ); + }) +}; + +export default Carousel; diff --git a/app/(site)/container.tsx b/app/(site)/container.tsx index 80207e7..e0aeb66 100644 --- a/app/(site)/container.tsx +++ b/app/(site)/container.tsx @@ -2,24 +2,20 @@ import Preview from '@/components/Movie/Preview'; -import { Carousel, Measurer } from './components/Carousel'; +import Carousel from './components/Carousel'; import useRandomMovie from './useRandomMovie'; export default function Container() { const { movies, movie, index, onIndexChange } = useRandomMovie(); return ( - - {(width) => ( - <> - } - /> -
- - )} - + <> + } + /> +
+ ); } diff --git a/app/(site)/useRandomMovie.ts b/app/(site)/useRandomMovie.ts index 5b06f68..cd90b25 100644 --- a/app/(site)/useRandomMovie.ts +++ b/app/(site)/useRandomMovie.ts @@ -1,27 +1,24 @@ 'use client'; -import { startTransition, useCallback, useEffect, useState } from 'react'; +import { startTransition, useCallback, useState } from 'react'; import { useQuery, useQueryClient } from '@tanstack/react-query'; -import useFilters from '@/app/(site)/useFilters'; import { randomMovies } from '@/lib/api'; const UPDATE_RATE = 10; -const previousIndexes: Record = {}; +let previousIndex: number = 0; const useRandomMovie = () => { const queryClient = useQueryClient(); - const { filters } = useFilters(); - const indexByFilter = previousIndexes[JSON.stringify(filters)]; - const [index, setIndex] = useState(indexByFilter); + const [index, setIndex] = useState(previousIndex); const { data: movies, refetch } = useQuery({ - queryKey: ['random-movie', filters], - queryFn: () => randomMovies({ filters }), + queryKey: ['random-movie'], + queryFn: () => randomMovies({ filters: {} }), structuralSharing(_, newData) { const uniqueIds: Record = {}; - const current = queryClient.getQueryData(['random-movie', filters]) || []; + const current = queryClient.getQueryData(['random-movie']) || []; const next = Array.isArray(newData) ? newData : [newData]; return [...current, ...next].reduce((unique, movie) => { @@ -35,10 +32,6 @@ const useRandomMovie = () => { } }); - useEffect(() => { - setIndex(indexByFilter || 0); - }, [indexByFilter]); - return { movies, movie: movies?.[index], @@ -48,10 +41,10 @@ const useRandomMovie = () => { if ((index && index % UPDATE_RATE === 0) || movies!.length < UPDATE_RATE) { refetch(); } - previousIndexes[JSON.stringify(filters)] = index; + previousIndex = index; startTransition(() => setIndex(index)); }, - [movies, filters] + [movies] ), index }; diff --git a/app/layout.tsx b/app/layout.tsx index 9888688..7525178 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -1,8 +1,12 @@ import React, { ReactNode } from 'react'; import type { Metadata, Viewport } from 'next'; +import { Poppins } from 'next/font/google'; import { cookies } from 'next/headers'; +const poppins = Poppins({ subsets: ['latin'], weight: '400' }); + import Header from '@/components/Header'; +import { cn } from '@/lib/utils'; import Providers from '@/providers'; import '@/styles/index.css'; @@ -28,16 +32,18 @@ export default function RootLayout({ children }: { children: ReactNode }) { - + {/* - + */}
-
{children}
+
+ {children} +
diff --git a/components/Header/components/Navigation/index.tsx b/components/Header/components/Navigation/index.tsx index 8f9da1c..5ef690c 100644 --- a/components/Header/components/Navigation/index.tsx +++ b/components/Header/components/Navigation/index.tsx @@ -21,7 +21,7 @@ export function MobileNavigation() { const currentPath = usePathname(); return ( -
+