feat: movie groups on main page
This commit is contained in:
1 parent
57ebf9e669
commit
e3b9ed78f2
5 files changed
+110
-63
No files matched your search
@@ -1,22 +1,43 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { FC } from 'react';
|
import { FC, useState } from 'react';
|
||||||
|
|
||||||
import Marquee from '@/components/Marquee';
|
import Marquee from '@/components/Marquee';
|
||||||
import { Badge } from '@/components/ui/badge';
|
import Nav from '@/components/ui/nav';
|
||||||
|
|
||||||
import useTrending from './useTrending';
|
import useTrending from './useTrending';
|
||||||
|
|
||||||
const Trending: FC<{ label: string; type: 'trending' | 'streaming' | 'theater' | 'rent' }> = ({ label, type }) => {
|
const tabs = [
|
||||||
|
{
|
||||||
|
title: 'Trending',
|
||||||
|
type: 'trending'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Streaming',
|
||||||
|
type: 'streaming'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'In theaters',
|
||||||
|
type: 'theater'
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
const Trending: FC = () => {
|
||||||
|
const [active, setActive] = useState(tabs[0]);
|
||||||
const isMobile = typeof window === 'undefined' ? true : matchMedia('(max-width: 600px)').matches;
|
const isMobile = typeof window === 'undefined' ? true : matchMedia('(max-width: 600px)').matches;
|
||||||
const { data: movies, isFetched } = useTrending({ type, enabled: !isMobile });
|
|
||||||
|
const { data: trending, isFetched } = useTrending({ type: 'trending', enabled: !isMobile });
|
||||||
|
const { data: streaming } = useTrending({ type: 'streaming', enabled: !isMobile });
|
||||||
|
const { data: theater } = useTrending({ type: 'theater', enabled: !isMobile });
|
||||||
|
|
||||||
|
const movies = { trending, streaming, theater }[active.type] || [];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
isFetched && (
|
isFetched && (
|
||||||
<>
|
<div>
|
||||||
<Badge className='mb-2 font-normal'>{label}</Badge>
|
<Nav tabs={tabs} active={active} onChange={(active) => setActive(active)} className='mb-3' />
|
||||||
<Marquee movies={movies || []} />
|
<Marquee key={active.type} movies={movies} />
|
||||||
</>
|
</div>
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -26,18 +26,9 @@ export default function Container() {
|
|||||||
<div className='w-full h-2' />
|
<div className='w-full h-2' />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className='xs:flex hidden flex-1 w-full h-full overflow-auto snap-mandatory snap-y relative rounded-lg no-scrollbar'>
|
<div className='xs:block hidden w-full rounded-lg'>
|
||||||
<div className='absolute left-0 top-0 w-full'>
|
<Trending />
|
||||||
<div className='snap-center'>
|
<div className='w-full h-2' />
|
||||||
<Trending label='Trending' type='trending' />
|
|
||||||
</div>
|
|
||||||
<div className='mt-6 snap-center'>
|
|
||||||
<Trending label='Streaming' type='streaming' />
|
|
||||||
</div>
|
|
||||||
<div className='mt-6 mb-6 snap-center'>
|
|
||||||
<Trending label='In theaters' type='theater' />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import { usePathname } from 'next/navigation';
|
|||||||
|
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger } from '@/components/ui/dropdown-menu';
|
import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger } from '@/components/ui/dropdown-menu';
|
||||||
import { cn } from '@/lib/utils';
|
import Nav from '@/components/ui/nav';
|
||||||
|
|
||||||
const paths = [
|
const paths = [
|
||||||
{ path: '/', title: 'Random' },
|
{ path: '/', title: 'Random' },
|
||||||
@@ -47,39 +47,16 @@ export function MobileNavigation() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function DesktopNavigation() {
|
export function DesktopNavigation() {
|
||||||
const [position, setPosition] = useState({ left: 0, width: 0, opacity: 0 });
|
|
||||||
const currentRef = useRef<HTMLLIElement>(null);
|
|
||||||
const currentPath = usePathname();
|
const currentPath = usePathname();
|
||||||
|
const active = paths.find(({ path }) => path === currentPath);
|
||||||
useEffect(() => {
|
|
||||||
const link = currentRef.current;
|
|
||||||
|
|
||||||
setPosition({ width: link?.clientWidth || 0, left: link?.offsetLeft || 0, opacity: link ? 1 : 0 });
|
|
||||||
}, [currentPath]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ul className='relative hidden md:flex items-center whitespace-nowrap text-sm gap-3'>
|
<Nav tabs={paths} active={active || paths[0]}>
|
||||||
{paths.map(({ path, title }) => (
|
{({ path, title }) => (
|
||||||
<li
|
<Link href={path}>
|
||||||
key={path}
|
<div>{title}</div>
|
||||||
ref={currentPath !== path ? undefined : currentRef}
|
</Link>
|
||||||
className={cn('mix-blend-difference text-white z-10 px-2.5 py-0.5', currentPath !== path && 'cursor-pointer')}
|
)}
|
||||||
>
|
</Nav>
|
||||||
<Link href={path}>
|
|
||||||
<div>{title}</div>
|
|
||||||
</Link>
|
|
||||||
</li>
|
|
||||||
))}
|
|
||||||
<Cursor options={position} />
|
|
||||||
</ul>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const Cursor: FC<{ options: { left: number; width: number; opacity: number } }> = ({ options }) => {
|
|
||||||
return (
|
|
||||||
<li
|
|
||||||
className='pointer-events-none absolute bg-primary rounded-full h-full z-0 transition-all'
|
|
||||||
style={{ width: options.width, left: options.left, opacity: options.opacity }}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
@@ -7,16 +7,14 @@ interface Props {
|
|||||||
movies: Movie[];
|
movies: Movie[];
|
||||||
}
|
}
|
||||||
|
|
||||||
const Marquee: FC<Props> = ({ movies }) => {
|
const Marquee: FC<Props> = ({ movies }) => (
|
||||||
return (
|
<ul className='flex overflow-auto gap-2 rounded-lg snap-mandatory snap-x no-scrollbar opacity-0 animate-fade-aside-slide-in'>
|
||||||
<ul className='flex overflow-auto gap-2 rounded-lg snap-mandatory snap-x no-scrollbar'>
|
{movies.map((movie) => (
|
||||||
{movies.map((movie) => (
|
<li key={movie.id} className='snap-start'>
|
||||||
<li key={movie.id} className='snap-start'>
|
<Card className='!h-[350px] !w-[230px] text-sm' movie={movie} onClick={() => showPreviewModal({ movie })} />
|
||||||
<Card className='!h-[250px] !w-[175px] text-sm' movie={movie} onClick={() => showPreviewModal({ movie })} />
|
</li>
|
||||||
</li>
|
))}
|
||||||
))}
|
</ul>
|
||||||
</ul>
|
);
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default Marquee;
|
export default Marquee;
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
import { FC, ReactNode, useLayoutEffect, useRef, useState } from 'react';
|
||||||
|
|
||||||
|
import { cn } from '@/lib/utils';
|
||||||
|
|
||||||
|
type Tab = { title: string };
|
||||||
|
|
||||||
|
type CursorProps = { left: number; width: number; opacity: number };
|
||||||
|
|
||||||
|
const Cursor: FC<CursorProps> = ({ width, left, opacity }) => (
|
||||||
|
<li
|
||||||
|
className='pointer-events-none absolute bg-primary rounded-2xl h-full z-0 transition-all'
|
||||||
|
style={{ width, left, opacity }}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
|
export type NavProps<T extends Tab> = {
|
||||||
|
tabs: T[];
|
||||||
|
className?: string;
|
||||||
|
active: T;
|
||||||
|
onChange?: (tab: T) => void;
|
||||||
|
children?: (tab: T) => ReactNode;
|
||||||
|
};
|
||||||
|
|
||||||
|
function Nav<T extends Tab>({ tabs, active, className, children, onChange }: NavProps<T>) {
|
||||||
|
const [cursor, setCursor] = useState({ left: 0, width: 0, opacity: 0 });
|
||||||
|
const currentRef = useRef<HTMLLIElement>(null);
|
||||||
|
|
||||||
|
useLayoutEffect(() => {
|
||||||
|
const link = currentRef.current;
|
||||||
|
|
||||||
|
setCursor({ width: link?.clientWidth || 0, left: link?.offsetLeft || 0, opacity: link ? 1 : 0 });
|
||||||
|
}, [active?.title]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ul className={cn('relative hidden md:flex items-center whitespace-nowrap text-[13px] gap-3', className)}>
|
||||||
|
{tabs.map((tab) => {
|
||||||
|
const { title } = tab;
|
||||||
|
|
||||||
|
const isActive = title === active.title;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<li
|
||||||
|
key={title}
|
||||||
|
ref={isActive ? currentRef : undefined}
|
||||||
|
onClick={() => onChange?.(tab)}
|
||||||
|
className={cn(
|
||||||
|
'mix-blend-difference text-white z-10 px-2.5 py-0.5 font-normal cursor-default',
|
||||||
|
!isActive && 'cursor-pointer'
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{children?.(tab) || <div>{title}</div>}
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
<Cursor {...cursor} />
|
||||||
|
</ul>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Nav;
|
||||||
Reference in new issue
Block a user