'use client' import { useState, type CSSProperties, type ReactNode } from 'react' /** * A file this build serves, drawn — and what stands in its place when it does * not arrive. * * `asset.ts` answers where a mark is served from, and the rule it states is * that a component does not ask the network for a picture it cannot promise: * a resolver answers null for a name it has no file for, and the asset's own * letters are drawn instead, which always resolve. * * A path a build does state can still fail to fetch — a file renamed under a * hash, a reader offline, a host answering 404 — and a bare `` has no * answer for that. Chrome draws its own torn-picture glyph, `alt=""` and * `aria-hidden` included, clipped by whatever radius the box carries: measured * at 32 beside a letters disc, the two read as a damaged mark and a good one. * The state the resolver prevents arrives anyway, one step later. * * So whether the file arrived is a state, and it is held here. The three places * a Lux surface draws a served file fall back to three different things — a * token's mark to the asset's letters, the chain badge on it to nothing, the * flag beside a currency to the region's two indicators — and which one is the * caller's to say. What they share is noticing that the file is not coming. */ export function Picture({ src, size, instead = null, style, }: { /** Where the file is, as the build's own resolver answered. Null is the * ordinary answer for a name a build serves no file for. */ src: string | null | undefined /** The square it is drawn in. */ size: number /** What stands there when there is no file, or when the file does not * arrive. Nothing, where the room itself is the answer. */ instead?: ReactNode /** The rest of the drawing — a radius, a fit, a corner to sit in. Everything * but the box, which is `size`. */ style?: CSSProperties }) { /** * The path that failed, rather than a flag that it did. * * A row in a list is recycled: the same element draws a different asset when * the list scrolls or a filter narrows it. A boolean set once stays set, and * every asset after the first failure draws letters. Holding the path means * a new one is a new question, with no effect and no key to remember. */ const [lost, setLost] = useState(null) if (!src || src === lost) return <>{instead} return ( setLost(src)} // The same question asked once on mount, because an error that fired // before React attached its handler is an error React never hears: a page // rendered on a server hydrates after the browser has already tried the // fetch. A finished image that measures nothing is one that did not // arrive — a picture with no intrinsic size of its own still reports the // 150 a browser gives it, so only a failure reads zero. ref={(el) => { if (el?.complete && el.naturalWidth === 0) setLost(src) }} style={{ width: size, height: size, display: 'block', ...style }} /> ) }