'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); useEffect(() => { if (typeof window !== 'undefined') { setHasWindow(true); } }, []); const { data, isFetching, error } = useQuery({ queryKey: ['video', `video:${video_id}`], queryFn: () => Queries.getVideo(video_id), }); async function addViews() { return axios.post(`https://turkmentv.gov.tm/v2/api/material/${video_id}/views/increment`); } const mutation = useMutation(() => addViews()); const onPlayHandler = () => { // Check if the video has not started playing before if (!hasStartedPlaying) { // Send a POST request to the server mutation.mutate(); // Update the state to indicate that the video has started playing 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;