From 1960aac90b16fce1ec620ac990aa931efcf04700 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 21 Mar 2025 11:23:49 +0100 Subject: [PATCH] Fix display of failed-to-load image attachments in web UI (#34217) --- .../mastodon/components/media_gallery.jsx | 8 +++++++- .../account_gallery/components/media_item.tsx | 13 ++++++++++++- app/javascript/styles/mastodon/components.scss | 4 ++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/app/javascript/mastodon/components/media_gallery.jsx b/app/javascript/mastodon/components/media_gallery.jsx index 5132316600..e52e14b1ed 100644 --- a/app/javascript/mastodon/components/media_gallery.jsx +++ b/app/javascript/mastodon/components/media_gallery.jsx @@ -38,6 +38,7 @@ class Item extends PureComponent { state = { loaded: false, + error: false, }; handleMouseEnter = (e) => { @@ -81,6 +82,10 @@ class Item extends PureComponent { this.setState({ loaded: true }); }; + handleImageError = () => { + this.setState({ error: true }); + }; + render () { const { attachment, lang, index, size, standalone, displayWidth, visible } = this.props; @@ -148,6 +153,7 @@ class Item extends PureComponent { lang={lang} style={{ objectPosition: `${x}% ${y}%` }} onLoad={this.handleImageLoad} + onError={this.handleImageError} /> ); @@ -183,7 +189,7 @@ class Item extends PureComponent { } return ( -
+
{ setLoaded(true); }, [setLoaded]); + const handleImageError = useCallback(() => { + setError(true); + }, [setError]); + const handleMouseEnter = useCallback( (e: React.MouseEvent) => { if (e.target instanceof HTMLVideoElement) { @@ -98,6 +103,7 @@ export const MediaItem: React.FC<{ alt={description} lang={lang} onLoad={handleImageLoad} + onError={handleImageError} />
@@ -118,6 +124,7 @@ export const MediaItem: React.FC<{ lang={lang} style={{ objectPosition: `${x}% ${y}%` }} onLoad={handleImageLoad} + onError={handleImageError} /> ); } else if (['video', 'gifv'].includes(type)) { @@ -173,7 +180,11 @@ export const MediaItem: React.FC<{ } return ( -
+