aboutsummaryrefslogtreecommitdiff
path: root/src/app/kdrama/components/episodesContainer.jsx
blob: 984ece5365c5284f94b79439921b838144918c73 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"use client";

import { MediaPlayer, MediaProvider } from "@vidstack/react";
import "@vidstack/react/player/styles/default/theme.css";
import "@vidstack/react/player/styles/default/layouts/video.css";
import {
	defaultLayoutIcons,
	DefaultVideoLayout,
} from "@vidstack/react/player/layouts/default";
import { Select, SelectItem, Button, Skeleton } from "@nextui-org/react";
import { useState, useEffect } from "react";

import { lexend } from "../../../../config/fonts";
import { videoLink } from "./requests";

const EpisodesContainer = ({ data: data }) => {
	const [videolink, setVideoLink] = useState("");
	const [loading, setLoading] = useState(<></>);

	async function handleSelectChange(episodeId) {
		setVideoLink("");
		setLoading(
			<div className="w-full flex items-center gap-3">
				<div className="w-full flex flex-col gap-2">
					<Skeleton className="h-44 rounded-lg lg:h-96" />
				</div>
			</div>
		);
		const videoURL = await videoLink(episodeId, data.id);
		setLoading(<></>);
		setVideoLink(videoURL);
	}

	return (
		<section>
			<div className="flex w-full flex-wrap md:flex-nowrap gap-4 my-2">
				<Select
					label="Select Episode"
					className={`${lexend.className} max-w-xs`}
				>
					{data.episodes && data.episodes.length > 0 ? (
						data.episodes.map((item, index) => (
							<SelectItem
								key={index}
								textValue={item.episode}
								onClick={async () =>
									await handleSelectChange(item.id)
								}
								className={lexend.className}
							>
								{item.episode}
							</SelectItem>
						))
					) : (
						<SelectItem disabled className={lexend.className}>
							No episodes available right now
						</SelectItem>
					)}
				</Select>
			</div>

			{loading}
			{videolink && (
				<div>
					<MediaPlayer
						title={data.title}
						src={videolink}
						aspectRatio="16/9"
						load="eager"
						playsInline
						volume={0.8}
						autoPlay
					>
						<MediaProvider />
						<DefaultVideoLayout icons={defaultLayoutIcons} />
					</MediaPlayer>
				</div>
			)}
		</section>
	);
};

export default EpisodesContainer;