feat: torrent player
This commit is contained in:
1 parent
b379bc229d
commit
f6cb8df39b
17 files changed
+1705
-3689
No files matched your search
@@ -4,6 +4,7 @@ import { FC } from 'react';
|
||||
import { Heart } from 'lucide-react';
|
||||
|
||||
import { useFavoriteToggle } from '@/app/(site)/favorites/useFavorites';
|
||||
import useMounted from '@/hooks/useMounted';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
interface Props {
|
||||
@@ -11,11 +12,16 @@ interface Props {
|
||||
}
|
||||
|
||||
const Favorite: FC<Props> = ({ show }) => {
|
||||
const isMounted = useMounted();
|
||||
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
|
||||
onClick={toggle}
|
||||
className='cursor-pointer'
|
||||
title={isMounted && isFavorite ? 'Remove from favorites' : 'Add to favorite'}
|
||||
>
|
||||
<Heart className={cn(isMounted && isFavorite && 'fill-white')} size={20} />
|
||||
</span>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
'use client';
|
||||
|
||||
import { FC, useEffect, useRef } from 'react';
|
||||
import { InstanceProps } from 'react-modal-promise';
|
||||
import video from 'video.js';
|
||||
import WebTorrent from 'webtorrent/dist/webtorrent.min.js';
|
||||
|
||||
import { Modal } from '@/components/ui/dialog';
|
||||
|
||||
import 'video.js/dist/video-js.css';
|
||||
|
||||
interface Props extends InstanceProps<void> {
|
||||
magnet: string;
|
||||
}
|
||||
|
||||
const Player: FC<Props> = ({ onResolve, magnet }) => {
|
||||
const videoRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
let skip = false;
|
||||
const client = new WebTorrent();
|
||||
|
||||
navigator.serviceWorker.register('./sw.min.js', { scope: './' }).then((registration) => {
|
||||
const worker = registration.active || registration.waiting || registration.installing;
|
||||
|
||||
const createServer = (worker: ServiceWorker | null) => {
|
||||
if (worker?.state !== 'activated' || skip) return false;
|
||||
|
||||
client.createServer({ controller: registration });
|
||||
|
||||
console.log(client);
|
||||
};
|
||||
|
||||
if (!createServer(worker)) {
|
||||
worker?.addEventListener('statechange', ({ target }) => createServer(target as ServiceWorker));
|
||||
}
|
||||
});
|
||||
|
||||
return () => {
|
||||
skip = true;
|
||||
client.removeAllListeners();
|
||||
client.destroy();
|
||||
navigator.serviceWorker.getRegistration('./sw.min.js').then((registration) => registration?.unregister());
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<Modal className='block p-0 max-w-[930px] border-none bg-black' style={{ aspectRatio: '16/9' }} onClose={onResolve}>
|
||||
<div ref={videoRef} className='w-full h-full' />
|
||||
</Modal>
|
||||
);
|
||||
};
|
||||
|
||||
export default Player;
|
||||
@@ -1,8 +1,77 @@
|
||||
import { create } from 'react-modal-promise';
|
||||
import dynamic from 'next/dynamic';
|
||||
'use client';
|
||||
|
||||
const Player = dynamic(() => import('./Player'), { ssr: false });
|
||||
import { useState } from 'react';
|
||||
import { create, InstanceProps } from 'react-modal-promise';
|
||||
import { Loader } from 'lucide-react';
|
||||
import { BigPlayButton, ClosedCaptionButton, ControlBar, Player } from 'video-react';
|
||||
|
||||
const showPlayer = create(Player);
|
||||
import { Modal } from '@/components/ui/dialog';
|
||||
import { useToast } from '@/hooks/useToast';
|
||||
|
||||
import showFiles from './components/Files';
|
||||
import useFiles from './useFiles';
|
||||
|
||||
interface Props extends InstanceProps<void> {
|
||||
magnet: string;
|
||||
}
|
||||
|
||||
const showPlayer = create(({ onResolve, magnet }: Props) => {
|
||||
const [source, setSource] = useState<string | null>(null);
|
||||
const { toast } = useToast();
|
||||
|
||||
const { data, isFetched } = useFiles({
|
||||
magnet,
|
||||
onSuccess(files) {
|
||||
if (!files.length) {
|
||||
toast({ title: 'This torrent doesn\'t contain any compatible video files' });
|
||||
return onResolve();
|
||||
}
|
||||
|
||||
if (files.length === 1) {
|
||||
const [file] = files;
|
||||
|
||||
return setSource(file.src);
|
||||
}
|
||||
|
||||
showFiles({ files }).then((file) => setSource(file.src));
|
||||
}
|
||||
});
|
||||
|
||||
return (
|
||||
<Modal
|
||||
className='p-0 border-none rounded-lg overflow-hidden flex items-center justify-center relative'
|
||||
style={{ aspectRatio: '16/9' }}
|
||||
onClose={onResolve}
|
||||
>
|
||||
<Player autoPlay aspectRatio='16:9'>
|
||||
{!isFetched && (
|
||||
<>
|
||||
<div className='w-full h-full left-0 top-0 absolute flex items-center justify-center'>
|
||||
<div className='animate-spin'>
|
||||
<Loader color='white' />
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
{source && <source src={source} />}
|
||||
{isFetched &&
|
||||
data?.map((file) =>
|
||||
file.subtitles.map(({ name, content }) => (
|
||||
<track
|
||||
key={name}
|
||||
kind='captions'
|
||||
src={URL.createObjectURL(new Blob([content], { type: 'text/vtt' }))}
|
||||
label={name}
|
||||
/>
|
||||
))
|
||||
)}
|
||||
<ControlBar autoHide={false}>
|
||||
<ClosedCaptionButton order={7} />
|
||||
</ControlBar>
|
||||
<BigPlayButton position='center' />
|
||||
</Player>
|
||||
</Modal>
|
||||
);
|
||||
});
|
||||
|
||||
export default showPlayer;
|
||||
@@ -0,0 +1,20 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { torrentFiles } from '@/api';
|
||||
|
||||
interface Props {
|
||||
magnet: string;
|
||||
onSuccess: (files: Source[]) => void;
|
||||
}
|
||||
|
||||
const useFiles = ({ magnet, onSuccess }: Props) =>
|
||||
useQuery({
|
||||
queryKey: [magnet],
|
||||
queryFn: () =>
|
||||
torrentFiles(magnet).then((files) => {
|
||||
onSuccess(files);
|
||||
return files;
|
||||
})
|
||||
});
|
||||
|
||||
export default useFiles;
|
||||
@@ -95,19 +95,25 @@ const showTorrentsModal = create(({ title, year, showType, showId, onResolve }:
|
||||
</TableRow>
|
||||
)}
|
||||
<TableRow>
|
||||
{!isMobile && <TableCell className='break-all p-2'>{torrent.title}</TableCell>}
|
||||
{!isMobile && (
|
||||
<TableCell className='break-all p-2'>
|
||||
{torrent.title} {torrent.type && `[${torrent.type?.toUpperCase()}]`}
|
||||
</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'>
|
||||
<div
|
||||
className='flex items-center justify-center h-8 w-8 border rounded-lg cursor-pointer'
|
||||
onClick={() => showPlayer({ magnet: torrent.magnet })}
|
||||
>
|
||||
<Play size={20} />
|
||||
</div>
|
||||
{Boolean(key === providers.yts.key && torrent.seeders) && (
|
||||
<div
|
||||
className='flex items-center justify-center h-8 w-8 border rounded-lg cursor-pointer'
|
||||
onClick={() => showPlayer({ magnet: torrent.magnet })}
|
||||
>
|
||||
<Play size={20} />
|
||||
</div>
|
||||
)}
|
||||
<a href={torrent.magnet} className='flex items-center justify-center h-8 w-8 border rounded-lg'>
|
||||
<Magnet size={20} />
|
||||
</a>
|
||||
|
||||
@@ -175,7 +175,7 @@ const Modal: React.FC<{
|
||||
</DialogHeader>
|
||||
<DialogContent style={style} scrollRef={scrollRef} forceMount onClose={handleClose} className={className}>
|
||||
{children}
|
||||
<DialogDescription />
|
||||
<DialogDescription className='hidden' />
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user