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' import { computeCommentFrameBox, ERROR_MIN_HEIGHT, type Dimensions } from './sizing' interface CommentFrameThumbnailProps { src: string annotations: Annotation[] initialDimensions?: Dimensions | null alt?: string onStatusChange?: (status: 'loaded' | 'error') => void } export const CommentFrameThumbnail: React.FC = ({ src, annotations, initialDimensions = null, alt = '', onStatusChange, }) => { const [status, setStatus] = useState<'loading' | 'loaded' | 'error'>('loading') const [naturalSize, setNaturalSize] = useState(initialDimensions) const imgRef = useRef(null) const onStatusChangeRef = useRef(onStatusChange) onStatusChangeRef.current = onStatusChange useEffect(() => { setStatus('loading') setNaturalSize(initialDimensions) const img = imgRef.current if (img && img.complete && img.naturalWidth > 0 && img.naturalHeight > 0) { setNaturalSize({ width: img.naturalWidth, height: img.naturalHeight }) setStatus('loaded') onStatusChangeRef.current?.('loaded') } }, [src, initialDimensions]) const handleLoad = (e: React.SyntheticEvent) => { const img = e.currentTarget if (img.naturalWidth > 0 && img.naturalHeight > 0) { setNaturalSize({ width: img.naturalWidth, height: img.naturalHeight }) } setStatus('loaded') onStatusChangeRef.current?.('loaded') } const handleError = () => { setStatus('error') onStatusChangeRef.current?.('error') } const { width: boxWidth, height: boxHeight } = computeCommentFrameBox(naturalSize) const containerHeight = status === 'error' ? Math.max(boxHeight, ERROR_MIN_HEIGHT) : boxHeight return (
{status === 'loading' && (
{m.print_comments_frame_loading()}
)} {status === 'error' ? (
{m.print_comments_frame_unavailable()}
) : ( <> {alt} {status === 'loaded' && annotations.length > 0 && ( )} )}
) }