feat: added torrents list
This commit is contained in:
1 parent
5d7a3464ed
commit
dd7339b62c
11 files changed
+2303
-5
No files matched your search
@@ -6,7 +6,9 @@ import Credits from '@/components/Movie/components/Credits';
|
||||
import Poster from '@/components/Movie/components/Poster';
|
||||
import Rating from '@/components/Movie/components/Rating';
|
||||
import Title from '@/components/Movie/components/Title';
|
||||
import Torrents from '@/components/Movie/components/Torrents';
|
||||
import Trailer from '@/components/Movie/components/Trailer';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Separator } from '@/components/ui/separator';
|
||||
|
||||
interface Props {
|
||||
@@ -18,6 +20,7 @@ interface Props {
|
||||
const Preview: FC<Props> = ({ movie, className, onPersonClick }) => {
|
||||
const [isShowTrailer, setIsShowTrailer] = useState(false);
|
||||
const { data: trailer } = useTrailer({ movieId: movie?.id });
|
||||
const [isShowTorrents, setIsShowTorrents] = useState(false);
|
||||
|
||||
if (!movie) return null;
|
||||
|
||||
@@ -59,11 +62,15 @@ const Preview: FC<Props> = ({ movie, className, onPersonClick }) => {
|
||||
<div className='flex flex-col items-start grow justify-between'>
|
||||
<p className='leading-7'>{movie.overview}</p>
|
||||
</div>
|
||||
<Button size='sm' className='h-8 mt-2' onClick={() => setIsShowTorrents(true)}>
|
||||
Torrents
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<Credits movieId={movie.id} onPersonClick={onPersonClick} />
|
||||
</div>
|
||||
{isShowTrailer && <Trailer trailerKey={trailer?.key} onClose={() => setIsShowTrailer(false)} />}
|
||||
{isShowTorrents && <Torrents title={movie.title} onClose={() => setIsShowTorrents(false)} />}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
import { Dispatch, FC, SetStateAction, useMemo, useState } from 'react';
|
||||
import { ArrowDownIcon, ArrowUpIcon, CaretSortIcon } from '@radix-ui/react-icons';
|
||||
import { Loader, Magnet } from 'lucide-react';
|
||||
|
||||
import useTorrents from '@/app/(site)/useTorrents';
|
||||
import { Dialog, DialogContent } from '@/components/ui/dialog';
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
|
||||
|
||||
interface Props {
|
||||
title: string;
|
||||
onClose: () => void;
|
||||
}
|
||||
|
||||
type Sorting = {
|
||||
by: 'seeders' | 'size';
|
||||
direction: 'asc' | 'desc';
|
||||
};
|
||||
|
||||
function convertToBytes(size: string) {
|
||||
const units: Record<string, number> = { B: 1, KiB: 1024, MiB: 1024 * 1024, GiB: 1024 * 1024 * 1024 };
|
||||
const [value, unit] = size.split(' ');
|
||||
return parseFloat(value) * units[unit];
|
||||
}
|
||||
|
||||
function compareSizes(size1: string, size2: string) {
|
||||
return convertToBytes(size1) - convertToBytes(size2);
|
||||
}
|
||||
|
||||
const TableHeadSortable = ({
|
||||
title,
|
||||
by,
|
||||
onChange,
|
||||
value
|
||||
}: {
|
||||
title: string;
|
||||
by: Sorting['by'];
|
||||
value: Sorting;
|
||||
onChange: Dispatch<SetStateAction<Sorting>>;
|
||||
}) => {
|
||||
return (
|
||||
<TableHead
|
||||
onClick={() =>
|
||||
onChange((sorting) => ({
|
||||
...sorting,
|
||||
by,
|
||||
direction: sorting.by !== by ? 'desc' : sorting.direction === 'asc' ? 'desc' : 'asc'
|
||||
}))
|
||||
}
|
||||
className='cursor-pointer select-none'
|
||||
>
|
||||
<div className='flex gap-1 items-center'>
|
||||
<span>{title}</span>
|
||||
{value.by === by &&
|
||||
(value.direction === 'desc' ? (
|
||||
<ArrowDownIcon className='ml-2 h-4 w-4' />
|
||||
) : (
|
||||
<ArrowUpIcon className='ml-2 h-4 w-4' />
|
||||
))}
|
||||
{value.by !== by && <CaretSortIcon className='ml-2 h-4 w-4' />}
|
||||
</div>
|
||||
</TableHead>
|
||||
);
|
||||
};
|
||||
|
||||
const Torrents: FC<Props> = ({ title, onClose }) => {
|
||||
const { torrents, isLoading, isFetched } = useTorrents({ query: title });
|
||||
const [sorting, setSorting] = useState<Sorting>({ by: 'seeders', direction: 'desc' });
|
||||
|
||||
const sortedTorrents = useMemo(() => {
|
||||
return [...torrents].sort((a, b) => {
|
||||
if (sorting.by === 'size')
|
||||
return sorting.direction === 'asc' ? compareSizes(a.size, b.size) : compareSizes(b.size, a.size);
|
||||
|
||||
return sorting.direction === 'asc' ? a.seeders - b.seeders : b.seeders - a.seeders;
|
||||
});
|
||||
}, [torrents, sorting]);
|
||||
|
||||
return (
|
||||
<Dialog defaultOpen={true} onOpenChange={(isOpen) => !isOpen && onClose()}>
|
||||
<DialogContent className='p-0'>
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>Title</TableHead>
|
||||
<TableHeadSortable title='Size' by='size' value={sorting} onChange={setSorting} />
|
||||
<TableHeadSortable title='Seeders' by='seeders' value={sorting} onChange={setSorting} />
|
||||
<TableHead>Magnet</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{sortedTorrents.map((torrent) => (
|
||||
<TableRow key={torrent.id}>
|
||||
<TableCell>{torrent.title}</TableCell>
|
||||
<TableCell>
|
||||
<div className='flex items-center gap-1 shrink-0'>{torrent.size}</div>
|
||||
</TableCell>
|
||||
<TableCell className='text-center'>{torrent.seeders}</TableCell>
|
||||
<TableCell className='text-center'>
|
||||
<a href={torrent.magnetLink} className='flex items-center justify-center h-8 w-8 border rounded-lg'>
|
||||
<Magnet size={20} />
|
||||
</a>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
{isLoading && (
|
||||
<TableRow>
|
||||
<TableCell colSpan={4}>
|
||||
<div className='h-40 w-full flex items-center justify-center'>
|
||||
<Loader className='animate-spin' />
|
||||
</div>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)}
|
||||
{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>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
};
|
||||
|
||||
export default Torrents;
|
||||
@@ -0,0 +1,72 @@
|
||||
import * as React from 'react';
|
||||
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
const Table = React.forwardRef<HTMLTableElement, React.HTMLAttributes<HTMLTableElement>>(
|
||||
({ className, ...props }, ref) => (
|
||||
<div className='w-full overflow-auto'>
|
||||
<table ref={ref} className={cn('w-full caption-bottom text-sm', className)} {...props} />
|
||||
</div>
|
||||
)
|
||||
);
|
||||
Table.displayName = 'Table';
|
||||
|
||||
const TableHeader = React.forwardRef<HTMLTableSectionElement, React.HTMLAttributes<HTMLTableSectionElement>>(
|
||||
({ className, ...props }, ref) => <thead ref={ref} className={cn('[&_tr]:border-b', className)} {...props} />
|
||||
);
|
||||
TableHeader.displayName = 'TableHeader';
|
||||
|
||||
const TableBody = React.forwardRef<HTMLTableSectionElement, React.HTMLAttributes<HTMLTableSectionElement>>(
|
||||
({ className, ...props }, ref) => (
|
||||
<tbody ref={ref} className={cn('[&_tr:last-child]:border-0', className)} {...props} />
|
||||
)
|
||||
);
|
||||
TableBody.displayName = 'TableBody';
|
||||
|
||||
const TableFooter = React.forwardRef<HTMLTableSectionElement, React.HTMLAttributes<HTMLTableSectionElement>>(
|
||||
({ className, ...props }, ref) => (
|
||||
<tfoot ref={ref} className={cn('bg-primary font-medium text-primary-foreground', className)} {...props} />
|
||||
)
|
||||
);
|
||||
TableFooter.displayName = 'TableFooter';
|
||||
|
||||
const TableRow = React.forwardRef<HTMLTableRowElement, React.HTMLAttributes<HTMLTableRowElement>>(
|
||||
({ className, ...props }, ref) => (
|
||||
<tr
|
||||
ref={ref}
|
||||
className={cn('border-b transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted', className)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
);
|
||||
TableRow.displayName = 'TableRow';
|
||||
|
||||
const TableHead = React.forwardRef<HTMLTableCellElement, React.ThHTMLAttributes<HTMLTableCellElement>>(
|
||||
({ className, ...props }, ref) => (
|
||||
<th
|
||||
ref={ref}
|
||||
className={cn(
|
||||
'h-12 px-4 text-left align-middle font-medium text-muted-foreground [&:has([role=checkbox])]:pr-0',
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
)
|
||||
);
|
||||
TableHead.displayName = 'TableHead';
|
||||
|
||||
const TableCell = React.forwardRef<HTMLTableCellElement, React.TdHTMLAttributes<HTMLTableCellElement>>(
|
||||
({ className, ...props }, ref) => (
|
||||
<td ref={ref} className={cn('p-4 align-middle [&:has([role=checkbox])]:pr-0', className)} {...props} />
|
||||
)
|
||||
);
|
||||
TableCell.displayName = 'TableCell';
|
||||
|
||||
const TableCaption = React.forwardRef<HTMLTableCaptionElement, React.HTMLAttributes<HTMLTableCaptionElement>>(
|
||||
({ className, ...props }, ref) => (
|
||||
<caption ref={ref} className={cn('mt-4 text-sm text-muted-foreground', className)} {...props} />
|
||||
)
|
||||
);
|
||||
TableCaption.displayName = 'TableCaption';
|
||||
|
||||
export { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow };
|
||||
Reference in new issue
Block a user