diff --git a/api/index.ts b/api/index.ts index c3ee2fa..e845769 100644 --- a/api/index.ts +++ b/api/index.ts @@ -153,9 +153,7 @@ export const popularMovies = async ({ page }: { page?: string } = {}): Promise

void; +interface Context { + onClick?: (id: string) => void; + width?: number; + height?: number; + gap?: number; } -const Credits: FC = ({ showId, showType = 'movie', onPersonClick }) => { +interface Props extends Context { + showId: Show['id']; + showType?: Show['type']; +} + +const Credits: FC = ({ showId, showType = 'movie', onClick, width = 120, height = 180, gap = 8 }) => { const { data: credits } = useCredits({ showId, showType }); + const context = useMemo(() => ({ width, height, gap, onClick }), [width, height, gap, onClick]); + + if (!credits?.length) return null; + return ( - Boolean(credits?.length) && ( - > - {(setRef) => ( -

- )} - - ) + ); }; +const components: VirtuosoProps['components'] = { + Item: ({ item: actor, children, context, ...props }) => { + if (!actor) return null; + + return ( +
+ context?.onClick?.(actor.id.toString())} + > +
+ {actor.name} +
+
+

{actor.name}

+ {Boolean(actor.character) &&

{actor.character}

} +
+ +
+ ); + }, + List: forwardRef(function List({ context, ...props }, ref) { + return
; + }), + Scroller: forwardRef(function Scroller({ context, ...props }, ref) { + const slideWidth = (context?.width || 0) + (context?.gap || 0); + + const handleRef = + (ref: ForwardedRef, setRef: (element: HTMLDivElement) => void) => + (element: HTMLDivElement | null) => { + if (!element || !ref) return; + + setRef(element); + + typeof ref === 'function' ? ref(element) : (ref.current = element); + }; + + return ( +
+ slideWidth={slideWidth}> + {({ setScrollElement, isArrowHovered }) => ( +
+ )} + +
+ ); + }) +}; + export default Credits; diff --git a/components/Preview/components/Torrents/components/Player/index.tsx b/components/Preview/components/Torrents/components/Player/index.tsx index 18ae88d..69fef97 100644 --- a/components/Preview/components/Torrents/components/Player/index.tsx +++ b/components/Preview/components/Torrents/components/Player/index.tsx @@ -2,7 +2,6 @@ import { useState } from 'react'; import { create, InstanceProps } from 'react-modal-promise'; import { Loader } from 'lucide-react'; import dynamic from 'next/dynamic'; -import Image from 'next/image'; import { Modal } from '@/components/ui/dialog'; import { useToast } from '@/hooks/useToast'; @@ -48,7 +47,7 @@ const showPlayer = create(({ onResolve, magnet, backdrop, title }: Props) => { {(!isReady || isLoading) && (
{title} -
+
diff --git a/components/Preview/index.tsx b/components/Preview/index.tsx index 6a6e5ec..fa5dfb8 100644 --- a/components/Preview/index.tsx +++ b/components/Preview/index.tsx @@ -56,7 +56,7 @@ const Preview: FC = ({ show: baseShow, className, onClose, poster }) => { {show.overview}

- +
diff --git a/components/Random/components/Carousel/index.tsx b/components/Random/components/Carousel/index.tsx index 91fb89e..62ff43d 100644 --- a/components/Random/components/Carousel/index.tsx +++ b/components/Random/components/Carousel/index.tsx @@ -1,14 +1,46 @@ 'use client'; -import { FC, ForwardedRef, forwardRef, useLayoutEffect, useRef } from 'react'; +import { FC, ForwardedRef, forwardRef, useLayoutEffect, useMemo, useRef } from 'react'; import { Virtuoso, VirtuosoProps } from 'react-virtuoso'; import Poster from '@/components/Poster'; import ScrollNavigation from '@/components/ScrollNavigation'; import { cn } from '@/lib/utils'; -const components: VirtuosoProps void>['components'] = { - Item: ({ item, children, context: onIndexChange, ...props }) => { +interface Context { + onIndexChange: (index: number) => void; + gap?: number; +} + +interface Props extends Context { + index: number; + shows?: Show[]; + onEndReached?: () => void; +} + +const Carousel: FC = ({ index, shows, gap = 8, onIndexChange, onEndReached }) => { + const initialIndex = useRef(index); + + const context = useMemo(() => ({ onIndexChange, gap }), [onIndexChange, gap]); + + return ( +
+ +
+ ); +}; + +const components: VirtuosoProps['components'] = { + Item: ({ item, children, context, ...props }) => { const ref = useRef(null); const index = props['data-index']; @@ -22,7 +54,7 @@ const components: VirtuosoProps void>['components'] = { entries.forEach((entry) => { if (!entry.isIntersecting) return; - onIndexChange?.(index); + context?.onIndexChange?.(index); }), { threshold: 0.7 @@ -32,7 +64,7 @@ const components: VirtuosoProps void>['components'] = { observer.observe(container); return () => observer.disconnect(); - }, [onIndexChange, index]); + }, [context, index]); return (
@@ -41,11 +73,9 @@ const components: VirtuosoProps void>['components'] = { ); }, List: forwardRef(function List({ context, ...props }, ref) { - return
; + return
; }), Scroller: forwardRef(function Scroller({ context, ...props }, ref) { - const GAP_BETWEEN_SLIDES = 8; // gap-2 = 0.5rem = 8px - const handleRef = (ref: ForwardedRef, setRef: (element: HTMLDivElement) => void) => (element: HTMLDivElement | null) => { @@ -62,12 +92,12 @@ const components: VirtuosoProps void>['components'] = { arrowsClassName='h-20 bg-secondary' backClassName='rounded-none rounded-r-full' nextClassName='rounded-none rounded-l-full' - gap={GAP_BETWEEN_SLIDES} + gap={context?.gap} > - {(setRef, isArrowHovered) => ( + {({ setScrollElement, isArrowHovered }) => (
void>['components'] = { }) }; -interface Props { - index: number; - shows?: Show[]; - onIndexChange: (index: number) => void; - onEndReached?: () => void; -} - -const Carousel: FC = ({ index, shows, onIndexChange, onEndReached }) => { - const initialIndex = useRef(index); - - return ( -
- -
- ); -}; - export default Carousel; diff --git a/components/ScrollNavigation/index.tsx b/components/ScrollNavigation/index.tsx index 18e45db..5a8402a 100644 --- a/components/ScrollNavigation/index.tsx +++ b/components/ScrollNavigation/index.tsx @@ -9,8 +9,15 @@ interface Props { backClassName?: string; nextClassName?: string; iconClassName?: string; + slideWidth?: number; gap?: number; - children: (setScrollElement: (element: E) => void, isArrowHovered: boolean) => ReactNode; + children: ({ + setScrollElement, + isArrowHovered + }: { + setScrollElement: (element: E) => void; + isArrowHovered: boolean; + }) => ReactNode; } function ScrollNavigation({ @@ -19,28 +26,36 @@ function ScrollNavigation({ backClassName, nextClassName, iconClassName, + slideWidth: initialSlideWidth, gap = 0, children }: Props) { const [scrollElement, setScrollElement] = useState(null); + const [isShowBack, setIsShowBack] = useState(true); const [isShowNext, setIsShowNext] = useState(true); - const [childWidth, setChildWidth] = useState(0); + const [isArrowHovered, setIsArrowHovered] = useState(false); + const [childWidth, setChildWidth] = useState(0); + + const slideWidth = initialSlideWidth ?? childWidth + gap; + useEffect(() => { if (!scrollElement) return; let timeout: NodeJS.Timeout; - if (scrollElement.firstChild) { - setChildWidth((scrollElement.firstChild as HTMLElement).clientWidth + gap); + const slide = scrollElement.firstChild as HTMLElement; + + if (!initialSlideWidth && slide) { + setChildWidth(slide.clientWidth); } const handleScroll = () => { clearTimeout(timeout); - const DELAY = 100; + const DELAY = 50; timeout = setTimeout(() => { setIsShowBack(Boolean(scrollElement.scrollLeft)); @@ -55,17 +70,20 @@ function ScrollNavigation({ return () => { scrollElement.removeEventListener('scroll', handleScroll); }; - }, [scrollElement, gap]); + }, [scrollElement, initialSlideWidth]); const handleScrollNext = (direction: 1 | -1) => () => { if (!scrollElement) return; - scrollElement.scrollTo({ left: scrollElement.scrollLeft + childWidth * direction, behavior: 'smooth' }); + scrollElement.scrollTo({ + left: scrollElement.scrollLeft + slideWidth * direction, + behavior: 'smooth' + }); }; return (
- {children(setScrollElement, isArrowHovered)} + {children({ setScrollElement, isArrowHovered })} {[ { Arrow: ChevronLeft, diff --git a/components/Trending/components/Gallery/index.tsx b/components/Trending/components/Gallery/index.tsx index b762ed6..e918957 100644 --- a/components/Trending/components/Gallery/index.tsx +++ b/components/Trending/components/Gallery/index.tsx @@ -10,9 +10,9 @@ interface Props { const Gallery: FC = ({ shows }) => ( > - {(setRef) => ( + {({ setScrollElement }) => (
    {shows.map((show) => (