turkmentv_front/components/VideoPlayer.tsx

115 lines
3.4 KiB
TypeScript
Raw Permalink Normal View History

"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);
const [canDownload, setCanDownload] = useState<boolean>(false); // State to store canDownload
2024-08-19 12:44:56 +00:00
useEffect(() => {
if (typeof window !== "undefined") {
2024-08-19 12:44:56 +00:00
setHasWindow(true);
}
}, []);
const { data, isFetching, error } = useQuery({
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
return response;
},
2024-08-19 12:44:56 +00:00
});
async function addViews() {
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);
}
};
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 (
<div className="w-full h-full">
2024-08-19 12:44:56 +00:00
{hasWindow ? (
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 ? (
<Image
src={data?.data.banner_url}
width={700}
height={420}
alt={"image"}
/>
2024-08-19 12:44:56 +00:00
) : null}
</div>
<audio
controls
2025-03-07 10:18:56 +00:00
controlsList={
data?.data.is_downloadable === 0 ? "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;