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,61 @@
|
||||
'use client';
|
||||
|
||||
import { FC } from 'react';
|
||||
import { User2 } from 'lucide-react';
|
||||
import Image from 'next/image';
|
||||
import Link from 'next/link';
|
||||
|
||||
import useCredits from './useCredits';
|
||||
|
||||
interface Props {
|
||||
showId: Show['id'];
|
||||
showType?: Show['type'];
|
||||
onPersonClick?: (id: string) => void;
|
||||
}
|
||||
|
||||
const Credits: FC<Props> = ({ showId, showType = 'movie', onPersonClick }) => {
|
||||
const { data: credits } = useCredits({ showId, showType });
|
||||
|
||||
return (
|
||||
Boolean(credits?.length) && (
|
||||
<ul className='flex gap-2 no-scrollbar overflow-auto rounded-lg snap-mandatory snap-x opacity-0 animate-fade-aside-slide-in'>
|
||||
{credits?.map((actor) => (
|
||||
<li
|
||||
key={`${actor.id}_${actor.cast_id}`}
|
||||
title={actor.name}
|
||||
className='bg-card text-card-foreground self-end max-w-[120px] w-full shrink-0 cursor-pointer transition-shadow hover:shadow-lg relative snap-start'
|
||||
>
|
||||
<Link
|
||||
href={`/starring?actorId=${actor.id}&title=${encodeURIComponent(actor.name)}`}
|
||||
onClick={() => onPersonClick?.(actor.id.toString())}
|
||||
>
|
||||
<div className='rounded-lg overflow-hidden'>
|
||||
{actor.profile_path ? (
|
||||
<Image
|
||||
unoptimized
|
||||
height={180}
|
||||
width={120}
|
||||
src={`https://image.tmdb.org/t/p/w500/${actor.profile_path}`}
|
||||
alt={actor.name}
|
||||
/>
|
||||
) : (
|
||||
<div className='h-[180px] w-[120px] flex items-center justify-center rounded-lg'>
|
||||
<User2 size={120} strokeWidth={1} />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className='text-white text-sm absolute w-full h-full top-0 left-0 rounded-md flex flex-col justify-between p-2 py-1 bg-vignette'>
|
||||
<p className=' overflow-ellipsis whitespace-nowrap overflow-hidden'>{actor.name}</p>
|
||||
{Boolean(actor.character) && (
|
||||
<p className='overflow-ellipsis whitespace-nowrap overflow-hidden'>{actor.character}</p>
|
||||
)}
|
||||
</div>
|
||||
</Link>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
export default Credits;
|
||||
@@ -0,0 +1,18 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { credits } from '@/api';
|
||||
|
||||
interface Props {
|
||||
showId: Show['id'];
|
||||
showType?: Show['type'];
|
||||
}
|
||||
|
||||
const useCredits = ({ showId, showType }: Props) => {
|
||||
return useQuery({
|
||||
queryKey: ['credits', showId, showType],
|
||||
queryFn: () => credits({ showId, showType }),
|
||||
enabled: Boolean(showId)
|
||||
});
|
||||
};
|
||||
|
||||
export default useCredits;
|
||||
@@ -0,0 +1,23 @@
|
||||
'use client';
|
||||
|
||||
import { FC } from 'react';
|
||||
import { Heart } from 'lucide-react';
|
||||
|
||||
import { useFavoriteToggle } from '@/app/(site)/favorites/useFavorites';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
interface Props {
|
||||
show: Show;
|
||||
}
|
||||
|
||||
const Favorite: FC<Props> = ({ show }) => {
|
||||
const { isFavorite, toggle } = useFavoriteToggle(show);
|
||||
|
||||
return (
|
||||
<span onClick={toggle} className='cursor-pointer' title={isFavorite ? 'Remove from favorites' : 'Add to favorite'}>
|
||||
<Heart className={cn(isFavorite && 'fill-white')} size={20} />
|
||||
</span>
|
||||
);
|
||||
};
|
||||
|
||||
export default Favorite;
|
||||
@@ -0,0 +1,18 @@
|
||||
'use client';
|
||||
|
||||
import { FC } from 'react';
|
||||
|
||||
import { useFavoriteToggle } from '@/app/(site)/favorites/useFavorites';
|
||||
|
||||
interface Props {
|
||||
show: Show;
|
||||
onClick?: () => void;
|
||||
}
|
||||
|
||||
const Handlers: FC<Props> = ({ onClick, show }) => {
|
||||
const { toggle } = useFavoriteToggle(show);
|
||||
|
||||
return <div onClick={onClick} onDoubleClick={toggle} className='absolute top-0 left-0 w-full h-full' />;
|
||||
};
|
||||
|
||||
export default Handlers;
|
||||
@@ -0,0 +1,23 @@
|
||||
import { FC } from 'react';
|
||||
import { ImageOff } from 'lucide-react';
|
||||
|
||||
interface Props {
|
||||
title: string;
|
||||
poster: string;
|
||||
}
|
||||
|
||||
const Poster: FC<Props> = ({ title, poster }) => {
|
||||
return poster ? (
|
||||
<img
|
||||
className='rounded-lg w-full h-full object-cover'
|
||||
src={`https://image.tmdb.org/t/p/w500/${poster}`}
|
||||
alt={title}
|
||||
/>
|
||||
) : (
|
||||
<div className='w-full flex items-center justify-center rounded-lg grow'>
|
||||
<ImageOff size={100} strokeWidth={1} color='hsl(215.4 16.3% 46.9%)' />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default Poster;
|
||||
@@ -0,0 +1,15 @@
|
||||
import { FC } from 'react';
|
||||
import { Star } from 'lucide-react';
|
||||
|
||||
interface Props {
|
||||
rating?: number;
|
||||
}
|
||||
|
||||
const Rating: FC<Props> = ({ rating }) => (
|
||||
<span className='flex items-center'>
|
||||
<Star size={18} className='mr-1 fill-white' />
|
||||
<p className='mt-[3px]'>{rating?.toFixed(1)}</p>
|
||||
</span>
|
||||
);
|
||||
|
||||
export default Rating;
|
||||
@@ -0,0 +1,25 @@
|
||||
import { create, InstanceProps } from 'react-modal-promise';
|
||||
|
||||
import { Modal } from '@/components/ui/dialog';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { usePrefix, useSetPrefix } from '@/hooks/useMagnetHosts';
|
||||
|
||||
const showHostManagerModal = create<InstanceProps<string>>(({ onResolve, onReject }) => {
|
||||
const prefix = usePrefix();
|
||||
const setPrefix = useSetPrefix();
|
||||
|
||||
return (
|
||||
<Modal className='p-2 max-w-[200px] m-auto' onClose={onReject}>
|
||||
<Input
|
||||
autoFocus
|
||||
placeholder='Enter address of your torrents streamer'
|
||||
value={prefix}
|
||||
className='focus-visible:ring-0'
|
||||
onChange={({ target: { value } }) => setPrefix(value)}
|
||||
onKeyDown={({ key }) => key === 'Enter' && onResolve(prefix)}
|
||||
/>
|
||||
</Modal>
|
||||
);
|
||||
});
|
||||
|
||||
export default showHostManagerModal;
|
||||
@@ -0,0 +1,31 @@
|
||||
import { FC } from 'react';
|
||||
import { CaretSortIcon } from '@radix-ui/react-icons';
|
||||
import { ArrowDownIcon } from 'lucide-react';
|
||||
|
||||
import { Sort } from '@/api/parsers';
|
||||
import { TableHead } from '@/components/ui/table';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
interface Props {
|
||||
title: string;
|
||||
sort: Sort;
|
||||
value: Sort;
|
||||
sortable: boolean;
|
||||
onChange: (sort: Sort) => void;
|
||||
}
|
||||
|
||||
const TableHeadSortable: FC<Props> = ({ title, sort, value, sortable, onChange }) => {
|
||||
const handleSort = !sortable ? undefined : () => onChange(value === Sort.seeds ? Sort.size : Sort.seeds);
|
||||
|
||||
return (
|
||||
<TableHead onClick={handleSort} className={cn('select-none px-2', sortable && 'cursor-pointer')}>
|
||||
<div className='flex gap-1 items-center'>
|
||||
<span>{title}</span>
|
||||
{sortable &&
|
||||
(sort === value ? <ArrowDownIcon className='ml-2 h-4 w-4' /> : <CaretSortIcon className='ml-2 h-4 w-4' />)}
|
||||
</div>
|
||||
</TableHead>
|
||||
);
|
||||
};
|
||||
|
||||
export default TableHeadSortable;
|
||||
@@ -0,0 +1,6 @@
|
||||
import { TPBTorrents, YTSTorrents } from '@/api';
|
||||
|
||||
export const providers = {
|
||||
yts: { label: 'YTS', key: 'yts', queryFn: YTSTorrents, sortable: false },
|
||||
tpb: { label: 'The Pirate Bay', key: 'tpb', queryFn: TPBTorrents, sortable: true }
|
||||
};
|
||||
@@ -0,0 +1,150 @@
|
||||
import React, { Fragment, useState } from 'react';
|
||||
import { create, InstanceProps } from 'react-modal-promise';
|
||||
import { Cast, Copy, Loader, Magnet } from 'lucide-react';
|
||||
|
||||
import { Sort } from '@/api/parsers';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Modal } from '@/components/ui/dialog';
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
|
||||
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
||||
import { useCastMagnet, usePrefix } from '@/hooks/useMagnetHosts';
|
||||
|
||||
import TableHeadSortable from './components/TableHeadSortable';
|
||||
import { providers } from './constants';
|
||||
import showHostManagerModal from './HostManager';
|
||||
import useTorrents from './useTorrents';
|
||||
|
||||
interface Props extends InstanceProps<void> {
|
||||
showId: Show['id'];
|
||||
showType: Show['type'];
|
||||
title: string;
|
||||
year: number;
|
||||
}
|
||||
|
||||
const showTorrentsModal = create(({ title, year, showType, showId, onResolve }: Props) => {
|
||||
const isMobile = matchMedia('(max-width: 600px)').matches;
|
||||
const prefix = usePrefix();
|
||||
const [sort, setSort] = useState<Sort>(Sort.seeds);
|
||||
const [withYear, setWithYear] = useState(true);
|
||||
const [{ key, queryFn, sortable }, setProvider] = useState(providers.yts);
|
||||
|
||||
const query = key === providers.yts.key ? title : withYear ? `${title} ${year || ''}` : title;
|
||||
|
||||
const {
|
||||
data: torrents,
|
||||
isLoading,
|
||||
isFetched
|
||||
} = useTorrents({
|
||||
query,
|
||||
sort,
|
||||
showId,
|
||||
showType,
|
||||
key,
|
||||
queryFn
|
||||
});
|
||||
|
||||
const cast = useCastMagnet();
|
||||
|
||||
return (
|
||||
<Modal className='p-0' onClose={onResolve}>
|
||||
<Table className='rounded-xl overflow-hidden'>
|
||||
<TableHeader>
|
||||
<TableRow className='!border-b-0'>
|
||||
<TableHead className='px-2' colSpan={4}>
|
||||
<div className='grid grid-cols-[1fr_auto] items-center gap-3'>
|
||||
<span className='overflow-ellipsis overflow-hidden text-base whitespace-nowrap'>{title}</span>
|
||||
<Button
|
||||
className='h-6 p-2'
|
||||
variant={withYear ? 'default' : 'ghost'}
|
||||
onClick={() => setWithYear((withYear) => !withYear)}
|
||||
>
|
||||
{year}
|
||||
</Button>
|
||||
</div>
|
||||
</TableHead>
|
||||
</TableRow>
|
||||
<TableRow className='!border-b-0'>
|
||||
<th colSpan={5}>
|
||||
<Tabs defaultValue={key} value={key} className='w-full rounded-none'>
|
||||
<TabsList className='grid w-full grid-cols-2'>
|
||||
{Object.values(providers).map((provider) => (
|
||||
<TabsTrigger key={provider.key} value={provider.key} onClick={() => setProvider(provider)}>
|
||||
{provider.label}
|
||||
</TabsTrigger>
|
||||
))}
|
||||
</TabsList>
|
||||
</Tabs>
|
||||
</th>
|
||||
</TableRow>
|
||||
<TableRow>
|
||||
{!isMobile && <TableHead className='px-2'>Title</TableHead>}
|
||||
<TableHeadSortable sortable={sortable} title='Size' sort={Sort.size} value={sort} onChange={setSort} />
|
||||
<TableHeadSortable sortable={sortable} title='Seeders' sort={Sort.seeds} value={sort} onChange={setSort} />
|
||||
<TableHead className='px-2 cursor-pointer select-none' onClick={() => showHostManagerModal()} />
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{torrents?.map((torrent) => (
|
||||
<Fragment key={torrent.magnet + torrent.id}>
|
||||
{isMobile && (
|
||||
<TableRow className='border-b-0'>
|
||||
<TableCell className='break-all p-2' colSpan={4}>
|
||||
{torrent.title}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)}
|
||||
<TableRow>
|
||||
{!isMobile && <TableCell className='break-all p-2'>{torrent.title}</TableCell>}
|
||||
<TableCell className='p-2'>
|
||||
<div className='flex items-center gap-1 shrink-0'>{torrent.size}</div>
|
||||
</TableCell>
|
||||
<TableCell className='p-2'>{torrent.seeders}</TableCell>
|
||||
<TableCell className='text-center p-1 pr-2'>
|
||||
<div className='flex gap-2 justify-end'>
|
||||
<a href={torrent.magnet} className='flex items-center justify-center h-8 w-8 border rounded-lg'>
|
||||
<Magnet size={20} />
|
||||
</a>
|
||||
<div
|
||||
className='flex items-center justify-center h-8 w-8 border rounded-lg cursor-pointer'
|
||||
onClick={() => navigator.clipboard.writeText(torrent.magnet)}
|
||||
>
|
||||
<Copy size={20} />
|
||||
</div>
|
||||
{prefix && !prefix.includes('{host}') && (
|
||||
<div
|
||||
onClick={() => cast(torrent.magnet!)}
|
||||
className='flex items-center justify-center h-8 w-8 border rounded-lg cursor-pointer'
|
||||
>
|
||||
<Cast size={20} />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
</Fragment>
|
||||
))}
|
||||
{isLoading && (
|
||||
<TableRow>
|
||||
<TableCell colSpan={4}>
|
||||
<div className='h-40 w-full flex items-center justify-center'>
|
||||
<Loader className='animate-spin' />
|
||||
</div>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)}
|
||||
{!isLoading && isFetched && !torrents?.length && (
|
||||
<TableRow>
|
||||
<TableCell colSpan={4}>
|
||||
<div className='h-40 w-full flex items-center justify-center text-xl text-muted-foreground'>
|
||||
Nothing was found
|
||||
</div>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</Modal>
|
||||
);
|
||||
});
|
||||
|
||||
export default showTorrentsModal;
|
||||
@@ -0,0 +1,27 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { Sort } from '@/api/parsers';
|
||||
|
||||
const useTorrents = ({
|
||||
query,
|
||||
sort,
|
||||
showId,
|
||||
showType,
|
||||
key,
|
||||
queryFn
|
||||
}: {
|
||||
showId: Show['id'];
|
||||
showType: Show['type'];
|
||||
key: string;
|
||||
query: string;
|
||||
sort: Sort;
|
||||
queryFn: (params: { query: string; sort: Sort; showId: Show['id']; showType: Show['type'] }) => Promise<Torrent[]>;
|
||||
}) => {
|
||||
return useQuery({
|
||||
enabled: Boolean(query),
|
||||
queryKey: [key, query, sort, showId, showType],
|
||||
queryFn: () => queryFn({ query, sort, showId, showType })
|
||||
});
|
||||
};
|
||||
|
||||
export default useTorrents;
|
||||
@@ -0,0 +1,39 @@
|
||||
'use client';
|
||||
|
||||
import { ReactNode } from 'react';
|
||||
import { create, InstanceProps } from 'react-modal-promise';
|
||||
|
||||
import { Modal } from '@/components/ui/dialog';
|
||||
|
||||
import useTrailer from './useTrailer';
|
||||
|
||||
interface Props {
|
||||
showId: Show['id'];
|
||||
showType?: Show['type'];
|
||||
children: ({ onPlay, disabled }: { onPlay: () => void; disabled: boolean }) => ReactNode;
|
||||
}
|
||||
|
||||
const showTrailerModal = create(({ trailerKey, onResolve }: { trailerKey?: string } & InstanceProps<void>) => {
|
||||
if (!trailerKey) return;
|
||||
|
||||
return (
|
||||
<Modal className='block p-0 max-w-[930px] border-none bg-black' style={{ aspectRatio: '16/9' }} onClose={onResolve}>
|
||||
<iframe
|
||||
allowFullScreen
|
||||
allow='autoplay'
|
||||
className='border-none rounded-lg'
|
||||
width='100%'
|
||||
height='100%'
|
||||
src={`//www.youtube.com/embed/${trailerKey}?autoplay=1`}
|
||||
/>
|
||||
</Modal>
|
||||
);
|
||||
});
|
||||
|
||||
const ToggleTrailer = ({ showId, showType = 'movie', children }: Props) => {
|
||||
const { data: trailer, isFetched } = useTrailer({ showId, showType });
|
||||
|
||||
return children({ onPlay: () => showTrailerModal({ trailerKey: trailer?.key }), disabled: isFetched && !trailer });
|
||||
};
|
||||
|
||||
export default ToggleTrailer;
|
||||
@@ -0,0 +1,21 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { trailers } from '@/api';
|
||||
|
||||
interface Props {
|
||||
showId: Show['id'];
|
||||
showType: Show['type'];
|
||||
}
|
||||
|
||||
const useTrailer = ({ showId, showType = 'movie' }: Props) =>
|
||||
useQuery({
|
||||
enabled: Boolean(showId),
|
||||
queryKey: ['trailers', showId, showType],
|
||||
queryFn: () => trailers({ showId, showType }),
|
||||
select(trailers) {
|
||||
const trailer = trailers[0];
|
||||
return trailer?.site === 'YouTube' ? trailer : null;
|
||||
}
|
||||
});
|
||||
|
||||
export default useTrailer;
|
||||
@@ -0,0 +1,48 @@
|
||||
import { DetailedHTMLProps, FC, HTMLAttributes } from 'react';
|
||||
|
||||
import Poster from '@/components/Show/components/Poster';
|
||||
import Rating from '@/components/Show/components/Rating';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
import Favorite from './components/Favorite';
|
||||
import Handlers from './components/Handlers';
|
||||
|
||||
interface Props {
|
||||
show?: Show;
|
||||
onClick?: () => void;
|
||||
className?: string;
|
||||
containerProps?: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>;
|
||||
}
|
||||
|
||||
const Show: FC<Props> = ({ show, onClick, className, containerProps }) => {
|
||||
if (!show) return null;
|
||||
|
||||
return (
|
||||
<div
|
||||
{...(containerProps || {})}
|
||||
className={cn(
|
||||
'card-aspect-ratio relative bg-card text-card-foreground shadow-sm transition-shadow hover:shadow-lg overflow-hidden rounded-lg text-lg',
|
||||
className
|
||||
)}
|
||||
>
|
||||
<div className='flex w-full h-full'>
|
||||
<Poster title={show.title} poster={show.poster} />
|
||||
</div>
|
||||
<div className='text-white absolute w-full h-full top-0 left-0 rounded-md flex flex-col justify-between p-2 bg-vignette'>
|
||||
<Handlers show={show} onClick={onClick} />
|
||||
<div className='grid grid-cols-[1fr_auto] opacity-75 gap-2'>
|
||||
<span className='overflow-ellipsis overflow-hidden whitespace-nowrap'>{show.title}</span>
|
||||
{Boolean(show.release) && <span>{new Date(show.release).getFullYear()}</span>}
|
||||
</div>
|
||||
<div className='opacity-75 mb-[-5px]'>
|
||||
<span className='select-none flex w-full items-center ml-auto gap-1 mt-[-8px] justify-between z-[1]'>
|
||||
<Favorite show={show} />
|
||||
<Rating rating={show.rating} />
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default Show;
|
||||
Reference in new issue
Block a user