refactoring: first render improvements
This commit is contained in:
1 parent
38aebcbe28
commit
0cface89bf
13 files changed
+38
-33
No files matched your search
@@ -9,10 +9,11 @@ interface Props {
|
||||
onClick?: () => void;
|
||||
className?: string;
|
||||
rounded?: string;
|
||||
lazy?: boolean;
|
||||
containerProps?: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>;
|
||||
}
|
||||
|
||||
const Poster: FC<Props> = ({ show, onClick, className, rounded, containerProps }) => {
|
||||
const Poster: FC<Props> = ({ show, onClick, className, rounded, containerProps, lazy = true }) => {
|
||||
if (!show) return null;
|
||||
|
||||
return (
|
||||
@@ -21,7 +22,12 @@ const Poster: FC<Props> = ({ show, onClick, className, rounded, containerProps }
|
||||
className={cn('card-aspect-ratio relative bg-black overflow-hidden rounded-lg text-lg', className, rounded)}
|
||||
>
|
||||
<div className='flex size-full'>
|
||||
<img className={cn('size-full rounded-lg object-cover', rounded)} src={show.poster} alt={show.title} />
|
||||
<img
|
||||
className={cn('size-full rounded-lg object-cover', rounded)}
|
||||
loading={lazy ? 'lazy' : 'eager'}
|
||||
src={show.poster}
|
||||
alt={show.title}
|
||||
/>
|
||||
</div>
|
||||
<div
|
||||
className={cn(
|
||||
|
||||
@@ -65,7 +65,7 @@ const Actions = ({ onClose, show, className }: Props) => {
|
||||
</Link>
|
||||
<Button
|
||||
aria-label='Watch show'
|
||||
className={cn(rightClassName, 'bg-red-600')}
|
||||
className={cn(rightClassName, 'bg-red-700')}
|
||||
variant='destructive'
|
||||
onClick={() =>
|
||||
showTorrentsModal({
|
||||
|
||||
@@ -58,7 +58,7 @@ const components: VirtuosoProps<Actor, Context>['components'] = {
|
||||
onClick={() => context?.onClick?.(actor.id.toString())}
|
||||
>
|
||||
<div className='overflow-hidden rounded-lg'>
|
||||
<img height={180} width={120} src={actor.photoUrl} alt={actor.name} />
|
||||
<img height={180} width={120} loading='lazy' src={actor.photoUrl} alt={actor.name} />
|
||||
</div>
|
||||
<div className='absolute left-0 top-0 flex size-full flex-col justify-between rounded-md bg-vignette p-2 py-1 text-sm text-white'>
|
||||
<p className=' truncate'>{actor.name}</p>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import { ForwardedRef, forwardRef, memo, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
||||
import { ForwardedRef, forwardRef, memo, useLayoutEffect, useMemo, useRef } from 'react';
|
||||
import { Virtuoso, VirtuosoProps } from 'react-virtuoso';
|
||||
|
||||
import Poster from '@/components/Poster';
|
||||
@@ -23,22 +23,15 @@ const Carousel = memo(
|
||||
function Carousel({ shows, gap = 8, onIndexChange, onEndReached }: Props) {
|
||||
const index = useSilentIndex();
|
||||
|
||||
const [isFirstRender, setIsFirstRender] = useState(true);
|
||||
|
||||
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
|
||||
initialItemCount={1}
|
||||
horizontalDirection
|
||||
skipAnimationFrameInResizeObserver
|
||||
data={shows}
|
||||
itemsRendered={() => isFirstRender && setIsFirstRender(false)}
|
||||
context={context}
|
||||
endReached={onEndReached}
|
||||
initialTopMostItemIndex={index}
|
||||
@@ -80,7 +73,7 @@ const components: VirtuosoProps<Show, Context>['components'] = {
|
||||
|
||||
return (
|
||||
<div {...props} ref={ref} className='snap-start'>
|
||||
<Poster show={item} />
|
||||
<Poster lazy={false} show={item} />
|
||||
</div>
|
||||
);
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user