feat(show-type): path-based /series filter and infinite trending galleries
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.
This commit is contained in:
1 parent
0824090c6d
commit
f22f3c45b4
31 files changed
+372
-229
No files matched your search
@@ -1,27 +1,21 @@
|
||||
'use client';
|
||||
|
||||
import { DropdownMenuItem } from '@radix-ui/react-dropdown-menu';
|
||||
import { useAtomValue } from 'jotai';
|
||||
import { Menu } from 'lucide-react';
|
||||
import Link from 'next/link';
|
||||
import { usePathname } from 'next/navigation';
|
||||
|
||||
import { showTypeAtom } from '@/components/ShowTypeToggle';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger } from '@/components/ui/dropdown-menu';
|
||||
import Nav from '@/components/ui/nav';
|
||||
import { showTypeFromPath, toShowTypePath } from '@/lib/showType';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
import { paths } from './constants';
|
||||
|
||||
const TYPE_AWARE_PATHS = ['/', '/popular', '/top', '/favorites', '/watchlist'];
|
||||
|
||||
const withShowType = (path: string, type: Show['type']) =>
|
||||
type === 'tv' && TYPE_AWARE_PATHS.includes(path) ? `${path}?type=tv` : path;
|
||||
|
||||
export const MobileNavigation = () => {
|
||||
const currentPath = usePathname();
|
||||
const showType = useAtomValue(showTypeAtom);
|
||||
const showType = showTypeFromPath(currentPath);
|
||||
|
||||
return (
|
||||
<div className='flex md:hidden'>
|
||||
@@ -34,9 +28,12 @@ export const MobileNavigation = () => {
|
||||
<DropdownMenuContent className='flex w-44 flex-col gap-2 rounded-none rounded-bl-lg border-none p-2'>
|
||||
{paths.map(({ path, title }) => (
|
||||
<DropdownMenuItem key={path} asChild>
|
||||
<Link href={withShowType(path, showType)} prefetch={path !== '/'}>
|
||||
<Link href={toShowTypePath(path, showType)} prefetch={path !== '/'}>
|
||||
<div>
|
||||
<Button className='relative w-full' variant={currentPath === path ? 'default' : 'outline'}>
|
||||
<Button
|
||||
className='relative w-full'
|
||||
variant={toShowTypePath(path, showType) === currentPath ? 'default' : 'outline'}
|
||||
>
|
||||
{title}
|
||||
</Button>
|
||||
</div>
|
||||
@@ -51,13 +48,13 @@ export const MobileNavigation = () => {
|
||||
|
||||
export const DesktopNavigation = () => {
|
||||
const currentPath = usePathname();
|
||||
const showType = useAtomValue(showTypeAtom);
|
||||
const active = paths.find(({ path }) => path === currentPath);
|
||||
const showType = showTypeFromPath(currentPath);
|
||||
const active = paths.find(({ path }) => toShowTypePath(path, showType) === currentPath);
|
||||
|
||||
return (
|
||||
<Nav tabs={paths} active={active}>
|
||||
{({ path, title }) => (
|
||||
<Link href={withShowType(path, showType)} prefetch={path !== '/'}>
|
||||
<Link href={toShowTypePath(path, showType)} prefetch={path !== '/'}>
|
||||
<div>{title}</div>
|
||||
</Link>
|
||||
)}
|
||||
|
||||
@@ -2,16 +2,16 @@
|
||||
|
||||
import { ChangeEvent, MutableRefObject, useRef, useState } from 'react';
|
||||
import { create } from 'react-modal-promise';
|
||||
import { useAtomValue } from 'jotai';
|
||||
import { Search as SearchIcon, X } from 'lucide-react';
|
||||
import { usePathname } from 'next/navigation';
|
||||
|
||||
import List from '@/components/List';
|
||||
import { showTypeAtom } from '@/components/ShowTypeToggle';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import ButtonsGroup from '@/components/ui/buttons-group';
|
||||
import { Modal } from '@/components/ui/dialog';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import useDistinctUntilChanged from '@/hooks/useDistinctUntilChanged';
|
||||
import { showTypeFromPath } from '@/lib/showType';
|
||||
|
||||
import useSearch from './useSearch';
|
||||
|
||||
@@ -20,7 +20,7 @@ export const showSearchModal = create(({ onResolve }) => {
|
||||
const scrollRef = useRef<HTMLDivElement>(null) as MutableRefObject<HTMLDivElement>;
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
const isFetchedRef = useRef(false);
|
||||
const showType = useAtomValue(showTypeAtom);
|
||||
const showType = showTypeFromPath(usePathname());
|
||||
|
||||
const query = useDistinctUntilChanged(title);
|
||||
const settledTitle = useDistinctUntilChanged(title, 1000);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ReactNode, useEffect, useState } from 'react';
|
||||
import { ReactNode, useEffect, useRef, useState } from 'react';
|
||||
import { ChevronLeft, ChevronRight } from 'lucide-react';
|
||||
|
||||
import { cn } from '@/lib/utils';
|
||||
@@ -11,6 +11,7 @@ interface Props<E extends HTMLElement> {
|
||||
iconClassName?: string;
|
||||
slideWidth?: number;
|
||||
gap?: number;
|
||||
onEndReached?: () => void;
|
||||
children: ({
|
||||
setScrollElement,
|
||||
isArrowHovered
|
||||
@@ -28,9 +29,14 @@ function ScrollNavigation<E extends HTMLElement>({
|
||||
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);
|
||||
@@ -58,8 +64,22 @@ function ScrollNavigation<E extends HTMLElement>({
|
||||
const DELAY = 50;
|
||||
|
||||
timeout = setTimeout(() => {
|
||||
const remaining = scrollElement.scrollWidth - (scrollElement.clientWidth + scrollElement.scrollLeft);
|
||||
|
||||
setIsShowBack(Boolean(scrollElement.scrollLeft));
|
||||
setIsShowNext(Boolean(scrollElement.scrollWidth - (scrollElement.clientWidth + 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);
|
||||
};
|
||||
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
'use client';
|
||||
|
||||
import { FC, useEffect } from 'react';
|
||||
import { FC } from 'react';
|
||||
import * as DropdownMenuPrimitive from '@radix-ui/react-dropdown-menu';
|
||||
import { atom, useAtom } from 'jotai';
|
||||
import { Check, ChevronDown, Clapperboard, LucideIcon, Tv } from 'lucide-react';
|
||||
import Link from 'next/link';
|
||||
import { usePathname, useRouter } from 'next/navigation';
|
||||
|
||||
import analytics from '@/lib/analytics';
|
||||
import { parseShowType } from '@/lib/showType';
|
||||
import { showTypeFromPath, toShowTypePath } from '@/lib/showType';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
export const showTypeAtom = atom<Show['type']>('movie');
|
||||
|
||||
interface Option {
|
||||
value: Show['type'];
|
||||
label: string;
|
||||
@@ -28,24 +26,17 @@ interface Props {
|
||||
}
|
||||
|
||||
const ShowTypeToggle: FC<Props> = ({ className }) => {
|
||||
const [value, setValue] = useAtom(showTypeAtom);
|
||||
|
||||
useEffect(() => {
|
||||
const urlType = parseShowType(new URLSearchParams(window.location.search).get('type'));
|
||||
|
||||
if (urlType !== value) setValue(urlType);
|
||||
}, [value, setValue]);
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
const value = showTypeFromPath(pathname);
|
||||
|
||||
const current = options.find(({ value: optionValue }) => optionValue === value) ?? options[0];
|
||||
|
||||
const handleChange = (next: Show['type']) => {
|
||||
analytics.randomTypeChanged({ type: next });
|
||||
setValue(next);
|
||||
if (next === value) return;
|
||||
|
||||
const url = new URL(window.location.href);
|
||||
if (next === 'tv') url.searchParams.set('type', 'tv');
|
||||
else url.searchParams.delete('type');
|
||||
window.history.replaceState(null, '', url);
|
||||
analytics.randomTypeChanged({ type: next });
|
||||
router.replace(toShowTypePath(pathname, next), { scroll: false });
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -59,7 +50,7 @@ const ShowTypeToggle: FC<Props> = ({ className }) => {
|
||||
)}
|
||||
>
|
||||
<Link
|
||||
href={value === 'tv' ? '/?type=tv' : '/'}
|
||||
href={toShowTypePath('/', value)}
|
||||
onPointerDown={(event) => event.stopPropagation()}
|
||||
onClick={(event) => event.stopPropagation()}
|
||||
className='pl-2.5 pr-1.5 text-[15px] font-normal leading-[18px]'
|
||||
|
||||
@@ -9,10 +9,11 @@ import { showPath } from '@/lib/utils';
|
||||
|
||||
interface Props {
|
||||
shows: Show[];
|
||||
onEndReached?: () => void;
|
||||
}
|
||||
|
||||
const Gallery: FC<Props> = ({ shows }) => (
|
||||
<ScrollNavigation<HTMLUListElement>>
|
||||
const Gallery: FC<Props> = ({ shows, onEndReached }) => (
|
||||
<ScrollNavigation<HTMLUListElement> onEndReached={onEndReached}>
|
||||
{({ setScrollElement }) => (
|
||||
<ul
|
||||
ref={setScrollElement}
|
||||
|
||||
@@ -66,7 +66,7 @@ const Trending: FC<Props> = ({ showId, showType, preview }) => {
|
||||
}}
|
||||
className='mb-3'
|
||||
/>
|
||||
{!tab.isPending && <Gallery key={active.type} shows={tab.data || []} />}
|
||||
{tab.isFetched && <Gallery key={active.type} shows={tab.shows} onEndReached={tab.fetchNextPage} />}
|
||||
</div>
|
||||
)
|
||||
);
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import similarQuery from '@/data/queries/similar';
|
||||
import useInfiniteList from '@/hooks/useInfiniteList';
|
||||
|
||||
interface Props {
|
||||
showId: Show['id'];
|
||||
@@ -12,6 +11,6 @@ interface Props {
|
||||
}
|
||||
|
||||
const useSimilar = ({ showId, showType = 'movie', type = 'similar', enabled }: Props) =>
|
||||
useQuery({ ...similarQuery({ showId, showType, type }), enabled });
|
||||
useInfiniteList({ ...similarQuery({ showId, showType, type }), mode: 'default', list: type, enabled });
|
||||
|
||||
export default useSimilar;
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import trendingQuery, { TrendingQueryProps } from '@/data/queries/trending';
|
||||
import useInfiniteList from '@/hooks/useInfiniteList';
|
||||
|
||||
const useTrending = ({ type, enabled }: TrendingQueryProps) => useQuery(trendingQuery({ type, enabled }));
|
||||
const useTrending = ({ type, enabled }: TrendingQueryProps) =>
|
||||
useInfiniteList({ ...trendingQuery({ type, enabled }), mode: 'default', list: 'trending' });
|
||||
|
||||
export default useTrending;
|
||||
Reference in new issue
Block a user