diff --git a/components/ScrollNavigation/index.tsx b/components/ScrollNavigation/index.tsx index 64d3c29..5b10f22 100644 --- a/components/ScrollNavigation/index.tsx +++ b/components/ScrollNavigation/index.tsx @@ -33,6 +33,9 @@ function ScrollNavigation({ children }: Props) { const [scrollElement, setScrollElement] = useState(null); + const scrollElementRef = useRef(null); + const containerRef = useRef(null); + const isArrowHoveredRef = useRef(false); const onEndReachedRef = useRef(onEndReached); const endReachedFiredRef = useRef(false); @@ -43,6 +46,10 @@ function ScrollNavigation({ const [isArrowHovered, setIsArrowHovered] = useState(false); + useEffect(() => { + scrollElementRef.current = scrollElement; + }, [scrollElement]); + const [childWidth, setChildWidth] = useState(0); const slideWidth = initialSlideWidth ?? childWidth + gap; @@ -92,6 +99,65 @@ function ScrollNavigation({ }; }, [scrollElement, initialSlideWidth]); + useEffect(() => { + const container = containerRef.current; + + if (!container) return; + + let accumulated = 0; + + const handleWheel = (event: WheelEvent) => { + if (!isArrowHoveredRef.current) return; + + const element = scrollElementRef.current; + + if (!element) return; + + const delta = event.deltaY; + + if (!delta) return; + + const atStart = element.scrollLeft <= 0; + const atEnd = element.scrollLeft + element.clientWidth >= element.scrollWidth - 1; + + if ((delta > 0 && atEnd) || (delta < 0 && atStart)) { + accumulated = 0; + return; + } + + event.preventDefault(); + + if (accumulated * delta < 0) accumulated = 0; + + accumulated += delta; + + const first = element.querySelector('.snap-start') as HTMLElement | null; + + if (!first) return; + + const second = first.nextElementSibling as HTMLElement | null; + const step = second ? second.offsetLeft - first.offsetLeft : first.clientWidth + gap; + + if (!step || Math.abs(accumulated) < step / 2) return; + + const index = Math.round(element.scrollLeft / step); + const direction = accumulated > 0 ? 1 : -1; + + accumulated = 0; + + element.scrollTo({ + left: (index + direction) * step, + behavior: 'smooth' + }); + }; + + container.addEventListener('wheel', handleWheel, { passive: false }); + + return () => { + container.removeEventListener('wheel', handleWheel); + }; + }, [gap]); + const handleScrollNext = (direction: 1 | -1) => () => { if (!scrollElement) return; @@ -102,7 +168,7 @@ function ScrollNavigation({ }; return ( -
+
{children({ setScrollElement, isArrowHovered })} {[ { @@ -127,8 +193,14 @@ function ScrollNavigation({
setIsArrowHovered(true)} - onMouseLeave={() => setIsArrowHovered(false)} + onMouseEnter={() => { + isArrowHoveredRef.current = true; + setIsArrowHovered(true); + }} + onMouseLeave={() => { + isArrowHoveredRef.current = false; + setIsArrowHovered(false); + }} className={cn( 'opacity-0 hover-none:hidden hover-none:pointer-events-none transition-opacity absolute top-1/2 transform -translate-y-1/2 bg-gradient-to-r h-full w-14 flex items-center cursor-pointer', { diff --git a/components/Trending/constants.ts b/components/Trending/constants.ts index 2262dd6..da3ed63 100644 --- a/components/Trending/constants.ts +++ b/components/Trending/constants.ts @@ -13,7 +13,8 @@ export const tabs = [ }, { title: 'Similar', - type: 'similar' + type: 'similar', + divider: true }, { title: 'Recommendations', @@ -34,9 +35,14 @@ export const seriesTabs = [ title: 'HBO', type: 'hbo' }, + { + title: 'Amazon', + type: 'amazon' + }, { title: 'Similar', - type: 'similar' + type: 'similar', + divider: true }, { title: 'Recommendations', diff --git a/components/Trending/index.tsx b/components/Trending/index.tsx index 03bc024..bc2eecb 100644 --- a/components/Trending/index.tsx +++ b/components/Trending/index.tsx @@ -39,6 +39,7 @@ const Trending: FC = ({ showId, showType, preview }) => { const apple = useTrending({ type: 'apple', enabled: isSeries && !preview }); const netflix = useTrending({ type: 'netflix', enabled: isSeries && active.type === 'netflix' }); const hbo = useTrending({ type: 'hbo', enabled: isSeries && active.type === 'hbo' }); + const amazon = useTrending({ type: 'amazon', enabled: isSeries && active.type === 'amazon' }); const similar = useSimilar({ showId, showType, @@ -52,7 +53,17 @@ const Trending: FC = ({ showId, showType, preview }) => { enabled: active.type === 'recommendations' }); - const tab = { streaming, trending, theater, apple, netflix, hbo, similar, recommendations }[active.type]!; + const tab = { + streaming, + trending, + theater, + apple, + netflix, + hbo, + amazon, + similar, + recommendations + }[active.type]!; return ( (streaming.isFetched || apple.isFetched || similar.isFetched) && ( diff --git a/components/ui/nav.tsx b/components/ui/nav.tsx index 3a53da9..3b317eb 100644 --- a/components/ui/nav.tsx +++ b/components/ui/nav.tsx @@ -1,8 +1,8 @@ -import { FC, ReactNode, useLayoutEffect, useRef, useState } from 'react'; +import { FC, Fragment, ReactNode, useLayoutEffect, useRef, useState } from 'react'; import { cn } from '@/lib/utils'; -type Tab = { title: string }; +type Tab = { title: string; divider?: boolean }; type CursorProps = { left: number; width: number; opacity: number; onTransitionEnd: () => void }; @@ -36,23 +36,25 @@ function Nav({ tabs, active, className, children, onChange }: Nav return (