feat: torrent player
This commit is contained in:
1 parent
6788dea0cd
commit
b379bc229d
7 files changed
+4170
-17
No files matched your search
@@ -0,0 +1,54 @@
|
||||
'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;
|
||||
@@ -0,0 +1,8 @@
|
||||
import { create } from 'react-modal-promise';
|
||||
import dynamic from 'next/dynamic';
|
||||
|
||||
const Player = dynamic(() => import('./Player'), { ssr: false });
|
||||
|
||||
const showPlayer = create(Player);
|
||||
|
||||
export default showPlayer;
|
||||
@@ -1,6 +1,6 @@
|
||||
import React, { Fragment, useState } from 'react';
|
||||
import { create, InstanceProps } from 'react-modal-promise';
|
||||
import { Cast, Copy, Loader, Magnet } from 'lucide-react';
|
||||
import { Cast, Copy, Loader, Magnet, Play } from 'lucide-react';
|
||||
|
||||
import { Sort } from '@/api/parsers';
|
||||
import { Button } from '@/components/ui/button';
|
||||
@@ -9,6 +9,7 @@ import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@
|
||||
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
||||
import { useCastMagnet, usePrefix } from '@/hooks/useMagnetHosts';
|
||||
|
||||
import showPlayer from './components/Player';
|
||||
import TableHeadSortable from './components/TableHeadSortable';
|
||||
import { providers } from './constants';
|
||||
import showHostManagerModal from './HostManager';
|
||||
@@ -101,6 +102,12 @@ const showTorrentsModal = create(({ title, year, showType, showId, onResolve }:
|
||||
<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>
|
||||
<a href={torrent.magnet} className='flex items-center justify-center h-8 w-8 border rounded-lg'>
|
||||
<Magnet size={20} />
|
||||
</a>
|
||||
|
||||
Reference in new issue
Block a user