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 = (
{src && status !== "error" && ( {alt} { const img = e.currentTarget; settle("loaded"); if (img.naturalWidth && img.naturalHeight) onLoad?.({ width: img.naturalWidth, height: img.naturalHeight }); }} onError={() => settle("error")} /> )} {status !== "loaded" && (
{status === "loading" ? ( painted ? null : ( ) ) : status === "error" ? ( ) : ( )} {compact || painted ? ( {labelFor(status, labels)} ) : ( {labelFor(status, labels)} )}
)}
); if (compact || !note) return frame; return (
{frame} {note}
); } function labelFor(status: ImagePreviewStatus, labels: ImagePreviewLabels): string { return status === "error" ? labels.error : status === "loading" ? labels.loading : labels.empty; } // ---- SearchResultPreview ------------------------------------------------------------------------ export interface SearchResultPreviewProps { /** The site's name, shown above the address like a search engine does. */ siteName: string; /** `example.com › posts › hello` (`searchBreadcrumb` in Studio). */ breadcrumb: string; title: string; /** Already cut to search length; `placeholder` renders it muted (nothing set). */ description: string; placeholder?: boolean; /** A badge row under the result, e.g. "Hidden from search engines". */ footer?: ReactNode; className?: string; } /** * A neutral sketch of a search engine result — site name, address, title in link colour, * description — built from Kumo tokens (no third-party branding). Read-only. */ export function SearchResultPreview({ siteName, breadcrumb, title, description, placeholder = false, footer, className, }: SearchResultPreviewProps) { return (
{siteName} {breadcrumb}
{title} {description} {footer &&
{footer}
}
); } // ---- SocialCardPreview -------------------------------------------------------------------------- export interface SocialCardPreviewProps { host: string; title: string; description: string; placeholder?: boolean; /** The image frame — an `ImagePreview size="card"` (the card crops it to 1.91:1). */ image: ReactNode; className?: string; } /** A neutral sketch of a link card on social networks and chat apps (Open Graph / Twitter). */ export function SocialCardPreview({ host, title, description, placeholder = false, image, className, }: SocialCardPreviewProps) { return (
{image}
{host} {title} {description}
); }