import { cn, Loader, Text } from "@cloudflare/kumo";
import { Globe, ImageBroken, Image as ImageIcon } from "@phosphor-icons/react";
import { type ReactNode, useEffect, useRef, useState } from "react";
// ---- ImagePreview -------------------------------------------------------------------------------
export type ImagePreviewStatus = "empty" | "loading" | "loaded" | "error";
export interface ImagePreviewLabels {
/** No source yet ("No cover image"). */
empty: string;
/** Accessible text while the image loads. */
loading: string;
/** The source did not load ("Couldn't load this image"). */
error: string;
}
export interface ImagePreviewProps {
/** URL the browser loads; `undefined` when there is nothing (valid) to show. */
src: string | undefined;
alt: string;
/**
* `card`: the 1.91:1 frame social networks crop to (Open Graph), labels shown in the frame.
* `thumb`: a 16:9, 3.5 rem list thumbnail with icons only.
* `tile`: a 4:3 frame filling its container, icons only (media library tiles, §2u).
*/
size?: "card" | "thumb" | "tile";
/** `cover` crops to the frame (default); `contain` shows the whole image (media previews). */
fit?: "cover" | "contain";
labels: ImagePreviewLabels;
/** A muted line under the frame, e.g. the image's size (`card` only). */
caption?: ReactNode;
/** Replaces the caption while the image is broken, e.g. why a site path cannot load here. */
errorHint?: ReactNode;
/**
* Shown while the image loads, in place of the spinner: its dominant colour and its small
* blurred preview (`Asset.image.color`, `.lqip`, computed at upload, RFC-0028).
*/
placeholder?: { color?: string; lqip?: string };
/** Called once per source with the image's natural size. */
onLoad?: (size: { width: number; height: number }) => void;
onStatusChange?: (status: ImagePreviewStatus) => void;
className?: string;
}
/**
* An image in a fixed frame with its four states — nothing set, loading, loaded, broken — so a
* URL field always shows what it points at (DESIGN.md §2o). The only place Studio renders an
* ``; screens pass the resolved URL and never draw images themselves.
*/
export function ImagePreview({
src,
alt,
size = "card",
fit = "cover",
labels,
caption,
errorHint,
onLoad,
onStatusChange,
className,
placeholder,
}: ImagePreviewProps) {
const [state, setState] = useState<{ src: string | undefined; status: ImagePreviewStatus }>({
src,
status: src ? "loading" : "empty",
});
// A new source starts over (derived during render, no effect round-trip).
const status: ImagePreviewStatus = state.src === src ? state.status : src ? "loading" : "empty";
const settle = (next: ImagePreviewStatus) => setState({ src, status: next });
// Report every state, including "loading"/"empty" after the source changed.
const report = useRef(onStatusChange);
report.current = onStatusChange;
useEffect(() => {
report.current?.(status);
}, [status]);
const thumb = size === "thumb";
const tile = size === "tile";
// Tiles and thumbnails show icons only; their name is next to them.
const compact = thumb || tile;
const iconSize = thumb ? 16 : tile ? 24 : 32;
const note = status === "error" ? errorHint : caption;
// The image's own colour and preview stand in while it loads (data, not a design colour).
const painted = status === "loading" && (placeholder?.color || placeholder?.lqip);
const paint = painted
? {
backgroundColor: placeholder?.color,
backgroundImage: placeholder?.lqip ? `url("${placeholder.lqip}")` : undefined,
backgroundSize: fit === "contain" ? "contain" : "cover",
backgroundPosition: "center",
backgroundRepeat: "no-repeat",
}
: undefined;
const frame = (