"use client"; import { Spinner } from "#/components/Spinner/Spinner"; import { useUserStore } from "#/store/auth"; import { Card, Dropdown, Button } from "flowbite-react"; import { ENDPOINTS } from "#/api/config"; import { useState, useEffect } from "react"; const DropdownTheme = { floating: { target: "w-full md:w-[256px] bg-blue-600 enabled:hover:bg-blue-700 focus:ring-4 focus:outline-none focus:ring-blue-300 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800", }, }; const ButtonThemeInactive = "bg-blue-600 enabled:hover:bg-blue-700 focus:ring-4 focus:outline-none focus:ring-blue-300 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"; const ButtonThemeActive = "bg-blue-800 dark:bg-blue-600 disabled:opacity-100 dark:disabled:opacity-100"; async function _fetch(url: string) { const data = fetch(url) .then((res) => { if (res.ok) { return res.json(); } else { throw new Error("Error fetching data"); } }) .catch((err) => console.log(err)); return data; } export const ReleasePlayer = (props: { id: number }) => { const token = useUserStore((state) => state.token); const [voiceoverInfo, setVoiceoverInfo] = useState(null); const [selectedVoiceover, setSelectedVoiceover] = useState(null); const [sourcesInfo, setSourcesInfo] = useState(null); const [selectedSource, setSelectedSource] = useState(null); const [episodeInfo, setEpisodeInfo] = useState(null); const [selectedEpisode, setSelectedEpisode] = useState(null); const [isFirstLoad, setIsFirstLoad] = useState(true); useEffect(() => { async function _fetchInfo() { const voiceover = await _fetch( `${ENDPOINTS.release.episode}/${props.id}` ); setVoiceoverInfo(voiceover.types); setSelectedVoiceover(voiceover.types[0]); } _fetchInfo(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); useEffect(() => { async function _fetchInfo() { const sources = await _fetch( `${ENDPOINTS.release.episode}/${props.id}/${selectedVoiceover.id}` ); setSourcesInfo(sources.sources); setSelectedSource(sources.sources[0]); } if (selectedVoiceover) { _fetchInfo(); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [selectedVoiceover]); useEffect(() => { async function _fetchInfo(url: string) { const episodes = await _fetch(url); setEpisodeInfo(episodes.episodes); setSelectedEpisode(episodes.episodes[0]); } if (selectedSource) { let url = `${ENDPOINTS.release.episode}/${props.id}/${selectedVoiceover.id}/${selectedSource.id}`; if (token) { url = `${ENDPOINTS.release.episode}/${props.id}/${selectedVoiceover.id}/${selectedSource.id}?token=${token}`; } _fetchInfo(url); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [selectedSource, token]); useEffect(() => { async function _fetchInfo() { _fetch(`${ENDPOINTS.statistic.addHistory}/${props.id}/${selectedVoiceover.id}/${selectedSource.id}?token=${token}`); _fetch(`${ENDPOINTS.statistic.markWatched}/${props.id}/${selectedVoiceover.id}/${selectedSource.id}?token=${token}`); } if (selectedEpisode && !isFirstLoad && token) { _fetchInfo(); } if (isFirstLoad) { setIsFirstLoad(false); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [selectedEpisode]); return ( {!voiceoverInfo || !sourcesInfo || !episodeInfo ? (
) : ( <>
{voiceoverInfo.map((voiceover: any) => ( setSelectedVoiceover(voiceover)} > {voiceover.name} ))} {sourcesInfo.map((source: any) => ( setSelectedSource(source)} > {source.name} ))}
{episodeInfo.map((episode: any) => ( ))}
)}
); };