import type { MediaMetadata } from '@shumai/dtos' import React, { useCallback, useEffect, useRef, useState } from 'react' const formatTime = (seconds: number): string => { if (isNaN(seconds)) return '00:00' const date = new Date(seconds * 1000) const hh = date.getUTCHours() const mm = date.getUTCMinutes() const ss = date.getUTCSeconds().toString().padStart(2, '0') if (hh) { return `${hh}:${mm.toString().padStart(2, '0')}:${ss}` } return `${mm}:${ss}` } interface ProgressBarProps { duration: number currentTime: number previewUrl?: string onSeek: (time: number) => void buffered?: number metadata?: MediaMetadata } const ProgressBar: React.FC = ({ duration, currentTime, previewUrl, onSeek, buffered = 0, metadata, }) => { const containerRef = useRef(null) const previewVideoRef = useRef(null) const [hoverPosition, setHoverPosition] = useState(null) // 0 to 1 const [isHovering, setIsHovering] = useState(false) // Load the preview video in background useEffect(() => { if (previewVideoRef.current && previewUrl) { previewVideoRef.current.load() } }, [previewUrl]) const handleMouseMove = useCallback( (e: React.MouseEvent) => { if (!containerRef.current) return const rect = containerRef.current.getBoundingClientRect() const x = Math.max(0, Math.min(e.clientX - rect.left, rect.width)) const percentage = x / rect.width setHoverPosition(percentage) setIsHovering(true) if (previewVideoRef.current && duration) { const seekTime = percentage * duration // Use fastSeek if available for smoother scrubbing, otherwise currentTime const vid = previewVideoRef.current if (Number.isFinite(seekTime)) { vid.currentTime = seekTime } } }, [duration], ) const handleMouseLeave = () => { setIsHovering(false) setHoverPosition(null) } const handleClick = (e: React.MouseEvent) => { if (!containerRef.current || !duration) return const rect = containerRef.current.getBoundingClientRect() const x = e.clientX - rect.left const percentage = x / rect.width onSeek(percentage * duration) } const progressPercent = duration > 0 ? (currentTime / duration) * 100 : 0 const hoverPercent = hoverPosition !== null ? hoverPosition * 100 : 0 // Calculate preview tooltip position // Logic: Interpolate translation based on position to keep preview fully visible // 0% -> translateX(0%) (Left aligned) // 50% -> translateX(-50%) (Center aligned) // 100% -> translateX(-100%) (Right aligned) const getTooltipStyle = () => { if (hoverPosition === null) return {} return { left: `${hoverPercent}%`, transform: `translateX(-${hoverPercent}%)`, } } // Dynamic Size Calculation const MAX_PREVIEW_SIZE = 180 let previewDimensions: React.CSSProperties = { width: `${MAX_PREVIEW_SIZE}px`, height: 'auto', } if (metadata && metadata.originalWidth && metadata.originalHeight) { const { originalWidth, originalHeight } = metadata if (originalWidth >= originalHeight) { // Landscape or Square: Constrain width const height = (MAX_PREVIEW_SIZE * originalHeight) / originalWidth previewDimensions = { width: `${MAX_PREVIEW_SIZE}px`, height: `${height}px`, } } else { // Portrait: Constrain height const width = (MAX_PREVIEW_SIZE * originalWidth) / originalHeight previewDimensions = { width: `${width}px`, height: `${MAX_PREVIEW_SIZE}px`, } } } return (
{/* Hidden Preview Video Element */}