"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"; interface IProps { maxWidth?: string; maxHeight?: string; video_id: number; } const VideoPlayer = ({ maxHeight, maxWidth, video_id }: IProps) => { const [hasWindow, setHasWindow] = useState(false); const [hasStartedPlaying, setHasStartedPlaying] = useState(false); const [canDownload, setCanDownload] = useState(false); // State to store canDownload useEffect(() => { if (typeof window !== "undefined") { setHasWindow(true); } }, []); const { data, isFetching, error } = useQuery({ queryKey: ["video", `video:${video_id}`], queryFn: async () => { const response = await Queries.getVideo(video_id); // console.log(response); // if (response.data.is_downloadable == 1) { // setCanDownload(true); // } else { // setCanDownload(false); // } // Set canDownload from API return response; }, }); async function addViews() { return axios.post( `https://turkmentv.gov.tm/v2/api/material/${video_id}/views/increment` ); } const mutation = useMutation(() => addViews()); const onPlayHandler = () => { if (!hasStartedPlaying) { mutation.mutate(); setHasStartedPlaying(true); } }; if (isFetching) return (
); if (error) return

{JSON.stringify(error)}

; return (
{hasWindow ? ( data?.data.content_url.endsWith(".mp4") ? (
) : (
{data?.data.banner_url ? ( {"image"} ) : null}
) ) : null}
); }; export default VideoPlayer;