import { cn } from '@/ui/lib/utils' 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}` } export interface SeekbarScrubHandlers { onStart: (frame: number) => void onMove: (frame: number) => void onEnd: (frame: number) => void onCancel: () => void } interface ProgressBarProps { totalFrames: number currentFrame: number fps: number previewUrl?: string onSeek: (frame: number) => void scrub?: SeekbarScrubHandlers buffered?: number metadata?: MediaMetadata } const ProgressBar: React.FC = ({ totalFrames, currentFrame, fps, previewUrl, onSeek, scrub, buffered = 0, metadata, }) => { const containerRef = useRef(null) const previewVideoRef = useRef(null) const [hoverPosition, setHoverPosition] = useState(null) // 0 to 1 const [isHovering, setIsHovering] = useState(false) const [isDragging, setIsDragging] = useState(false) // Tracks whether the pointer actually moved during a drag. A plain tap/click // already seeks on pointer-down; re-seeking to the same frame on release // restarts the native seek and stalls playback on slow connections. const didDragMoveRef = useRef(false) const lastSeekPosRef = useRef(null) // After a touch interaction, mobile browsers fire synthetic mouse events // (mousedown/mouseup) for the same tap. Suppress them so one tap does not // trigger a second seek through the mouse path. const lastTouchEndAtRef = useRef(0) const handlersRef = useRef({ onSeek, scrub }) useEffect(() => { handlersRef.current = { onSeek, scrub } }, [onSeek, scrub]) useEffect( () => () => { if (didDragMoveRef.current) handlersRef.current.scrub?.onCancel() }, [], ) // Load the preview video in background useEffect(() => { if (previewVideoRef.current && previewUrl) { previewVideoRef.current.load() } }, [previewUrl]) const getPercentageFromEvent = useCallback((clientX: number) => { const rect = containerRef.current?.getBoundingClientRect() if (!rect?.width) return 0 const x = Math.max(0, Math.min(clientX - rect.left, rect.width)) return x / rect.width }, []) const seekPreview = useCallback( (percentage: number) => { const video = previewVideoRef.current const seekTime = (percentage * totalFrames + 0.5) / fps if (video && totalFrames > 0 && Number.isFinite(seekTime)) { video.currentTime = seekTime } }, [totalFrames, fps], ) const handleMouseMove = useCallback( (e: React.MouseEvent) => { if (isDragging) return const percentage = getPercentageFromEvent(e.clientX) setHoverPosition(percentage) setIsHovering(true) seekPreview(percentage) }, [getPercentageFromEvent, isDragging, seekPreview], ) const handleMouseLeave = () => { if (!isDragging) { setIsHovering(false) setHoverPosition(null) } } const startInteraction = (clientX: number) => { if (isDragging || totalFrames <= 0) return setIsDragging(true) didDragMoveRef.current = false lastSeekPosRef.current = clientX const percentage = getPercentageFromEvent(clientX) setHoverPosition(percentage) setIsHovering(true) if (!scrub) onSeek(percentage * totalFrames) } const handleMouseDown = (e: React.MouseEvent) => { if (e.button !== 0 || Date.now() - lastTouchEndAtRef.current < 700) return e.preventDefault() startInteraction(e.clientX) } const handleTouchStart = (e: React.TouchEvent) => { if (e.touches.length === 1) startInteraction(e.touches[0].clientX) } useEffect(() => { if (!isDragging) return const moveInteraction = (clientX: number) => { const start = lastSeekPosRef.current if (start === null) return const alreadyMoved = didDragMoveRef.current if (!alreadyMoved && Math.abs(clientX - start) < 2) return didDragMoveRef.current = true const percentage = getPercentageFromEvent(clientX) setHoverPosition(percentage) setIsHovering(true) const { onSeek, scrub } = handlersRef.current const frame = percentage * totalFrames if (scrub) { if (alreadyMoved) scrub.onMove(frame) else scrub.onStart(frame) } else { seekPreview(percentage) onSeek(frame) } } const finishInteraction = (clientX: number, clientY: number, isTouch = false) => { if (lastSeekPosRef.current === null) return const { onSeek, scrub } = handlersRef.current const moved = didDragMoveRef.current didDragMoveRef.current = false lastSeekPosRef.current = null setIsDragging(false) const percentage = getPercentageFromEvent(clientX) const rect = containerRef.current?.getBoundingClientRect() if ( !rect || (isTouch && scrub) || clientX < rect.left || clientX > rect.right || clientY < rect.top || clientY > rect.bottom ) { setIsHovering(false) setHoverPosition(null) } else if (scrub) { setHoverPosition(percentage) seekPreview(percentage) } const frame = percentage * totalFrames if (scrub && moved) { scrub.onEnd(frame) } else if (scrub || moved) { // Compare and audio already seek on press, so only commit moved gestures here. onSeek(frame) } } const cancelInteraction = () => { if (didDragMoveRef.current) handlersRef.current.scrub?.onCancel() didDragMoveRef.current = false lastSeekPosRef.current = null setIsDragging(false) setIsHovering(false) setHoverPosition(null) } const handleWindowMouseMove = (e: MouseEvent) => moveInteraction(e.clientX) const handleWindowMouseUp = (e: MouseEvent) => finishInteraction(e.clientX, e.clientY) const handleWindowTouchMove = (e: TouchEvent) => { if (e.touches.length === 1) moveInteraction(e.touches[0].clientX) } const handleWindowTouchEnd = (e: TouchEvent) => { if (e.touches.length > 0) return lastTouchEndAtRef.current = Date.now() const touch = e.changedTouches[0] if (touch) finishInteraction(touch.clientX, touch.clientY, true) else cancelInteraction() } const handleWindowTouchCancel = () => { lastTouchEndAtRef.current = Date.now() cancelInteraction() } window.addEventListener('mousemove', handleWindowMouseMove) window.addEventListener('mouseup', handleWindowMouseUp) window.addEventListener('touchmove', handleWindowTouchMove, { passive: true }) window.addEventListener('touchend', handleWindowTouchEnd) window.addEventListener('touchcancel', handleWindowTouchCancel) window.addEventListener('blur', cancelInteraction) return () => { window.removeEventListener('mousemove', handleWindowMouseMove) window.removeEventListener('mouseup', handleWindowMouseUp) window.removeEventListener('touchmove', handleWindowTouchMove) window.removeEventListener('touchend', handleWindowTouchEnd) window.removeEventListener('touchcancel', handleWindowTouchCancel) window.removeEventListener('blur', cancelInteraction) } }, [isDragging, totalFrames, getPercentageFromEvent, seekPreview]) const progressPercent = isDragging && hoverPosition !== null ? hoverPosition * 100 : totalFrames > 0 ? (currentFrame / totalFrames) * 100 : 0 const hoverPercent = hoverPosition !== null ? hoverPosition * 100 : 0 // Calculate preview tooltip position 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 */}