Spaces:
Running
Running
File size: 7,972 Bytes
98291a5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
import { useState, useEffect, useCallback, useRef } from "react";
import GlassContainer from "./GlassContainer";
import GlassButton from "./GlassButton";
import { GLASS_EFFECTS } from "../constants";
interface VideoScrubberProps {
videoRef: React.RefObject<HTMLVideoElement | null>;
isVisible: boolean;
}
export default function VideoScrubber({ videoRef, isVisible }: VideoScrubberProps) {
const [currentTime, setCurrentTime] = useState(0);
const [duration, setDuration] = useState(0);
const [isDragging, setIsDragging] = useState(false);
const [isHovered, setIsHovered] = useState(false);
const [isPlaying, setIsPlaying] = useState(false);
const scrubberRef = useRef<HTMLInputElement>(null);
const formatTime = useCallback((seconds: number) => {
if (!isFinite(seconds) || isNaN(seconds)) {
return "0:00";
}
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${mins}:${secs.toString().padStart(2, '0')}`;
}, []);
const updateProgress = useCallback(() => {
const video = videoRef.current;
if (video && !isDragging) {
const time = isFinite(video.currentTime) ? video.currentTime : 0;
const dur = isFinite(video.duration) ? video.duration : 0;
setCurrentTime(time);
setDuration(dur);
setIsPlaying(!video.paused);
}
}, [videoRef, isDragging]);
const togglePlayPause = useCallback(() => {
const video = videoRef.current;
if (video) {
if (video.paused) {
video.play();
} else {
video.pause();
}
}
}, [videoRef]);
const handleSeek = useCallback((newTime: number) => {
const video = videoRef.current;
if (video && !isNaN(newTime)) {
video.currentTime = newTime;
setCurrentTime(newTime);
}
}, [videoRef]);
const handleScrubberChange = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
const newTime = parseFloat(event.target.value);
handleSeek(newTime);
}, [handleSeek]);
const handleMouseDown = useCallback(() => {
setIsDragging(true);
}, []);
const handleMouseUp = useCallback(() => {
setIsDragging(false);
}, []);
useEffect(() => {
const video = videoRef.current;
if (!video) return;
const handleLoadedMetadata = () => {
const dur = isFinite(video.duration) ? video.duration : 0;
const time = isFinite(video.currentTime) ? video.currentTime : 0;
setDuration(dur);
setCurrentTime(time);
};
const handleTimeUpdate = () => {
updateProgress();
};
const handlePlay = () => setIsPlaying(true);
const handlePause = () => setIsPlaying(false);
video.addEventListener('loadedmetadata', handleLoadedMetadata);
video.addEventListener('timeupdate', handleTimeUpdate);
video.addEventListener('play', handlePlay);
video.addEventListener('pause', handlePause);
// Update immediately if metadata is already loaded
if (video.readyState >= 1) {
handleLoadedMetadata();
}
return () => {
video.removeEventListener('loadedmetadata', handleLoadedMetadata);
video.removeEventListener('timeupdate', handleTimeUpdate);
video.removeEventListener('play', handlePlay);
video.removeEventListener('pause', handlePause);
};
}, [videoRef, updateProgress]);
useEffect(() => {
if (isDragging) {
document.addEventListener('mouseup', handleMouseUp);
return () => {
document.removeEventListener('mouseup', handleMouseUp);
};
}
}, [isDragging, handleMouseUp]);
if (!isVisible) {
return null;
}
const progressPercentage = duration > 0 && isFinite(duration) && isFinite(currentTime)
? Math.min((currentTime / duration) * 100, 100)
: 0;
return (
<div
className={`absolute bottom-4 left-4 right-4 z-[200] transition-opacity duration-300 ${
isHovered || isDragging ? 'opacity-100' : 'opacity-90'
}`}
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
style={{ pointerEvents: 'auto' }}
>
<GlassContainer
bgColor={GLASS_EFFECTS.COLORS.DEFAULT_BG}
className="rounded-lg px-4 py-3 shadow-lg"
>
<div className="flex items-center space-x-4">
{/* Play/Pause Button */}
<GlassButton
onClick={togglePlayPause}
className="w-10 h-10 rounded-full flex items-center justify-center p-0"
aria-label={isPlaying ? "Pause video" : "Play video"}
>
{isPlaying ? (
<svg className="w-5 h-5 text-white" fill="currentColor" viewBox="0 0 20 20">
<path fillRule="evenodd" d="M6 4a1 1 0 011 1v10a1 1 0 11-2 0V5a1 1 0 011-1zM14 4a1 1 0 011 1v10a1 1 0 11-2 0V5a1 1 0 011-1z" clipRule="evenodd" />
</svg>
) : (
<svg className="w-5 h-5 text-white ml-0.5" fill="currentColor" viewBox="0 0 20 20">
<path fillRule="evenodd" d="M6.267 3.455a.5.5 0 01.531-.024L15.5 8.5a.5.5 0 010 .872l-8.702 5.069a.5.5 0 01-.765-.436V3.455z" clipRule="evenodd" />
</svg>
)}
</GlassButton>
{/* Current Time */}
<div className="text-white text-sm font-mono min-w-[3.5rem]">
{formatTime(currentTime)}
</div>
{/* Timeline Scrubber */}
<div className="flex-1 relative px-2">
<input
ref={scrubberRef}
type="range"
min={0}
max={duration || 100}
step={0.1}
value={currentTime}
onChange={handleScrubberChange}
onMouseDown={handleMouseDown}
className="w-full h-1.5 bg-transparent rounded-lg appearance-none cursor-pointer
[&::-webkit-slider-track]:bg-gray-600/50
[&::-webkit-slider-track]:rounded-lg
[&::-webkit-slider-track]:h-1.5
[&::-webkit-slider-thumb]:appearance-none
[&::-webkit-slider-thumb]:w-4
[&::-webkit-slider-thumb]:h-4
[&::-webkit-slider-thumb]:rounded-full
[&::-webkit-slider-thumb]:bg-white
[&::-webkit-slider-thumb]:shadow-lg
[&::-webkit-slider-thumb]:cursor-pointer
[&::-webkit-slider-thumb]:border-2
[&::-webkit-slider-thumb]:border-blue-500
[&::-webkit-slider-thumb]:transition-transform
[&::-moz-range-track]:bg-gray-600/50
[&::-moz-range-track]:rounded-lg
[&::-moz-range-track]:h-1.5
[&::-moz-range-track]:border-0
[&::-moz-range-thumb]:w-4
[&::-moz-range-thumb]:h-4
[&::-moz-range-thumb]:rounded-full
[&::-moz-range-thumb]:bg-white
[&::-moz-range-thumb]:border-2
[&::-moz-range-thumb]:border-blue-500
[&::-moz-range-thumb]:cursor-pointer
[&::-moz-range-thumb]:transition-transform
hover:[&::-webkit-slider-thumb]:scale-110
hover:[&::-moz-range-thumb]:scale-110"
style={{
background: `linear-gradient(to right,
#3b82f6 0%,
#3b82f6 ${progressPercentage}%,
rgba(75, 85, 99, 0.3) ${progressPercentage}%,
rgba(75, 85, 99, 0.3) 100%)`
}}
/>
</div>
{/* Duration */}
<div className="text-white text-sm font-mono min-w-[3.5rem] text-right">
{formatTime(duration)}
</div>
</div>
</GlassContainer>
</div>
);
} |