'use client'; import { useState, useEffect } from 'react'; import { Button } from '@/components/ui/button'; import { Loader2, AlertTriangle, Copy, Check } from 'lucide-react'; interface Torrent { magnet: string; title?: string; name?: string; quality?: string; seeders?: number; size_gb?: number; } interface GroupedTorrents { [quality: string]: Torrent[]; } interface SeasonGroupedTorrents { [season: string]: GroupedTorrents; } interface TorrentSelectorProps { imdbId: string | null; type: 'movie' | 'tv'; totalSeasons?: number; } export default function TorrentSelector({ imdbId, type, totalSeasons }: TorrentSelectorProps) { const [torrents, setTorrents] = useState(null); const [selectedSeason, setSelectedSeason] = useState(type === 'movie' ? 1 : null); const [selectedMagnet, setSelectedMagnet] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [isCopied, setIsCopied] = useState(false); // Для TV показов автоматически выбираем первый сезон useEffect(() => { if (type === 'tv' && totalSeasons && totalSeasons > 0 && !selectedSeason) { setSelectedSeason(1); } }, [type, totalSeasons, selectedSeason]); useEffect(() => { if (!imdbId) return; // Для фильмов загружаем сразу if (type === 'movie') { fetchTorrents(); } // Для TV показов загружаем только когда выбран сезон else if (type === 'tv' && selectedSeason) { fetchTorrents(); } }, [imdbId, type, selectedSeason]); const fetchTorrents = async () => { setLoading(true); setError(null); setSelectedMagnet(null); try { const apiUrl = process.env.NEXT_PUBLIC_API_URL; if (!apiUrl) { throw new Error('API URL не настроен'); } let url = `${apiUrl}/torrents/search/${imdbId}?type=${type}`; if (type === 'tv' && selectedSeason) { url += `&season=${selectedSeason}`; } console.log('API URL:', url, 'IMDB:', imdbId, 'season:', selectedSeason); const res = await fetch(url); if (!res.ok) { throw new Error('Failed to fetch torrents'); } const data = await res.json(); if (data.total === 0) { setError('Торренты не найдены.'); } else { setTorrents(data.results as Torrent[]); } } catch (err) { console.error(err); setError('Не удалось загрузить список торрентов.'); } finally { setLoading(false); } }; const handleQualitySelect = (torrent: Torrent) => { setSelectedMagnet(torrent.magnet); setIsCopied(false); }; const handleCopy = () => { if (!selectedMagnet) return; navigator.clipboard.writeText(selectedMagnet); setIsCopied(true); setTimeout(() => setIsCopied(false), 2000); }; if (loading) { return (
Загрузка торрентов...
); } if (error) { return (
{error}
); } if (!torrents) return null; const renderTorrentButtons = (list: Torrent[]) => { if (!list?.length) { return (

Торрентов для выбранного сезона нет.

); } return list.map(torrent => { const size = torrent.size_gb; const label = torrent.title || torrent.name || 'Раздача'; return ( ); }); }; return (
{type === 'tv' && totalSeasons && totalSeasons > 0 && (

Сезоны

{Array.from({ length: totalSeasons }, (_, i) => i + 1).map(season => ( ))}
)} {selectedSeason && torrents && (

Раздачи

{renderTorrentButtons(torrents)}
)} {selectedMagnet && (

Magnet-ссылка

{selectedMagnet}
)}
); }