feat: torrents modal view for mobile
This commit is contained in:
1 parent
b2e973e09e
commit
c256b7fb7c
4 files changed
+59
-48
No files matched your search
@@ -2,20 +2,21 @@ import { create, InstanceProps } from 'react-modal-promise';
|
||||
|
||||
import { Modal } from '@/components/ui/dialog';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { useHost, useSetHost } from '@/hooks/useMagnetHosts';
|
||||
import { usePrefix, useSetPrefix } from '@/hooks/useMagnetHosts';
|
||||
|
||||
const showHostManagerModal = create<InstanceProps<string>>(({ onResolve, onReject }) => {
|
||||
const host = useHost();
|
||||
const setHost = useSetHost();
|
||||
const prefix = usePrefix();
|
||||
const setPrefix = useSetPrefix();
|
||||
|
||||
return (
|
||||
<Modal className='p-2 max-w-[200px] m-auto' onClose={onReject}>
|
||||
<Input
|
||||
autoFocus
|
||||
placeholder='Enter host of Elementum'
|
||||
value={host}
|
||||
placeholder='Enter address of your torrents streamer'
|
||||
value={prefix}
|
||||
className='focus-visible:ring-0'
|
||||
onChange={({ target: { value } }) => setHost(value)}
|
||||
onKeyDown={({ key }) => key === 'Enter' && onResolve(host)}
|
||||
onChange={({ target: { value } }) => setPrefix(value)}
|
||||
onKeyDown={({ key }) => key === 'Enter' && onResolve(prefix)}
|
||||
/>
|
||||
</Modal>
|
||||
);
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import React, { Dispatch, FC, SetStateAction, useState } from 'react';
|
||||
import React, { Dispatch, FC, Fragment, SetStateAction, useState } from 'react';
|
||||
import { create, InstanceProps } from 'react-modal-promise';
|
||||
import { ArrowDownIcon, CaretSortIcon } from '@radix-ui/react-icons';
|
||||
import { Cast, Loader, Magnet } from 'lucide-react';
|
||||
import { Cast, Copy,Loader, Magnet } from 'lucide-react';
|
||||
|
||||
import useTorrents from '@/app/(site)/useTorrents';
|
||||
import { Modal } from '@/components/ui/dialog';
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
|
||||
import { Toggle } from '@/components/ui/toggle';
|
||||
import { useCastMagnet, useHost, useSetHost } from '@/hooks/useMagnetHosts';
|
||||
import { useCastMagnet, usePrefix } from '@/hooks/useMagnetHosts';
|
||||
import { ORDER } from '@/lib/api/parsers/pirate-bay';
|
||||
|
||||
import showHostManagerModal from './HostManager';
|
||||
@@ -41,7 +41,7 @@ const TableHeadSortable = ({
|
||||
}
|
||||
className='cursor-pointer select-none px-2'
|
||||
>
|
||||
<div className='flex gap-1 items-center justify-center'>
|
||||
<div className='flex gap-1 items-center'>
|
||||
<span>{title}</span>
|
||||
{value.by === by ? <ArrowDownIcon className='ml-2 h-4 w-4' /> : <CaretSortIcon className='ml-2 h-4 w-4' />}
|
||||
</div>
|
||||
@@ -50,7 +50,8 @@ const TableHeadSortable = ({
|
||||
};
|
||||
|
||||
const Torrents: FC<Props> = ({ title, year, onResolve }) => {
|
||||
const host = useHost();
|
||||
const isMobile = matchMedia('(max-width: 600px)').matches;
|
||||
const prefix = usePrefix();
|
||||
const [sorting, setSorting] = useState<Sorting>({ by: ORDER.seeders });
|
||||
const [withYear, setWithYear] = useState(true);
|
||||
|
||||
@@ -68,7 +69,8 @@ const Torrents: FC<Props> = ({ title, year, onResolve }) => {
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead className='px-2' colSpan={4}>
|
||||
{title}{' '}
|
||||
<div className='grid grid-cols-[1fr_auto] items-center gap-3'>
|
||||
<span className='overflow-ellipsis overflow-hidden text-base whitespace-nowrap'>{title}</span>
|
||||
<Toggle
|
||||
size='sm'
|
||||
className='p-2 h-7'
|
||||
@@ -77,45 +79,55 @@ const Torrents: FC<Props> = ({ title, year, onResolve }) => {
|
||||
>
|
||||
{year}
|
||||
</Toggle>
|
||||
</div>
|
||||
</TableHead>
|
||||
</TableRow>
|
||||
<TableRow>
|
||||
<TableHead className='px-2'>Title</TableHead>
|
||||
{!isMobile && <TableHead className='px-2'>Title</TableHead>}
|
||||
<TableHeadSortable title='Size' by={ORDER.size} value={sorting} onChange={setSorting} />
|
||||
<TableHeadSortable title='Seeders' by={ORDER.seeders} value={sorting} onChange={setSorting} />
|
||||
<TableHead className='px-2 text-center cursor-pointer select-none' onClick={() => showHostManagerModal()}>
|
||||
Magnet
|
||||
</TableHead>
|
||||
<TableHead className='px-2 cursor-pointer select-none' onClick={() => showHostManagerModal()} />
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{torrents?.map((torrent) => (
|
||||
<TableRow key={torrent.magnet + torrent.id}>
|
||||
<TableCell className='break-all p-2'>{torrent.title}</TableCell>
|
||||
<TableCell className='p-2'>
|
||||
<div className='flex items-center gap-1 shrink-0 justify-center'>{torrent.size}</div>
|
||||
<Fragment key={torrent.magnet + torrent.id}>
|
||||
{isMobile && (
|
||||
<TableRow className='border-b-0'>
|
||||
<TableCell className='break-all p-2' colSpan={4}>
|
||||
{torrent.title}
|
||||
</TableCell>
|
||||
<TableCell className='text-center p-2'>{torrent.seeders}</TableCell>
|
||||
<TableCell className='text-center p-1'>
|
||||
<div className='flex gap-2 m-auto justify-center'>
|
||||
{host ? (
|
||||
<a
|
||||
onClick={(event) => {
|
||||
event.preventDefault();
|
||||
cast(torrent.magnet!);
|
||||
}}
|
||||
className='flex items-center justify-center h-8 w-8 border rounded-lg'
|
||||
>
|
||||
<Cast size={20} />
|
||||
</a>
|
||||
) : (
|
||||
</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>
|
||||
|
||||
@@ -17,11 +17,10 @@ const Trailer: FC<{ trailerKey?: string } & InstanceProps<void>> = ({ trailerKey
|
||||
if (!trailerKey) return;
|
||||
|
||||
return (
|
||||
<Modal className='block p-0 min-h-[400px] border-none' style={{ aspectRatio: '16/9' }} onClose={onResolve}>
|
||||
<Modal className='block p-0 max-w-[930px] border-none bg-black' style={{ aspectRatio: '16/9' }} onClose={onResolve}>
|
||||
<iframe
|
||||
allow='autoplay'
|
||||
className='border-none rounded-lg'
|
||||
style={{ aspectRatio: '16/9' }}
|
||||
width='100%'
|
||||
height='100%'
|
||||
src={`//www.youtube.com/embed/${trailerKey}?autoplay=1`}
|
||||
|
||||
+10
-11
@@ -4,29 +4,28 @@ import { atomWithStorage } from 'jotai/utils';
|
||||
|
||||
import showHostManagerModal from '@/components/Movie/components/Torrents/HostManager';
|
||||
|
||||
const hostAtom = atomWithStorage<string>('magnet-host', '', undefined, {
|
||||
const magnetPrefixAtom = atomWithStorage<string>('magnet-prefix', 'http://{host}:65220/playuri?uri=', undefined, {
|
||||
unstable_getOnInit: true
|
||||
});
|
||||
|
||||
export const useHost = () => useAtomValue(hostAtom);
|
||||
export const usePrefix = () => useAtomValue(magnetPrefixAtom);
|
||||
|
||||
export const useSetHost = () => useSetAtom(hostAtom);
|
||||
export const useSetPrefix = () => useSetAtom(magnetPrefixAtom);
|
||||
|
||||
export const useCastMagnet = () => {
|
||||
const hosts = useHost();
|
||||
const prefix = usePrefix();
|
||||
|
||||
return useCallback(
|
||||
(magnet: string) => {
|
||||
const open = (hosts: string) =>
|
||||
hosts.split(',').forEach((host) => {
|
||||
const link = window.open(`http://${host}:65220/playuri?uri=${magnet}`, '_blank');
|
||||
const open = (prefix: string) => {
|
||||
const link = window.open(`${prefix}${magnet}`, '_blank');
|
||||
setTimeout(() => link?.close(), 1000);
|
||||
});
|
||||
};
|
||||
|
||||
if (!hosts) return showHostManagerModal().then(open);
|
||||
if (!prefix || prefix.includes('{host}')) return showHostManagerModal().then(open);
|
||||
|
||||
return Promise.resolve(open(hosts));
|
||||
return Promise.resolve(open(prefix));
|
||||
},
|
||||
[hosts]
|
||||
[prefix]
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user