refactoring: improvements for lighthouse

This commit is contained in:
ner committed 2024-10-21 15:35:08 +03:00
1 parent f2f242a684
commit 38aebcbe28
27 files changed
+132 -93

No files matched your search

@@ -18,7 +18,7 @@ export const MobileNavigation = () => {
<div className='flex md:hidden'>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant='ghost' size='icon'>
<Button aria-label='Navigation toggle' variant='ghost' size='icon'>
<Menu size={19} />
</Button>
</DropdownMenuTrigger>
@@ -106,7 +106,7 @@ export const showSearchModal = create(({ onResolve }) => {
});
const ToggleSearch = () => (
<Button variant='ghost' size='icon' onClick={() => showSearchModal()}>
<Button aria-label='Search show' variant='ghost' size='icon' onClick={() => showSearchModal()}>
<SearchIcon size={19} />
</Button>
);
@@ -10,6 +10,7 @@ const ThemeToggle = () => {
return (
<Button
aria-label='Toggle theme'
variant='ghost'
size='icon'
onClick={() => setTheme(theme === 'dark' ? 'light' : 'dark')}
+1 -1
View File
@@ -26,7 +26,7 @@ const User = () => {
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant='ghost' size='icon'>
<Button aria-label='Account menu' variant='ghost' size='icon'>
<UserRound size={20} />
</Button>
</DropdownMenuTrigger>
@@ -25,7 +25,7 @@ const Actions = ({ onClose, show, className }: Props) => {
return (
<div className={cn('flex gap-2 flex-wrap md:flex-grow-0 flex-grow', className)}>
<div className={cn(groupClassName, 'pointer-events-none')}>
<Button className={leftClassName} variant='outline'>
<Button aria-label='Show rating' className={leftClassName} variant='outline'>
<Star size={14} />
</Button>
<Button className={rightClassName} variant='outline'>
@@ -34,6 +34,7 @@ const Actions = ({ onClose, show, className }: Props) => {
</div>
<div className={groupClassName}>
<Button
aria-label='Toggle favorite'
className={leftClassName}
variant='outline'
disabled={toggle.list === 'favorite' && toggle.isPending}
@@ -42,6 +43,7 @@ const Actions = ({ onClose, show, className }: Props) => {
<Heart size={14} className={cn({ 'fill-secondary-foreground': state?.favorite })} />
</Button>
<Button
aria-label='Toggle watch list'
className={rightClassName}
variant='outline'
disabled={toggle.list === 'watchlist' && toggle.isPending}
@@ -56,12 +58,13 @@ const Actions = ({ onClose, show, className }: Props) => {
href={`/similar?id=${show.id}&title=${encodeURIComponent(show.title)}&type=${show.type}`}
onClick={onClose}
>
<Button className={leftClassName} variant='outline'>
<Button aria-label='Similar show' className={leftClassName} variant='outline'>
<Copy size={14} />
Similar
</Button>
</Link>
<Button
aria-label='Watch show'
className={cn(rightClassName, 'bg-red-600')}
variant='destructive'
onClick={() =>
@@ -53,6 +53,7 @@ const components: VirtuosoProps<Actor, Context>['components'] = {
className='relative shrink-0 cursor-pointer snap-start self-end bg-card text-card-foreground transition-shadow hover:shadow-lg'
>
<Link
prefetch={false}
href={`/starring?actorId=${actor.id}&title=${encodeURIComponent(actor.name)}`}
onClick={() => context?.onClick?.(actor.id.toString())}
>
@@ -3,13 +3,15 @@ import { useQuery } from '@tanstack/react-query';
import { credits } from '@/api';
interface Props {
showId: Show['id'];
showId?: Show['id'];
showType?: Show['type'];
}
export const KEY = ({ showId, showType }: Props) => ['credits', showId, showType];
const useCredits = ({ showId, showType }: Props) => {
return useQuery({
queryKey: ['credits', showId, showType],
queryKey: KEY({ showId, showType }),
queryFn: () => credits({ showId, showType }),
enabled: Boolean(showId)
});
@@ -19,6 +19,9 @@ import CaptionsMenu from './components/CaptionsMenu';
import VideosMenu from './components/VideosMenu';
import useSource from './useSource';
import '@vidstack/react/player/styles/base.css';
import '@vidstack/react/player/styles/plyr/theme.css';
interface Props {
magnet: string;
subtitles: Source[];
+9 -2
View File
@@ -2,10 +2,17 @@ import { useQuery } from '@tanstack/react-query';
import { details } from '@/api';
const useDetails = ({ showId, showType = 'movie' }: { showId?: Show['id']; showType?: Show['type'] }) =>
interface Props {
showId?: Show['id'];
showType?: Show['type'];
}
export const KEY = ({ showId, showType = 'movie' }: Props) => ['details', showId, showType];
const useDetails = ({ showId, showType = 'movie' }: Props) =>
useQuery({
enabled: Boolean(showId && showType),
queryKey: ['details', showId, showType],
queryKey: KEY({ showId, showType }),
queryFn: () => details({ showId: showId!, showType })
});
+32 -20
View File
@@ -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 }) => {
+2 -2
View File
@@ -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} />}
/>
);
};
+11 -10
View File
@@ -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]
)
};
};