import { NoImageIcon } from 'icons'
import { humanizeSize, removeRedundantCharacters } from 'utils/Utils'
import { useEffect, useState } from 'react'
import { Button, ButtonGroup } from '@material-ui/core'
import ptt from 'parse-torrent-title'
import axios from 'axios'
import { viewedHost } from 'utils/Hosts'
import { GETTING_INFO, IN_DB } from 'torrentStates'
import CircularProgress from '@material-ui/core/CircularProgress'
import { useTranslation } from 'react-i18next'
import { useUpdateCache, useGetSettings } from './customHooks'
import DialogHeader from './DialogHeader'
import TorrentCache from './TorrentCache'
import Table from './Table'
import DetailedView from './DetailedView'
import {
DialogContentGrid,
MainSection,
Poster,
SectionTitle,
SectionSubName,
WidgetWrapper,
LoadingProgress,
SectionHeader,
CacheSection,
TorrentFilesSection,
Divider,
} from './style'
import { DownlodSpeedWidget, UploadSpeedWidget, PeersWidget, SizeWidget, StatusWidget } from './widgets'
import TorrentFunctions from './TorrentFunctions'
import { isFilePlayable } from './helpers'
const Loader = () => (
)
export default function DialogTorrentDetailsContent({ closeDialog, torrent }) {
const { t } = useTranslation()
const [isLoading, setIsLoading] = useState(true)
const [isDetailedCacheView, setIsDetailedCacheView] = useState(false)
const [viewedFileList, setViewedFileList] = useState()
const [playableFileList, setPlayableFileList] = useState()
const [seasonAmount, setSeasonAmount] = useState(null)
const [selectedSeason, setSelectedSeason] = useState()
const {
poster,
hash,
title,
name,
stat,
download_speed: downloadSpeed,
upload_speed: uploadSpeed,
torrent_size: torrentSize,
file_stats: torrentFileList,
} = torrent
const cache = useUpdateCache(hash)
const settings = useGetSettings(cache)
const { Capacity, PiecesCount, PiecesLength, Filled } = cache
useEffect(() => {
if (playableFileList && seasonAmount === null) {
const seasons = []
playableFileList.forEach(({ path }) => {
const currentSeason = ptt.parse(path).season
if (currentSeason) {
!seasons.includes(currentSeason) && seasons.push(currentSeason)
}
})
seasons.length && setSelectedSeason(seasons[0])
setSeasonAmount(seasons.sort((a, b) => a - b))
}
}, [playableFileList, seasonAmount])
useEffect(() => {
setPlayableFileList(torrentFileList?.filter(({ path }) => isFilePlayable(path)))
}, [torrentFileList])
useEffect(() => {
const cacheLoaded = !!Object.entries(cache).length
const torrentLoaded = stat !== GETTING_INFO && stat !== IN_DB
if (!cacheLoaded && !isLoading) setIsLoading(true)
if (cacheLoaded && isLoading && torrentLoaded) setIsLoading(false)
}, [stat, cache, isLoading])
useEffect(() => {
// getting viewed file list
axios.post(viewedHost(), { action: 'list', hash }).then(({ data }) => {
if (data) {
const lst = data.map(itm => itm.file_index).sort((a, b) => a - b)
setViewedFileList(lst)
} else setViewedFileList()
})
}, [hash])
const bufferSize = settings?.PreloadBuffer ? Capacity : 33554432 // Default is 32mb if PreloadBuffer is false
const getParsedTitle = () => {
const newNameStringArr = []
const torrentParsedName = name && ptt.parse(name)
if (title !== name) {
newNameStringArr.push(removeRedundantCharacters(title))
} else if (torrentParsedName?.title) newNameStringArr.push(removeRedundantCharacters(torrentParsedName?.title))
// These 2 checks are needed to get year and resolution from torrent name if title does not have this info
if (torrentParsedName?.year && !newNameStringArr[0].includes(torrentParsedName?.year))
newNameStringArr.push(torrentParsedName?.year)
if (torrentParsedName?.resolution && !newNameStringArr[0].includes(torrentParsedName?.resolution))
newNameStringArr.push(torrentParsedName?.resolution)
const newNameString = newNameStringArr.join('. ')
// removeRedundantCharacters is returning ".." if it was "..."
const lastDotShouldBeAdded =
newNameString[newNameString.length - 1] === '.' && newNameString[newNameString.length - 2] === '.'
return lastDotShouldBeAdded ? `${newNameString}.` : newNameString
}
return (
<>
setIsDetailedCacheView(false) })}
/>
{isLoading ? (
) : isDetailedCacheView ? (
) : (
{poster ?
: }
{title && name !== title ? (
getParsedTitle().length > 90 ? (
<>
{ptt.parse(name).title}
{getParsedTitle()}
>
) : (
<>
{getParsedTitle()}
{ptt.parse(name).title}
>
)
) : (
{getParsedTitle()}
)}
{t('Buffer')}
{!settings?.PreloadBuffer && {t('BufferNote')}}
{t('TorrentContent')}
{seasonAmount?.length > 1 && (
<>
{t('SelectSeason')}
{seasonAmount.map(season => (
))}
{t('Season')} {selectedSeason}
>
)}
)}
>
)
}