2023-10-12 20:31:40 +02:00
|
|
|
import { RunOutput } from "@movie-web/providers";
|
2023-10-14 19:28:27 +02:00
|
|
|
import { useCallback, useEffect, useRef, useState } from "react";
|
2023-10-12 20:31:40 +02:00
|
|
|
import { useParams } from "react-router-dom";
|
2023-10-08 18:16:30 +02:00
|
|
|
|
2023-09-30 20:57:00 +02:00
|
|
|
import { usePlayer } from "@/components/player/hooks/usePlayer";
|
2023-10-12 20:31:40 +02:00
|
|
|
import { usePlayerMeta } from "@/components/player/hooks/usePlayerMeta";
|
2023-10-14 16:06:25 +02:00
|
|
|
import { convertRunoutputToSource } from "@/components/player/utils/convertRunoutputToSource";
|
2023-10-12 23:05:47 +02:00
|
|
|
import { MetaPart } from "@/pages/parts/player/MetaPart";
|
2023-10-12 20:31:40 +02:00
|
|
|
import { PlayerPart } from "@/pages/parts/player/PlayerPart";
|
2023-10-01 21:08:26 +02:00
|
|
|
import { ScrapingPart } from "@/pages/parts/player/ScrapingPart";
|
2023-10-12 20:31:40 +02:00
|
|
|
import { playerStatus } from "@/stores/player/slices/source";
|
2023-07-23 15:00:08 +02:00
|
|
|
|
|
|
|
export function PlayerView() {
|
2023-10-12 20:31:40 +02:00
|
|
|
const params = useParams<{
|
|
|
|
media: string;
|
|
|
|
episode?: string;
|
|
|
|
season?: string;
|
|
|
|
}>();
|
2023-10-12 23:05:47 +02:00
|
|
|
const { status, playMedia, reset } = usePlayer();
|
2023-10-12 20:31:40 +02:00
|
|
|
const { setPlayerMeta, scrapeMedia } = usePlayerMeta();
|
2023-10-12 23:38:08 +02:00
|
|
|
const [backUrl] = useState("/"); // TODO redirect to search when needed
|
2023-10-11 23:04:41 +02:00
|
|
|
|
2023-10-14 19:28:27 +02:00
|
|
|
const lastMedia = useRef(params.media);
|
2023-10-12 23:05:47 +02:00
|
|
|
useEffect(() => {
|
2023-10-14 19:28:27 +02:00
|
|
|
if (params.media === lastMedia.current) return;
|
|
|
|
lastMedia.current = params.media;
|
|
|
|
console.log("resetting");
|
2023-10-12 23:05:47 +02:00
|
|
|
reset();
|
2023-10-14 19:28:27 +02:00
|
|
|
}, [params, reset]);
|
2023-10-02 21:04:40 +02:00
|
|
|
|
2023-10-12 20:31:40 +02:00
|
|
|
const playAfterScrape = useCallback(
|
|
|
|
(out: RunOutput | null) => {
|
2023-10-14 16:06:25 +02:00
|
|
|
if (!out) return;
|
|
|
|
playMedia(convertRunoutputToSource(out));
|
2023-10-12 20:31:40 +02:00
|
|
|
},
|
|
|
|
[playMedia]
|
|
|
|
);
|
2023-10-05 22:12:25 +02:00
|
|
|
|
2023-10-12 20:31:40 +02:00
|
|
|
return (
|
2023-10-12 23:05:47 +02:00
|
|
|
<PlayerPart backUrl={backUrl}>
|
2023-10-12 20:31:40 +02:00
|
|
|
{status === playerStatus.IDLE ? (
|
2023-10-12 23:05:47 +02:00
|
|
|
<MetaPart onGetMeta={setPlayerMeta} />
|
2023-10-12 20:31:40 +02:00
|
|
|
) : null}
|
|
|
|
{status === playerStatus.SCRAPING && scrapeMedia ? (
|
|
|
|
<ScrapingPart media={scrapeMedia} onGetStream={playAfterScrape} />
|
|
|
|
) : null}
|
|
|
|
</PlayerPart>
|
2023-07-23 15:00:08 +02:00
|
|
|
);
|
|
|
|
}
|