import React, { useState, useEffect, useRef, CSSProperties } from "react" import classNames from "classnames" import { ImageManager } from "./formContext" export interface Image { /** UUID of image */ id: string /** Optional clockwise rotation (0, 90, 180, 270) */ rotation?: number /** Optional caption */ caption?: string /** If part of an image set, true if cover image */ cover?: boolean } export interface RotationAwareImageComponentProps { image: Image imageManager: ImageManager /** True to use thumbnail (max 160 height) */ thumbnail?: boolean /** Optional width */ width?: number /** Optional height */ height?: number /** Called when clicked */ onClick?: () => void } interface LoadState { url?: string error: boolean } /** Displays a single image rotated appropriately */ export default function RotationAwareImageComponent(props: RotationAwareImageComponentProps) { const { image, imageManager, thumbnail, width, height, onClick } = props const [state, setState] = useState({ error: false }) const [naturalSize, setNaturalSize] = useState<{ w: number; h: number } | null>(null) // True if the itself failed to load (e.g. 404 from the image URL). const [imgLoadError, setImgLoadError] = useState(false) const loadSeqRef = useRef(0) const isSwapped = image.rotation === 90 || image.rotation === 270 useEffect(() => { loadSeqRef.current += 1 const seq = loadSeqRef.current setNaturalSize(null) setImgLoadError(false) const callback = (update: Partial) => { if (seq !== loadSeqRef.current) return setState((prev) => ({ ...prev, ...update })) } if (thumbnail) { imageManager.getImageThumbnailUrl( image.id, (url) => callback({ url, error: false }), () => callback({ error: true }) ) } else { imageManager.getImageUrl( image.id, (url) => callback({ url, error: false }), () => callback({ error: true }) ) } }, [image.id, thumbnail, imageManager]) const imgRef = useRef(null) const captureNaturalSize = (img: HTMLImageElement) => { if (thumbnail && img.naturalWidth > 0) { setNaturalSize({ w: img.naturalWidth, h: img.naturalHeight }) } } const handleImageLoad = (e: React.SyntheticEvent) => { captureNaturalSize(e.currentTarget) } // Handle cached images where onLoad may have already fired useEffect(() => { if (thumbnail && !naturalSize && imgRef.current?.complete) { captureNaturalSize(imgRef.current) } }) // If the image URL could not be obtained or the image itself failed to load, // render a friendly placeholder so the user sees something meaningful. if (state.error || imgLoadError) { return renderMissingImagePlaceholder({ thumbnail, width, height }) } if (!state.url) return null const maxW = width ?? 160 const maxH = height ?? 160 const imageStyle: CSSProperties = {} const containerStyle: CSSProperties = {} const classes = classNames({ "img-thumbnail": thumbnail, rotated: image.rotation, "rotate-90": image.rotation === 90, "rotate-180": image.rotation === 180, "rotate-270": image.rotation === 270 }) const containerClasses = classNames({ "rotated-image-container": true, "rotated-thumbnail": thumbnail }) if (thumbnail) { if (isSwapped) { imageStyle.maxHeight = maxW imageStyle.maxWidth = maxH if (naturalSize) { const scale = Math.min(maxH / naturalSize.w, maxW / naturalSize.h, 1) const renderedW = naturalSize.w * scale const renderedH = naturalSize.h * scale containerStyle.width = renderedH containerStyle.height = renderedW } else { containerStyle.maxHeight = maxH containerStyle.maxWidth = maxH } } else { imageStyle.maxHeight = maxH imageStyle.maxWidth = maxW containerStyle.maxHeight = maxH } } else { imageStyle.maxWidth = "100%" } return ( {image.caption setImgLoadError(true)} /> ) } /** * Renders a muted "image not available" icon. Used when the image URL cannot * be obtained or the image itself fails to load (e.g. 404). * * Lays out as inline-block + vertical-align: middle so it composes with the * same parent centering tricks used for `` (e.g. the image carousel's * `::before` pseudo-element). For thumbnails, fills the requested box with * a centered icon. */ function renderMissingImagePlaceholder(opts: { thumbnail?: boolean; width?: number; height?: number }) { const { thumbnail, width, height } = opts if (thumbnail) { return ( ) } return ( ) }