Persist the show-type filter in the path via a /series prefix: movies keep bare URLs, series use /series across random, popular, top, favorites and watchlist. The path is the single source of truth - the jotai atom is removed and the toggle navigates with router.replace so the server prefetches the target variant. Legacy ?type=tv deep links redirect to the prefixed URLs. Trending and similar/recommendations queries are now paginated and feed useInfiniteList like the main lists. Gallery wires fetchNextPage to a new ScrollNavigation onEndReached callback (fires within two slides of the end, once per approach), so all gallery tabs load more on scroll. The show page prefetches the first similar page with prefetchInfiniteQuery, and trending/similar/recommendations are added to the analytics ListName.
150 lines
4.2 KiB
TypeScript
150 lines
4.2 KiB
TypeScript
import { ReactNode, useEffect, useRef, useState } from 'react';
|
|
import { ChevronLeft, ChevronRight } from 'lucide-react';
|
|
|
|
import { cn } from '@/lib/utils';
|
|
|
|
interface Props<E extends HTMLElement> {
|
|
className?: string;
|
|
arrowsClassName?: string;
|
|
backClassName?: string;
|
|
nextClassName?: string;
|
|
iconClassName?: string;
|
|
slideWidth?: number;
|
|
gap?: number;
|
|
onEndReached?: () => void;
|
|
children: ({
|
|
setScrollElement,
|
|
isArrowHovered
|
|
}: {
|
|
setScrollElement: (element: E) => void;
|
|
isArrowHovered: boolean;
|
|
}) => ReactNode;
|
|
}
|
|
|
|
function ScrollNavigation<E extends HTMLElement>({
|
|
className,
|
|
arrowsClassName,
|
|
backClassName,
|
|
nextClassName,
|
|
iconClassName,
|
|
slideWidth: initialSlideWidth,
|
|
gap = 0,
|
|
onEndReached,
|
|
children
|
|
}: Props<E>) {
|
|
const [scrollElement, setScrollElement] = useState<E | null>(null);
|
|
const onEndReachedRef = useRef(onEndReached);
|
|
const endReachedFiredRef = useRef(false);
|
|
|
|
onEndReachedRef.current = onEndReached;
|
|
|
|
const [isShowBack, setIsShowBack] = useState(true);
|
|
const [isShowNext, setIsShowNext] = useState(true);
|
|
|
|
const [isArrowHovered, setIsArrowHovered] = useState(false);
|
|
|
|
const [childWidth, setChildWidth] = useState(0);
|
|
|
|
const slideWidth = initialSlideWidth ?? childWidth + gap;
|
|
|
|
useEffect(() => {
|
|
if (!scrollElement) return;
|
|
|
|
let timeout: NodeJS.Timeout;
|
|
|
|
const slide = scrollElement.firstChild as HTMLElement;
|
|
|
|
if (!initialSlideWidth && slide) {
|
|
setChildWidth(slide.clientWidth);
|
|
}
|
|
|
|
const handleScroll = () => {
|
|
clearTimeout(timeout);
|
|
|
|
const DELAY = 50;
|
|
|
|
timeout = setTimeout(() => {
|
|
const remaining = scrollElement.scrollWidth - (scrollElement.clientWidth + scrollElement.scrollLeft);
|
|
|
|
setIsShowBack(Boolean(scrollElement.scrollLeft));
|
|
setIsShowNext(Boolean(remaining));
|
|
|
|
if (onEndReachedRef.current) {
|
|
const slide = scrollElement.firstChild as HTMLElement;
|
|
const threshold = (slide?.clientWidth ?? 0) * 2 || 300;
|
|
|
|
if (remaining <= threshold && !endReachedFiredRef.current) {
|
|
endReachedFiredRef.current = true;
|
|
onEndReachedRef.current();
|
|
} else if (remaining > threshold) {
|
|
endReachedFiredRef.current = false;
|
|
}
|
|
}
|
|
}, DELAY);
|
|
};
|
|
|
|
handleScroll();
|
|
|
|
scrollElement.addEventListener('scroll', handleScroll);
|
|
|
|
return () => {
|
|
scrollElement.removeEventListener('scroll', handleScroll);
|
|
};
|
|
}, [scrollElement, initialSlideWidth]);
|
|
|
|
const handleScrollNext = (direction: 1 | -1) => () => {
|
|
if (!scrollElement) return;
|
|
|
|
scrollElement.scrollTo({
|
|
left: scrollElement.scrollLeft + slideWidth * direction,
|
|
behavior: 'smooth'
|
|
});
|
|
};
|
|
|
|
return (
|
|
<div className={cn('relative group', className)}>
|
|
{children({ setScrollElement, isArrowHovered })}
|
|
{[
|
|
{
|
|
Arrow: ChevronLeft,
|
|
isShow: isShowBack,
|
|
handleScroll: handleScrollNext(-1),
|
|
className: cn(
|
|
'left-0 bg-gradient-to-r from-background to-transparent justify-start rounded-l-md',
|
|
backClassName
|
|
)
|
|
},
|
|
{
|
|
Arrow: ChevronRight,
|
|
isShow: isShowNext,
|
|
handleScroll: handleScrollNext(1),
|
|
className: cn(
|
|
'right-0 bg-gradient-to-r from-transparent to-background justify-end rounded-r-md',
|
|
nextClassName
|
|
)
|
|
}
|
|
].map(({ Arrow, isShow, handleScroll, className }) => (
|
|
<div
|
|
key={className}
|
|
onClick={handleScroll}
|
|
onMouseEnter={() => setIsArrowHovered(true)}
|
|
onMouseLeave={() => 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',
|
|
{
|
|
'group-hover:opacity-100 hover:!opacity-60': isShow,
|
|
'pointer-events-none': !isShow
|
|
},
|
|
className,
|
|
arrowsClassName
|
|
)}
|
|
>
|
|
<Arrow className={cn('text-primary', iconClassName)} size={30} />
|
|
</div>
|
|
))}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default ScrollNavigation;
|