import { useState, useRef, useCallback } from 'react' interface ProgressBarProps { progress: number duration: number onSeek: (time: number) => void className?: string } export function ProgressBar({ progress, duration, onSeek, className = '' }: ProgressBarProps) { const [isDragging, setIsDragging] = useState(false) const barRef = useRef(null) const percent = duration > 0 ? (progress / duration) * 100 : 0 const handleSeek = useCallback((clientX: number) => { if (!barRef.current) return const rect = barRef.current.getBoundingClientRect() const x = Math.max(0, Math.min(clientX - rect.left, rect.width)) const ratio = x / rect.width onSeek(duration * ratio) }, [duration, onSeek]) const handleClick = (e: React.MouseEvent) => handleSeek(e.clientX) const handleMouseDown = (e: React.MouseEvent) => { setIsDragging(true) handleSeek(e.clientX) } const handleMouseMove = (e: React.MouseEvent) => { if (isDragging) handleSeek(e.clientX) } const handleMouseUp = () => setIsDragging(false) const formatTime = (sec: number) => { const m = Math.floor(sec / 60) const s = Math.floor(sec % 60) return `${m}:${s.toString().padStart(2, '0')}` } return (
{formatTime(progress)} {formatTime(duration)}
) }