import { m } from '@/ui/paraglide/messages.js' import type { Annotation } from '@/ui/types' import { ImageOff, Loader2 } from 'lucide-react' import React, { useEffect, useRef, useState } from 'react' import { AnnotationOverlaySvg } from './annotation-overlay-svg' const BOX_WIDTH = 200 const BOX_HEIGHT = 112 interface CommentFrameThumbnailProps { src: string annotations: Annotation[] alt?: string onStatusChange?: (status: 'loaded' | 'error') => void } export const CommentFrameThumbnail: React.FC = ({ src, annotations, alt = '', onStatusChange, }) => { const [status, setStatus] = useState<'loading' | 'loaded' | 'error'>('loading') const [naturalSize, setNaturalSize] = useState<{ width: number; height: number }>({ width: BOX_WIDTH, height: BOX_HEIGHT, }) const imgRef = useRef(null) const onStatusChangeRef = useRef(onStatusChange) onStatusChangeRef.current = onStatusChange useEffect(() => { setStatus('loading') const img = imgRef.current if (img && img.complete && img.naturalWidth > 0) { setNaturalSize({ width: img.naturalWidth, height: img.naturalHeight }) setStatus('loaded') onStatusChangeRef.current?.('loaded') } }, [src]) const handleLoad = (e: React.SyntheticEvent) => { const img = e.currentTarget const nw = img.naturalWidth || BOX_WIDTH const nh = img.naturalHeight || BOX_HEIGHT setNaturalSize({ width: nw, height: nh }) setStatus('loaded') onStatusChangeRef.current?.('loaded') } const handleError = () => { setStatus('error') onStatusChangeRef.current?.('error') } // Fit the actual image rect inside 200x112 while preserving aspect ratio so the // SVG markup overlay aligns accurately for portrait, square, PDF, and ultrawide media. const scale = Math.min(BOX_WIDTH / naturalSize.width, BOX_HEIGHT / naturalSize.height) const fittedWidth = Math.max(1, Math.round(naturalSize.width * scale)) const fittedHeight = Math.max(1, Math.round(naturalSize.height * scale)) return (
{status === 'loading' && (
{m.print_comments_frame_loading()}
)} {status === 'error' ? (
{m.print_comments_frame_unavailable()}
) : (
{alt} {status === 'loaded' && annotations.length > 0 && ( )}
)}
) }