Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(web): show fallback for failed media previews (#14662)

Tobias Wochinger5d820577e5ad
diff --git a/web/src/components/ui/media/MediaTag.clienttest.tsx b/web/src/components/ui/media/MediaTag.clienttest.tsx
index bfc378c1a7..5c1c0e5f81 100644
--- a/web/src/components/ui/media/MediaTag.clienttest.tsx
+++ b/web/src/components/ui/media/MediaTag.clienttest.tsx
@@ -46,4 +46,22 @@ describe("MediaTag", () => {
expect(onOpenChange).toHaveBeenCalledWith(true);
expect(onOpenChange).toHaveBeenCalledTimes(1);
});
+
+ it("shows the fallback when resolved image media fails to render", async () => {
+ render(
+ <MediaTag
+ contentType="image/jpeg"
+ status="ready"
+ url="https://commons.wikimedia.org/wiki/File:Gull_portrait_ca_usa.jpg"
+ open
+ />,
+ );
+
+ const image = document.body.querySelector("img");
+ expect(image).toBeInTheDocument();
+
+ fireEvent.error(image!);
+
+ expect(await screen.findByText("Failed to load media")).toBeInTheDocument();
+ });
});
diff --git a/web/src/components/ui/media/MediaTag.stories.tsx b/web/src/components/ui/media/MediaTag.stories.tsx
index 4114a126ef..1302b7cdd9 100644
--- a/web/src/components/ui/media/MediaTag.stories.tsx
+++ b/web/src/components/ui/media/MediaTag.stories.tsx
@@ -66,6 +66,17 @@ export const PreviewLargeImage = meta.story({
},
});
+// Wikimedia file pages can look like direct images by URL extension, but they
+// serve HTML. The preview should fall back once the image element errors.
+export const WikimediaFilePageFallback = meta.story({
+ args: {
+ contentType: "image/jpeg",
+ open: true,
+ status: "ready",
+ url: "https://commons.wikimedia.org/wiki/File:Gull_portrait_ca_usa.jpg",
+ },
+});
+
// Peek popover while the URL is still resolving (hover-triggered fetch).
export const Loading = meta.story({
args: {
diff --git a/web/src/components/ui/media/MediaTag.tsx b/web/src/components/ui/media/MediaTag.tsx
index 601f79f9ac..8dee839ba9 100644
--- a/web/src/components/ui/media/MediaTag.tsx
+++ b/web/src/components/ui/media/MediaTag.tsx
@@ -83,20 +83,27 @@ function KindIcon({
return <Icon className={className} />;
}
+type PreviewRenderer = (params: {
+ url: string;
+ onError: () => void;
+}) => React.ReactNode;
+
const MEDIA_KIND_PREVIEW = {
- image: (url: string) => (
+ image: ({ url, onError }) => (
// eslint-disable-next-line @next/next/no-img-element
<img
src={url}
alt=""
+ onError={onError}
// Bounded by the max-h cap and the card's max-w-sm; object-contain
// keeps high-resolution images from blowing out the popover.
className="max-h-64 max-w-full rounded object-contain"
/>
),
- video: (url: string) => (
+ video: ({ url, onError }) => (
<video
src={url}
+ onError={onError}
className="max-h-64 max-w-full rounded"
controls
muted
@@ -104,8 +111,14 @@ const MEDIA_KIND_PREVIEW = {
preload="metadata"
/>
),
- audio: (url: string) => (
- <audio src={url} controls className="w-64" preload="metadata" />
+ audio: ({ url, onError }) => (
+ <audio
+ src={url}
+ onError={onError}
+ controls
+ className="w-64"
+ preload="metadata"
+ />
),
file: () => (
<div className="text-muted-foreground flex h-24 w-64 flex-col items-center justify-center gap-2">
@@ -113,7 +126,7 @@ const MEDIA_KIND_PREVIEW = {
<span className="text-xs">No inline preview</span>
</div>
),
-} satisfies Record<MediaKind, (url: string) => React.ReactNode>;
+} satisfies Record<MediaKind, PreviewRenderer>;
/** The peek body — a glance, not a full player. Images/video show a thumbnail;
* audio gets an inline player; other types are open-in-new-tab only. */
@@ -121,10 +134,12 @@ function PeekBody({
kind,
status,
url,
+ onPreviewError,
}: {
kind: MediaKind;
status: MediaTagStatus;
url?: string;
+ onPreviewError: () => void;
}) {
if (status === "error") {
return (
@@ -139,7 +154,7 @@ function PeekBody({
return <Skeleton className="h-32 w-64" />;
}
- return MEDIA_KIND_PREVIEW[kind](url);
+ return MEDIA_KIND_PREVIEW[kind]({ url, onError: onPreviewError });
}
/**
@@ -155,6 +170,11 @@ export const MediaTag = React.forwardRef<HTMLButtonElement, MediaTagProps>(
const kind = getMediaKind(contentType);
const chipLabel = label ?? getDefaultLabel(contentType);
const canOpen = status === "ready" && Boolean(url);
+ const [failedPreviewUrl, setFailedPreviewUrl] = React.useState<
+ string | null
+ >(null);
+ const previewStatus =
+ status === "ready" && url && failedPreviewUrl === url ? "error" : status;
const isControlled = open !== undefined;
const [uncontrolledOpen, setUncontrolledOpen] = React.useState(false);
const isOpen = isControlled ? open : uncontrolledOpen;
@@ -225,7 +245,12 @@ export const MediaTag = React.forwardRef<HTMLButtonElement, MediaTagProps>(
</Button>
)}
</div>
- <PeekBody kind={kind} status={status} url={url} />
+ <PeekBody
+ kind={kind}
+ status={previewStatus}
+ url={url}
+ onPreviewError={() => setFailedPreviewUrl(url ?? null)}
+ />
</HoverCardContent>
</HoverCard>
);