refactor: clean code and remove unnecessary dependencies
This commit is contained in:
1 parent
eceae6fec7
commit
ebf599eed6
92 files changed
+1152
-2842
No files matched your search
@@ -0,0 +1,99 @@
|
||||
'use client';
|
||||
|
||||
import { FC, forwardRef, useEffect, useLayoutEffect, useRef } from 'react';
|
||||
import { Virtuoso, VirtuosoHandle, VirtuosoProps } from 'react-virtuoso';
|
||||
|
||||
import Card from '@/components/Show';
|
||||
|
||||
const components: VirtuosoProps<Show, (index: number) => void>['components'] = {
|
||||
Item: ({ item, children, context: onIndexChange, ...props }) => {
|
||||
const ref = useRef<HTMLDivElement>(null);
|
||||
const index = props['data-index'];
|
||||
|
||||
useLayoutEffect(() => {
|
||||
const container = ref.current;
|
||||
|
||||
if (!container) return;
|
||||
|
||||
const observer = new IntersectionObserver(
|
||||
(entries) =>
|
||||
entries.forEach((entry) => {
|
||||
if (!entry.isIntersecting) return;
|
||||
|
||||
onIndexChange?.(index);
|
||||
}),
|
||||
{
|
||||
threshold: 0.7
|
||||
}
|
||||
);
|
||||
|
||||
observer.observe(container);
|
||||
|
||||
return () => observer.disconnect();
|
||||
}, [onIndexChange, index]);
|
||||
|
||||
return (
|
||||
<div {...props} ref={ref} className='snap-start'>
|
||||
<Card show={item} />
|
||||
</div>
|
||||
);
|
||||
},
|
||||
List: forwardRef(function List({ context, ...props }, ref) {
|
||||
return <div {...props} ref={ref} style={{ ...props.style, display: 'flex' }} className='gap-2' />;
|
||||
}),
|
||||
Scroller: forwardRef(function Scroller({ context, ...props }, ref) {
|
||||
return (
|
||||
<div {...props} ref={ref} className='overflow-hidden no-scrollbar snap-mandatory snap-x rounded-lg h-full' />
|
||||
);
|
||||
})
|
||||
};
|
||||
|
||||
interface Props {
|
||||
index: number;
|
||||
shows?: Show[];
|
||||
onIndexChange: (index: number) => void;
|
||||
onEndReached?: () => void;
|
||||
}
|
||||
|
||||
const Carousel: FC<Props> = ({ index, shows, onIndexChange, onEndReached }) => {
|
||||
const ref = useRef<VirtuosoHandle>(null);
|
||||
const initialIndex = useRef(index);
|
||||
|
||||
useEffect(() => {
|
||||
const scrollTo = ({ key }: globalThis.KeyboardEvent) => {
|
||||
if (key === 'ArrowLeft') {
|
||||
if (index <= 0) return;
|
||||
ref.current?.scrollIntoView({ index: index - 1, behavior: 'smooth' });
|
||||
}
|
||||
|
||||
if (key === 'ArrowRight') {
|
||||
if (index >= (shows?.length || 0) - 1) return;
|
||||
ref.current?.scrollIntoView({ index: index + 1, behavior: 'smooth' });
|
||||
}
|
||||
};
|
||||
|
||||
document.addEventListener('keydown', scrollTo);
|
||||
|
||||
return () => {
|
||||
document.removeEventListener('keydown', scrollTo);
|
||||
};
|
||||
}, [index, shows?.length]);
|
||||
|
||||
return (
|
||||
<div className='card-aspect-ratio mx-auto'>
|
||||
<Virtuoso
|
||||
ref={ref}
|
||||
data={shows}
|
||||
context={onIndexChange}
|
||||
endReached={onEndReached}
|
||||
initialTopMostItemIndex={initialIndex.current}
|
||||
components={components}
|
||||
horizontalDirection
|
||||
skipAnimationFrameInResizeObserver
|
||||
increaseViewportBy={1000}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default Carousel;
|
||||
@@ -0,0 +1,20 @@
|
||||
'use client';
|
||||
|
||||
import Preview from '../Preview';
|
||||
|
||||
import Carousel from './components/Carousel';
|
||||
import useRandomMovie from './useRandomMovie';
|
||||
|
||||
const Random = () => {
|
||||
const { movies, movie, index, onIndexChange, fetchNextPage } = useRandomMovie();
|
||||
|
||||
return (
|
||||
<Preview
|
||||
show={movie}
|
||||
className='bg-background rounded-md'
|
||||
card={<Carousel index={index} shows={movies} onIndexChange={onIndexChange} onEndReached={fetchNextPage} />}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
export default Random;
|
||||
@@ -0,0 +1,38 @@
|
||||
'use client';
|
||||
|
||||
import { startTransition, useCallback, useState } from 'react';
|
||||
|
||||
import { randomMovies } from '@/api';
|
||||
import useInfiniteList from '@/hooks/useInfiniteList';
|
||||
import useSessionRef from '@/hooks/useSessionRef';
|
||||
|
||||
const useRandomMovie = () => {
|
||||
const [indexRef, setIndexRef] = useSessionRef('random-movie-index', 0);
|
||||
const [index, setIndex] = useState(indexRef.current);
|
||||
|
||||
const { shows: movies, fetchNextPage } = useInfiniteList({
|
||||
queryKey: ['random-movie'],
|
||||
queryFn: () => randomMovies()
|
||||
});
|
||||
|
||||
if (movies.length && !movies[index]) {
|
||||
setIndex(0);
|
||||
setIndexRef(0);
|
||||
}
|
||||
|
||||
return {
|
||||
index,
|
||||
movies,
|
||||
movie: movies[index],
|
||||
fetchNextPage,
|
||||
onIndexChange: useCallback(
|
||||
(index: number) => {
|
||||
setIndexRef(index);
|
||||
startTransition(() => setIndex(index));
|
||||
},
|
||||
[setIndexRef]
|
||||
)
|
||||
};
|
||||
};
|
||||
|
||||
export default useRandomMovie;
|
||||
Reference in new issue
Block a user