From acc6f9e57b7707e0869ce3a89b33ffc0e4406e68 Mon Sep 17 00:00:00 2001 From: parsa aghaei Date: Mon, 17 Aug 2026 16:05:45 +0330 Subject: [PATCH] feat(player): add modern custom video player with auto-hide controls, speed, jumps and download --- .../application/components/VetGallery.tsx | 44 +- .../components/VideoModalPlayer.tsx | 503 ++++++++++++++++++ .../application/components/VideosPage.tsx | 48 +- 3 files changed, 517 insertions(+), 78 deletions(-) create mode 100644 frontend/application/components/VideoModalPlayer.tsx diff --git a/frontend/application/components/VetGallery.tsx b/frontend/application/components/VetGallery.tsx index 6b3e3f0a..ec8cc67f 100644 --- a/frontend/application/components/VetGallery.tsx +++ b/frontend/application/components/VetGallery.tsx @@ -6,6 +6,7 @@ import Link from "next/link"; import SafeImage from "./SafeImage"; import { useSettingsStore } from "../lib/store/settingsStore"; import { videoService, Video } from "../lib/services/videoService"; +import VideoModalPlayer from './VideoModalPlayer'; export interface TestimonialItem { id?: string; @@ -159,43 +160,12 @@ export default function VetGallery({ testimonials = [] }: { testimonials?: Testi - {/* Video Modal */} - - {selectedVideo && ( -
- setSelectedVideo(null)} - className="absolute inset-0 bg-medical-gray-900/90 backdrop-blur-xl" - /> - - - -
- )} -
+ {/* Custom Feature-Rich Video Player Modal */} + setSelectedVideo(null)} + video={selectedVideo} + /> ); } diff --git a/frontend/application/components/VideoModalPlayer.tsx b/frontend/application/components/VideoModalPlayer.tsx new file mode 100644 index 00000000..36d8a641 --- /dev/null +++ b/frontend/application/components/VideoModalPlayer.tsx @@ -0,0 +1,503 @@ +"use client"; +import React, { useState, useRef, useEffect, useCallback } from "react"; +import { motion, AnimatePresence } from "motion/react"; +import { + Play, + Pause, + RotateCcw, + RotateCw, + Volume2, + VolumeX, + Maximize, + Minimize, + Download, + Share2, + X, + Check, + ChevronDown, + ChevronUp, + Sparkles, + Loader2 +} from "lucide-react"; + +interface VideoModalPlayerProps { + isOpen: boolean; + onClose: () => void; + video: { + id?: string; + title?: string; + doctor?: string; + videoUrl?: string; + thumbnail?: string; + description?: string; + quote?: string; + } | null; +} + +const PLAYBACK_RATES = [0.5, 1, 1.25, 1.5, 1.75, 2, 2.5]; + +export default function VideoModalPlayer({ isOpen, onClose, video }: VideoModalPlayerProps) { + const videoRef = useRef(null); + const containerRef = useRef(null); + const controlsTimeoutRef = useRef | null>(null); + + const [isPlaying, setIsPlaying] = useState(false); + const [currentTime, setCurrentTime] = useState(0); + const [duration, setDuration] = useState(0); + const [volume, setVolume] = useState(1); + const [isMuted, setIsMuted] = useState(false); + const [playbackRate, setPlaybackRate] = useState(1); + const [showRateMenu, setShowRateMenu] = useState(false); + const [isFullscreen, setIsFullscreen] = useState(false); + const [isBuffering, setIsBuffering] = useState(true); + const [showControls, setShowControls] = useState(true); + const [isExpandedDesc, setIsExpandedDesc] = useState(false); + const [isCopied, setIsCopied] = useState(false); + + // Reset states on open/close + useEffect(() => { + if (isOpen) { + setIsPlaying(true); + setShowControls(true); + setIsExpandedDesc(false); + setShowRateMenu(false); + } else { + setIsPlaying(false); + setCurrentTime(0); + } + }, [isOpen, video]); + + // Handle auto-hide timer for controls + const resetControlsTimer = useCallback(() => { + setShowControls(true); + if (controlsTimeoutRef.current) { + clearTimeout(controlsTimeoutRef.current); + } + // If video is playing, auto-hide after 3.5s; if paused, stay visible + if (isPlaying) { + controlsTimeoutRef.current = setTimeout(() => { + if (!showRateMenu) { + setShowControls(false); + } + }, 3500); + } + }, [isPlaying, showRateMenu]); + + useEffect(() => { + if (!isPlaying) { + setShowControls(true); + if (controlsTimeoutRef.current) clearTimeout(controlsTimeoutRef.current); + } else { + resetControlsTimer(); + } + return () => { + if (controlsTimeoutRef.current) clearTimeout(controlsTimeoutRef.current); + }; + }, [isPlaying, resetControlsTimer]); + + const togglePlay = () => { + if (!videoRef.current) return; + if (videoRef.current.paused) { + videoRef.current.play().catch(console.error); + setIsPlaying(true); + } else { + videoRef.current.pause(); + setIsPlaying(false); + setShowControls(true); + } + }; + + const handleSeek = (e: React.ChangeEvent) => { + const time = Number(e.target.value); + if (videoRef.current) { + videoRef.current.currentTime = time; + setCurrentTime(time); + } + }; + + const skipTime = (seconds: number) => { + if (videoRef.current) { + videoRef.current.currentTime = Math.max(0, Math.min(videoRef.current.duration || 0, videoRef.current.currentTime + seconds)); + resetControlsTimer(); + } + }; + + const handleRateChange = (rate: number) => { + if (videoRef.current) { + videoRef.current.playbackRate = rate; + setPlaybackRate(rate); + setShowRateMenu(false); + resetControlsTimer(); + } + }; + + const toggleMute = () => { + if (videoRef.current) { + videoRef.current.muted = !isMuted; + setIsMuted(!isMuted); + } + }; + + const handleVolumeChange = (e: React.ChangeEvent) => { + const val = Number(e.target.value); + setVolume(val); + if (videoRef.current) { + videoRef.current.volume = val; + if (val === 0) { + setIsMuted(true); + videoRef.current.muted = true; + } else { + setIsMuted(false); + videoRef.current.muted = false; + } + } + }; + + const toggleFullscreen = async () => { + if (!containerRef.current) return; + try { + if (!document.fullscreenElement) { + await containerRef.current.requestFullscreen(); + setIsFullscreen(true); + } else { + await document.exitFullscreen(); + setIsFullscreen(false); + } + } catch (err) { + console.error('Fullscreen toggle failed:', err); + } + }; + + const handleShare = async () => { + const shareUrl = window.location.href; + if (navigator.share) { + try { + await navigator.share({ + title: video?.title || 'ویدئوی آموزشی کنینا', + text: video?.description || '', + url: shareUrl + }); + } catch { + // Fallback to clipboard + } + } else { + try { + await navigator.clipboard.writeText(shareUrl); + setIsCopied(true); + setTimeout(() => setIsCopied(false), 2500); + } catch (err) { + console.error('Copy failed:', err); + } + } + }; + + const handleDownload = () => { + if (!video?.videoUrl) return; + const a = document.createElement('a'); + a.href = video.videoUrl; + a.download = `${video.title || 'canina-video'}.mp4`; + a.target = '_blank'; + a.rel = 'noopener noreferrer'; + document.body.appendChild(a); + a.click(); + document.body.removeChild(a); + }; + + const formatTime = (secs: number) => { + if (isNaN(secs) || secs < 0) return "00:00"; + const mins = Math.floor(secs / 60); + const remainingSecs = Math.floor(secs % 60); + return `${mins.toString().padStart(2, "0")}:${remainingSecs.toString().padStart(2, "0")}`; + }; + + if (!isOpen || !video) return null; + + const isIframe = video.videoUrl?.includes(" +
+ {/* Backdrop */} + + + {/* Modal Window */} + + {/* Iframe Support (Aparat / Youtube) */} + {isIframe ? ( +
+ +
+
+ ) : ( + <> + {/* Native Video Element */} +
+
+ + {/* Top Header Overlay */} +
+
+
+ +
+
+

+ {video.title} +

+

+ {video.doctor || "کادر علمی کنینا"} +

+
+
+ +
+ {/* Share button */} + + + {/* Download button */} + + + {/* Close button */} + +
+
+ + {/* Bottom Custom Controls Bar */} +
e.stopPropagation()} + > + {/* Expandable Description Box if available */} + {video.description && ( +
+

+ {video.description} +

+ {video.description.length > 80 && ( + + )} +
+ )} + + {/* Progress / Seek Bar */} +
+ + {formatTime(currentTime)} + +
+ +
+ + {formatTime(duration)} + +
+ + {/* Action Buttons Row */} +
+ {/* Left Controls (Playback & Jumps) */} +
+ {/* Play/Pause */} + + + {/* -10s */} + + + {/* +10s */} + + + {/* Volume slider */} +
+ + +
+
+ + {/* Right Controls (Speed Menu & Fullscreen) */} +
+ {/* Playback Rate Popup Menu */} +
+ + + {showRateMenu && ( +
+ {PLAYBACK_RATES.map((rate) => ( + + ))} +
+ )} +
+ + {/* Fullscreen Button */} + +
+
+
+ + )} + +
+ + ); +} diff --git a/frontend/application/components/VideosPage.tsx b/frontend/application/components/VideosPage.tsx index 5d03320d..973cad0c 100644 --- a/frontend/application/components/VideosPage.tsx +++ b/frontend/application/components/VideosPage.tsx @@ -7,6 +7,7 @@ import SafeImage from "./SafeImage"; import { videoService, Video } from "../lib/services/videoService"; import { useRouter } from 'next/navigation'; +import VideoModalPlayer from "./VideoModalPlayer"; const FALLBACK_VIDEOS: Video[] = [ { @@ -165,47 +166,12 @@ export default function VideosPage() {
- {/* Video Modal */} - - {selectedVideo && ( -
- setSelectedVideo(null)} - className="absolute inset-0 bg-black/95 backdrop-blur-xl" - /> - - - -
- )} -
+ {/* Custom Feature-Rich Video Player Modal */} + setSelectedVideo(null)} + video={selectedVideo} + /> ); }