refactoring: improvements for lighthouse
This commit is contained in:
1 parent
f2f242a684
commit
38aebcbe28
27 files changed
+132
-93
No files matched your search
@@ -1,43 +1,55 @@
|
||||
'use client';
|
||||
|
||||
import { FC, ForwardedRef, forwardRef, useLayoutEffect, useMemo, useRef } from 'react';
|
||||
import { ForwardedRef, forwardRef, memo, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
||||
import { Virtuoso, VirtuosoProps } from 'react-virtuoso';
|
||||
|
||||
import Poster from '@/components/Poster';
|
||||
import ScrollNavigation from '@/components/ScrollNavigation';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
import { useSilentIndex } from '../../useRandomMovie';
|
||||
|
||||
interface Context {
|
||||
onIndexChange: (index: number) => void;
|
||||
gap?: number;
|
||||
}
|
||||
|
||||
interface Props extends Context {
|
||||
index: number;
|
||||
shows?: Show[];
|
||||
onEndReached?: () => void;
|
||||
}
|
||||
|
||||
const Carousel: FC<Props> = ({ index, shows, gap = 8, onIndexChange, onEndReached }) => {
|
||||
const initialIndex = useRef(index);
|
||||
const Carousel = memo(
|
||||
function Carousel({ shows, gap = 8, onIndexChange, onEndReached }: Props) {
|
||||
const index = useSilentIndex();
|
||||
|
||||
const context = useMemo(() => ({ onIndexChange, gap }), [onIndexChange, gap]);
|
||||
const [isFirstRender, setIsFirstRender] = useState(true);
|
||||
|
||||
return (
|
||||
<div className='card-aspect-ratio mx-auto'>
|
||||
<Virtuoso
|
||||
horizontalDirection
|
||||
skipAnimationFrameInResizeObserver
|
||||
data={shows}
|
||||
context={context}
|
||||
endReached={onEndReached}
|
||||
initialTopMostItemIndex={initialIndex.current}
|
||||
components={components}
|
||||
increaseViewportBy={1000}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
const context = useMemo(() => ({ onIndexChange, gap }), [onIndexChange, gap]);
|
||||
|
||||
return (
|
||||
<div className='card-aspect-ratio relative mx-auto'>
|
||||
{isFirstRender && (
|
||||
<div className='absolute left-0 top-0 size-full'>
|
||||
<Poster show={shows?.[index]} />
|
||||
</div>
|
||||
)}
|
||||
<Virtuoso
|
||||
horizontalDirection
|
||||
skipAnimationFrameInResizeObserver
|
||||
data={shows}
|
||||
itemsRendered={() => isFirstRender && setIsFirstRender(false)}
|
||||
context={context}
|
||||
endReached={onEndReached}
|
||||
initialTopMostItemIndex={index}
|
||||
components={components}
|
||||
increaseViewportBy={1000}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
},
|
||||
(prev, next) => prev.shows === next.shows
|
||||
);
|
||||
|
||||
const components: VirtuosoProps<Show, Context>['components'] = {
|
||||
Item: ({ item, children, context, ...props }) => {
|
||||
|
||||
@@ -6,13 +6,13 @@ import Carousel from './components/Carousel';
|
||||
import useRandomMovie from './useRandomMovie';
|
||||
|
||||
const Random = () => {
|
||||
const { movies, movie, index, onIndexChange, fetchNextPage } = useRandomMovie();
|
||||
const { movie, movies, onIndexChange, fetchNextPage } = useRandomMovie();
|
||||
|
||||
return (
|
||||
<Preview
|
||||
show={movie}
|
||||
className='rounded-md bg-background'
|
||||
poster={<Carousel index={index} shows={movies} onIndexChange={onIndexChange} onEndReached={fetchNextPage} />}
|
||||
poster={<Carousel shows={movies} onIndexChange={onIndexChange} onEndReached={fetchNextPage} />}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,36 +1,37 @@
|
||||
'use client';
|
||||
|
||||
import { startTransition, useCallback, useState } from 'react';
|
||||
import { startTransition, useCallback } from 'react';
|
||||
import { atom, getDefaultStore, useAtom } from 'jotai';
|
||||
|
||||
import { randomMovies } from '@/api';
|
||||
import useInfiniteList from '@/hooks/useInfiniteList';
|
||||
import useSessionRef from '@/hooks/useSessionRef';
|
||||
|
||||
const indexAtom = atom(0);
|
||||
|
||||
export const useSilentIndex = () => getDefaultStore().get(indexAtom);
|
||||
|
||||
const useRandomMovie = () => {
|
||||
const [indexRef, setIndexRef] = useSessionRef('random-movie-index', 0);
|
||||
const [index, setIndex] = useState(indexRef.current);
|
||||
const [index, setIndex] = useAtom(indexAtom);
|
||||
|
||||
const { shows: movies, fetchNextPage } = useInfiniteList({
|
||||
suspense: true,
|
||||
queryKey: ['random-movie'],
|
||||
queryFn: () => randomMovies()
|
||||
queryFn: randomMovies
|
||||
});
|
||||
|
||||
if (movies.length && !movies[index]) {
|
||||
setIndex(0);
|
||||
setIndexRef(0);
|
||||
}
|
||||
|
||||
return {
|
||||
index,
|
||||
movies,
|
||||
movie: movies[index],
|
||||
movies,
|
||||
fetchNextPage,
|
||||
onIndexChange: useCallback(
|
||||
(index: number) => {
|
||||
setIndexRef(index);
|
||||
startTransition(() => setIndex(index));
|
||||
},
|
||||
[setIndexRef]
|
||||
[setIndex]
|
||||
)
|
||||
};
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user