From 46948e7914a5748afc79f01f4ce40f303b19ebc9 Mon Sep 17 00:00:00 2001 From: ner Date: Fri, 4 Oct 2024 15:46:08 +0300 Subject: [PATCH] fix: subtitles --- api/dto/Show.ts | 6 +- components/Preview/components/Actions.tsx | 1 + components/Show/components/Poster/index.tsx | 6 +- .../components/Player/components/Files.tsx | 29 ------ .../Torrents/components/Player/index.tsx | 89 +++++++++---------- .../Torrents/components/Player/useFiles.ts | 20 ----- .../components/Player/useSubtitles.ts | 16 ++++ components/Show/components/Torrents/index.tsx | 5 +- styles/player.css | 2 +- 9 files changed, 69 insertions(+), 105 deletions(-) delete mode 100644 components/Show/components/Torrents/components/Player/components/Files.tsx delete mode 100644 components/Show/components/Torrents/components/Player/useFiles.ts create mode 100644 components/Show/components/Torrents/components/Player/useSubtitles.ts diff --git a/api/dto/Show.ts b/api/dto/Show.ts index 57c9d0d..1db8cc1 100644 --- a/api/dto/Show.ts +++ b/api/dto/Show.ts @@ -9,15 +9,15 @@ const mapMoviesSeriesResponseToShows = ( id, type, overview, - backdrop: backdrop_path, - poster: poster_path, rating: vote_average, votes: vote_count, + backdrop: backdrop_path ? `https://image.tmdb.org/t/p/w1280/${backdrop_path}` : '', + poster: poster_path ? `https://image.tmdb.org/t/p/w500/${poster_path}` : '', release: 'release_date' in item ? item.release_date : item.first_air_date, title: 'release_date' in item ? item.title : item.name, genres: [] })) - .filter((show) => show.votes && show.release) + .filter((show) => show.votes && show.release && show.backdrop && show.poster) }); export default mapMoviesSeriesResponseToShows; diff --git a/components/Preview/components/Actions.tsx b/components/Preview/components/Actions.tsx index af403e9..4a26b00 100644 --- a/components/Preview/components/Actions.tsx +++ b/components/Preview/components/Actions.tsx @@ -27,6 +27,7 @@ const Actions = ({ onClose, show }: Props) => ( showId: show.id, showType: show.type, title: show.title, + backdrop: show.backdrop, year: new Date(show.release).getFullYear() }) } diff --git a/components/Show/components/Poster/index.tsx b/components/Show/components/Poster/index.tsx index fc2e625..83321d4 100644 --- a/components/Show/components/Poster/index.tsx +++ b/components/Show/components/Poster/index.tsx @@ -8,11 +8,7 @@ interface Props { const Poster: FC = ({ title, poster }) => { return poster ? ( - {title} + {title} ) : (
diff --git a/components/Show/components/Torrents/components/Player/components/Files.tsx b/components/Show/components/Torrents/components/Player/components/Files.tsx deleted file mode 100644 index 819547a..0000000 --- a/components/Show/components/Torrents/components/Player/components/Files.tsx +++ /dev/null @@ -1,29 +0,0 @@ -import { create, InstanceProps } from 'react-modal-promise'; - -import { Modal } from '@/components/ui/dialog'; -import { Table, TableBody, TableCaption, TableCell, TableRow } from '@/components/ui/table'; - -interface Props extends InstanceProps { - files: Source[]; -} - -const showFiles = create(({ onResolve, onReject, files }: Props) => { - return ( - - - Choose file to play - - {files.map((file) => ( - - onResolve(file)}> - {file.name} - - - ))} - -
-
- ); -}); - -export default showFiles; diff --git a/components/Show/components/Torrents/components/Player/index.tsx b/components/Show/components/Torrents/components/Player/index.tsx index 30a981b..d053cce 100644 --- a/components/Show/components/Torrents/components/Player/index.tsx +++ b/components/Show/components/Torrents/components/Player/index.tsx @@ -1,41 +1,39 @@ 'use client'; -import { useState } from 'react'; +import { useEffect, useState } from 'react'; import { create, InstanceProps } from 'react-modal-promise'; import { Loader } from 'lucide-react'; -import { BigPlayButton, ClosedCaptionButton, ControlBar, Player } from 'video-react'; +import { BigPlayButton, ClosedCaptionButton, ControlBar, Player, PlayerReference } from 'video-react'; import { Modal } from '@/components/ui/dialog'; import { useToast } from '@/hooks/useToast'; -import showFiles from './components/Files'; -import useFiles from './useFiles'; +import useSubtitles from './useSubtitles'; interface Props extends InstanceProps { magnet: string; + backdrop: string; } -const showPlayer = create(({ onResolve, magnet }: Props) => { - const [source, setSource] = useState(null); +type PlayerRef = PlayerReference & { video: { video: HTMLVideoElement } }; + +const showPlayer = create(({ onResolve, magnet, backdrop }: Props) => { + const [player, setPlayer] = useState(null); + const [isLoading, setIsLoading] = useState(true); const { toast } = useToast(); + const { data: subtitles } = useSubtitles({ magnet }); - const { data, isFetched } = useFiles({ - magnet, - onSuccess(files) { - if (!files.length) { - toast({ title: 'This torrent doesn\'t contain any compatible video files' }); - return onResolve(); - } + useEffect(() => { + if (!player) return; - if (files.length === 1) { - const [file] = files; + const { video } = player.video; - return setSource(file.src); - } + const handleCanPlay = () => setIsLoading(false); - showFiles({ files }).then((file) => setSource(file.src)); - } - }); + video.addEventListener('canplay', handleCanPlay); + + return () => video.removeEventListener('canplay', handleCanPlay); + }, [player]); return ( { style={{ aspectRatio: '16/9' }} onClose={onResolve} > - - {!isFetched && ( - <> -
-
- -
-
- - )} - {source && } - {isFetched && - data?.map((file) => - file.subtitles.map(({ name, content }) => ( - - )) - )} - - - + setPlayer(player as PlayerRef)}> + {subtitles?.map(({ name, content }) => ( + + ))} + { + toast({ title: 'This torrent doesn\'t contain any compatible video files' }); + setIsLoading(false); + }} + /> + {Boolean(subtitles?.length) && } + {isLoading && ( + <> +
+
+ +
+
+ + )}
); }); diff --git a/components/Show/components/Torrents/components/Player/useFiles.ts b/components/Show/components/Torrents/components/Player/useFiles.ts deleted file mode 100644 index 1ce9e65..0000000 --- a/components/Show/components/Torrents/components/Player/useFiles.ts +++ /dev/null @@ -1,20 +0,0 @@ -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; diff --git a/components/Show/components/Torrents/components/Player/useSubtitles.ts b/components/Show/components/Torrents/components/Player/useSubtitles.ts new file mode 100644 index 0000000..faba530 --- /dev/null +++ b/components/Show/components/Torrents/components/Player/useSubtitles.ts @@ -0,0 +1,16 @@ +import { useQuery } from '@tanstack/react-query'; + +interface Props { + magnet: string; +} + +const useSubtitles = ({ magnet }: Props) => + useQuery({ + queryKey: [magnet], + queryFn: () => + fetch(`${process.env.NEXT_PUBLIC_TRACKER_PROXY_BASE}/subtitles?magnet=${encodeURIComponent(magnet)}`).then( + (response) => response.json() + ) + }); + +export default useSubtitles; diff --git a/components/Show/components/Torrents/index.tsx b/components/Show/components/Torrents/index.tsx index cd20030..4db8153 100644 --- a/components/Show/components/Torrents/index.tsx +++ b/components/Show/components/Torrents/index.tsx @@ -20,9 +20,10 @@ interface Props extends InstanceProps { showType: Show['type']; title: string; year: number; + backdrop: string; } -const showTorrentsModal = create(({ title, year, showType, showId, onResolve }: Props) => { +const showTorrentsModal = create(({ title, year, showType, showId, backdrop, onResolve }: Props) => { const isMobile = matchMedia('(max-width: 600px)').matches; const prefix = usePrefix(); const [sort, setSort] = useState(Sort.seeds); @@ -109,7 +110,7 @@ const showTorrentsModal = create(({ title, year, showType, showId, onResolve }: {Boolean(key === providers.yts.key && torrent.seeders) && (
showPlayer({ magnet: torrent.magnet })} + onClick={() => showPlayer({ magnet: torrent.magnet, backdrop })} >
diff --git a/styles/player.css b/styles/player.css index 7160d48..500c5b7 100644 --- a/styles/player.css +++ b/styles/player.css @@ -24,6 +24,6 @@ border-top-right-radius: 4px; } -.video-react-big-play-button-center { +.video-react .video-react-big-play-button.video-react-big-play-button-center { display: none; }