/* ═══════════════════════════════════════════════════════════════ useSmallImage For an upload shown small (an avatar, a logo, a card picture): try its 640px copy first (-640.webp, which the server makes beside every raster upload, server/src/media.js), then the full file if the copy isn't there, then give up so the caller can draw its fallback. Anything that isn't an upload, and SVGs, go straight to the full file. const img = useSmallImage(mediaUrl(person.photo)) img.failed ? : ═══════════════════════════════════════════════════════════════ */ import { useEffect, useState } from 'react' /* '/uploads/abc.webp' → '/uploads/abc-640.webp'; null for anything else. */ export function smallCopy(src?: string | null): string | null { if (!src || !src.startsWith('/uploads/') || /\.svg(\?|#|$)/i.test(src)) return null return src.replace(/\.[^./?#]+(?=$|[?#])/, '') + '-640.webp' } export function useSmallImage(src?: string | null) { // 0: the small copy (when there is one), 1: the full file, 2: failed const [stage, setStage] = useState(0) useEffect(() => setStage(0), [src]) const small = smallCopy(src) const current = !src ? null : stage === 0 && small ? small : stage <= 1 ? src : null return { src: current ?? undefined, failed: current === null, onError: () => setStage((s) => (s === 0 && small ? 1 : 2)), } }