2025-01-19 11:03:08 +00:00
|
|
|
"use client";
|
|
|
|
|
import { Queries } from "@/api/queries";
|
|
|
|
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
|
|
|
|
import { useState, useEffect } from "react";
|
|
|
|
|
import ReactPlayer from "react-player";
|
|
|
|
|
import Loader from "./Loader";
|
|
|
|
|
import Image from "next/image";
|
|
|
|
|
import axios from "axios";
|
|
|
|
|
|
2024-08-19 12:44:56 +00:00
|
|
|
interface IProps {
|
|
|
|
|
maxWidth?: string;
|
|
|
|
|
maxHeight?: string;
|
|
|
|
|
video_id: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const VideoPlayer = ({ maxHeight, maxWidth, video_id }: IProps) => {
|
|
|
|
|
const [hasWindow, setHasWindow] = useState<boolean>(false);
|
|
|
|
|
const [hasStartedPlaying, setHasStartedPlaying] = useState<boolean>(false);
|
2025-01-19 11:03:08 +00:00
|
|
|
const [canDownload, setCanDownload] = useState<boolean>(false); // State to store canDownload
|
2024-08-19 12:44:56 +00:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-01-19 11:03:08 +00:00
|
|
|
if (typeof window !== "undefined") {
|
2024-08-19 12:44:56 +00:00
|
|
|
setHasWindow(true);
|
|
|
|
|
}
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const { data, isFetching, error } = useQuery({
|
2025-01-19 11:03:08 +00:00
|
|
|
queryKey: ["video", `video:${video_id}`],
|
|
|
|
|
queryFn: async () => {
|
|
|
|
|
const response = await Queries.getVideo(video_id);
|
2025-02-25 11:27:05 +00:00
|
|
|
|
|
|
|
|
// console.log(response);
|
|
|
|
|
// if (response.data.is_downloadable == 1) {
|
|
|
|
|
// setCanDownload(true);
|
|
|
|
|
// } else {
|
|
|
|
|
// setCanDownload(false);
|
|
|
|
|
// } // Set canDownload from API
|
2025-01-19 11:03:08 +00:00
|
|
|
return response;
|
|
|
|
|
},
|
2024-08-19 12:44:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
async function addViews() {
|
2025-01-19 11:03:08 +00:00
|
|
|
return axios.post(
|
|
|
|
|
`https://turkmentv.gov.tm/v2/api/material/${video_id}/views/increment`
|
|
|
|
|
);
|
2024-08-19 12:44:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mutation = useMutation(() => addViews());
|
|
|
|
|
|
|
|
|
|
const onPlayHandler = () => {
|
|
|
|
|
if (!hasStartedPlaying) {
|
|
|
|
|
mutation.mutate();
|
|
|
|
|
setHasStartedPlaying(true);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-20 11:53:01 +00:00
|
|
|
if (isFetching)
|
|
|
|
|
return (
|
|
|
|
|
<div className="lg:w-[700px] md:w-[550px] w-full h-[200px] sm:h-[250px] md:h-[350px] lg:h-[420px] flex items-center justify-center">
|
|
|
|
|
<Loader height={700} />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2024-08-19 12:44:56 +00:00
|
|
|
if (error) return <h1>{JSON.stringify(error)}</h1>;
|
|
|
|
|
|
|
|
|
|
return (
|
2025-01-19 11:03:08 +00:00
|
|
|
<div className="w-full h-full">
|
2024-08-19 12:44:56 +00:00
|
|
|
{hasWindow ? (
|
2025-01-19 11:03:08 +00:00
|
|
|
data?.data.content_url.endsWith(".mp4") ? (
|
2024-08-19 12:44:56 +00:00
|
|
|
<div className="lg:w-[700px] md:w-[550px] w-full h-[200px] sm:h-[250px] md:h-[350px] lg:h-[420px]">
|
|
|
|
|
<video
|
|
|
|
|
controls
|
2025-02-25 11:27:05 +00:00
|
|
|
controlsList={
|
|
|
|
|
data?.data.is_downloadable === 0 ? "nodownload" : ""
|
|
|
|
|
} // Conditionally enable/disable download
|
2024-08-19 12:44:56 +00:00
|
|
|
poster={data?.data.banner_url}
|
|
|
|
|
playsInline
|
2025-02-25 11:27:05 +00:00
|
|
|
>
|
|
|
|
|
<source
|
|
|
|
|
src={data!.data.video_stream_url}
|
|
|
|
|
type="video/mp4"
|
|
|
|
|
onPlay={() => onPlayHandler()}
|
|
|
|
|
/>
|
|
|
|
|
</video>
|
2024-08-19 12:44:56 +00:00
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="flex flex-col gap-4 h-fit">
|
|
|
|
|
<div className="relative lg:w-[700px] md:w-[550px] w-full h-[200px] sm:h-[400px] md:h-[420px]">
|
|
|
|
|
{data?.data.banner_url ? (
|
2025-01-19 11:03:08 +00:00
|
|
|
<Image
|
|
|
|
|
src={data?.data.banner_url}
|
|
|
|
|
width={700}
|
|
|
|
|
height={420}
|
|
|
|
|
alt={"image"}
|
|
|
|
|
/>
|
2024-08-19 12:44:56 +00:00
|
|
|
) : null}
|
|
|
|
|
</div>
|
2025-01-19 11:03:08 +00:00
|
|
|
<audio
|
|
|
|
|
controls
|
|
|
|
|
controlsList={canDownload ? "" : "nodownload"} // Conditionally enable/disable download
|
|
|
|
|
className="w-full rounded bg-white"
|
|
|
|
|
onPlay={() => onPlayHandler()}
|
|
|
|
|
>
|
2024-08-19 12:44:56 +00:00
|
|
|
<source src={data?.data.content_url} />
|
|
|
|
|
</audio>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default VideoPlayer;
|