import React, { useCallback, useEffect, useImperativeHandle, useRef, useState } from 'react' import { AlertCircle, Loader2 } from 'lucide-react' import { cn } from '@/ui/lib/utils' import DrawingCanvas from '@/ui/components/drawing-canvas' import { useAnnotationStore } from '@/ui/stores/annotation-store' import type { FileViewerProps, MediaController } from '../types' import { centeredPan, fitScale, zoomAtPoint } from '../pan-zoom' import { ModelControlBar } from './model-control-bar' import { secondToDegree } from './index' const TOTAL_FRAMES = 24 const FPS = 6 const FRAME_DURATION = 1 / FPS export const ModelViewer = React.forwardRef( ( { file: data, onPlay, onPause, onTimeUpdate, annotations, startTime, children, allowDownload = true, autoPlay = false, }, ref, ) => { const rootRef = useRef(null) const videoRef = useRef(null) const containerRef = useRef(null) const [containerSize, setContainerSize] = useState({ width: 0, height: 0 }) const [isPlaying, setIsPlaying] = useState(false) const [currentTime, setCurrentTime] = useState(startTime || 0) const [isLoading, setIsLoading] = useState(true) const [error, setError] = useState(null) const [isFullScreen, setIsFullScreen] = useState(false) // Pan & Zoom const [zoom, setZoom] = useState(1) const [pan, setPan] = useState({ x: 0, y: 0 }) const [hasManuallyZoomed, setHasManuallyZoomed] = useState(false) // Drag-to-rotate state const [isDraggingRotation, setIsDraggingRotation] = useState(false) const dragStartRef = useRef<{ clientX: number; frame: number }>({ clientX: 0, frame: 0 }) const videoSrc = data.media?.videoPreview?.url || data.media?.videoTranscodes?.[0]?.url || '' const posterUrl = data.preview?.thumbnailUrl || undefined const downloadUrl = data.media?.original?.key ? videoSrc : undefined // Drawing Annotation store const { isDrawing, currentTool, currentColor, addAnnotation, annotations: draftAnnotations, } = useAnnotationStore() const displayAnnotations = [...(annotations || []), ...draftAnnotations] const isDrawingMode = isDrawing const mediaWidth = data.media?.metadata?.originalWidth || 1080 const mediaHeight = data.media?.metadata?.originalHeight || 1080 // Resize observer useEffect(() => { if (!containerRef.current) return const observer = new ResizeObserver((entries) => { const entry = entries[0] if (entry) { setContainerSize({ width: entry.contentRect.width, height: entry.contentRect.height, }) } }) observer.observe(containerRef.current) return () => observer.disconnect() }, []) // Initial scale and center useEffect(() => { if (containerSize.width > 0 && containerSize.height > 0 && !hasManuallyZoomed) { const scale = fitScale(containerSize.width, containerSize.height, mediaWidth, mediaHeight) setZoom(scale) setPan( centeredPan(containerSize.width, containerSize.height, mediaWidth, mediaHeight, scale), ) } }, [containerSize, mediaWidth, mediaHeight, hasManuallyZoomed]) // Current angle const currentDegree = secondToDegree(currentTime) // Seek helper const seekToFrame = useCallback( (frameIndex: number) => { const normalizedFrame = ((frameIndex % TOTAL_FRAMES) + TOTAL_FRAMES) % TOTAL_FRAMES const targetSec = normalizedFrame * FRAME_DURATION if (videoRef.current) { videoRef.current.currentTime = targetSec } setCurrentTime(targetSec) onTimeUpdate?.(targetSec) }, [onTimeUpdate], ) const seekToDegree = useCallback( (degree: number) => { const frame = Math.round((((degree % 360) + 360) % 360) / 15) % TOTAL_FRAMES seekToFrame(frame) }, [seekToFrame], ) // Play / Pause toggle const togglePlay = useCallback(() => { const video = videoRef.current if (!video) return if (video.paused || video.ended) { video.play().catch(() => {}) } else { video.pause() } }, []) // Video Imperative Controller useImperativeHandle( ref, () => ({ play: () => { videoRef.current?.play().catch(() => {}) }, pause: () => { videoRef.current?.pause() }, seekTo: (second: number) => { seekToFrame(Math.floor(second * FPS)) }, getCurrentTime: () => videoRef.current?.currentTime || 0, getDuration: () => 4, }), [seekToFrame], ) // Keyboard Hotkeys useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { const active = document.activeElement if ( active && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA' || (active instanceof HTMLElement && active.isContentEditable)) ) { return } if (e.key === ' ') { e.preventDefault() togglePlay() } else if (e.key === 'ArrowLeft') { e.preventDefault() const currentFrame = Math.floor(currentTime * FPS) seekToFrame(currentFrame - 1) } else if (e.key === 'ArrowRight') { e.preventDefault() const currentFrame = Math.floor(currentTime * FPS) seekToFrame(currentFrame + 1) } } window.addEventListener('keydown', handleKeyDown) return () => window.removeEventListener('keydown', handleKeyDown) }, [currentTime, seekToFrame, togglePlay]) // Pointer events for Drag-to-Rotate const handleViewerPointerDown = (e: React.PointerEvent) => { if (isDrawingMode || e.button !== 0) return const target = e.currentTarget target.setPointerCapture(e.pointerId) videoRef.current?.pause() const currentFrame = Math.floor(currentTime * FPS) dragStartRef.current = { clientX: e.clientX, frame: currentFrame, } setIsDraggingRotation(true) } const handleViewerPointerMove = (e: React.PointerEvent) => { if (!isDraggingRotation) return const deltaX = e.clientX - dragStartRef.current.clientX // 12px drag movement corresponds to 1 frame (15°) const deltaFrames = Math.round(deltaX / 12) seekToFrame(dragStartRef.current.frame + deltaFrames) } const handleViewerPointerUp = (e: React.PointerEvent) => { if (!isDraggingRotation) return setIsDraggingRotation(false) try { e.currentTarget.releasePointerCapture(e.pointerId) } catch { // pointer may already be released } } // Fullscreen toggle const toggleFullScreen = () => { if (!rootRef.current) return if (!document.fullscreenElement) { rootRef.current.requestFullscreen().catch(() => {}) } else { document.exitFullscreen().catch(() => {}) } } useEffect(() => { const onFsChange = () => { setIsFullScreen(Boolean(document.fullscreenElement)) } document.addEventListener('fullscreenchange', onFsChange) return () => document.removeEventListener('fullscreenchange', onFsChange) }, []) const handleZoomChange = (newZoom: number) => { setHasManuallyZoomed(true) const next = zoomAtPoint( zoom, pan, newZoom / zoom, containerSize.width / 2, containerSize.height / 2, ) setZoom(next.zoom) setPan(next.pan) } const handleZoomReset = () => { setHasManuallyZoomed(false) if (containerSize.width > 0 && containerSize.height > 0) { const scale = fitScale(containerSize.width, containerSize.height, mediaWidth, mediaHeight) setZoom(scale) setPan( centeredPan(containerSize.width, containerSize.height, mediaWidth, mediaHeight, scale), ) } } return (
{/* Render Carousel/Sidebar here if not fullscreen */} {!isFullScreen && children} {/* Interactive Drag Rotation Surface & Model Area */}
{/* Centered Scaled Media Wrapper */}
{videoSrc && (
{/* Drawing Canvas Overlay */} {containerSize.width > 0 && containerSize.height > 0 && ( )} {/* Loading Spinner */} {isLoading && !error && (
)} {/* Error Display */} {error && (

{error}

)}
{/* Full-width docked bottom control bar */} seekToDegree(0)} onZoomChange={handleZoomChange} onZoomReset={handleZoomReset} toggleFullScreen={toggleFullScreen} />
) }, ) ModelViewer.displayName = 'ModelViewer' export default ModelViewer