diff --git a/src/components/views/messages/MImageBody.tsx b/src/components/views/messages/MImageBody.tsx index ad99aa142e6..84e4e763de0 100644 --- a/src/components/views/messages/MImageBody.tsx +++ b/src/components/views/messages/MImageBody.tsx @@ -385,22 +385,24 @@ export default class MImageBody extends React.Component { } protected messageContent( - contentUrl: string, + contentUrl: string | null, thumbUrl: string | null, content: IMediaEventContent, forcedHeight?: number, ): JSX.Element { if (!thumbUrl) thumbUrl = contentUrl; // fallback - let infoWidth: number; - let infoHeight: number; + // magic number + // edge case for this not to be set by conditions below + let infoWidth = 500; + let infoHeight = 500; let infoSvg = false; if (content.info?.w && content.info?.h) { infoWidth = content.info.w; infoHeight = content.info.h; infoSvg = content.info.mimetype === "image/svg+xml"; - } else { + } else if (thumbUrl && contentUrl) { // Whilst the image loads, display nothing. We also don't display a blurhash image // because we don't really know what size of image we'll end up with. // @@ -522,7 +524,7 @@ export default class MImageBody extends React.Component { ); - return this.wrapImage(contentUrl, thumbnail); + return contentUrl ? this.wrapImage(contentUrl, thumbnail) : thumbnail; } // Overridden by MStickerBody @@ -596,7 +598,7 @@ export default class MImageBody extends React.Component { thumbUrl = this.state.thumbUrl ?? this.state.contentUrl; } - const thumbnail = contentUrl ? this.messageContent(contentUrl, thumbUrl, content) : undefined; + const thumbnail = this.messageContent(contentUrl, thumbUrl, content); const fileBody = this.getFileBody(); return ( diff --git a/test/components/views/messages/MImageBody-test.tsx b/test/components/views/messages/MImageBody-test.tsx index 29825f128eb..6534828bd65 100644 --- a/test/components/views/messages/MImageBody-test.tsx +++ b/test/components/views/messages/MImageBody-test.tsx @@ -61,6 +61,10 @@ describe("", () => { sender: userId, type: EventType.RoomMessage, content: { + info: { + w: 40, + h: 50, + }, file: { url: "mxc://server/encrypted-image", }, @@ -72,6 +76,21 @@ describe("", () => { permalinkCreator: new RoomPermalinkCreator(new Room(encryptedMediaEvent.getRoomId()!, cli, cli.getUserId()!)), }; + it("should show a thumbnail while image is being downloaded", async () => { + fetchMock.getOnce(url, { status: 200 }); + + const { container } = render( + , + ); + + // thumbnail with dimensions present + expect(container).toMatchSnapshot(); + }); + it("should show error when encrypted media cannot be downloaded", async () => { fetchMock.getOnce(url, { status: 500 }); diff --git a/test/components/views/messages/__snapshots__/MImageBody-test.tsx.snap b/test/components/views/messages/__snapshots__/MImageBody-test.tsx.snap new file mode 100644 index 00000000000..553090de629 --- /dev/null +++ b/test/components/views/messages/__snapshots__/MImageBody-test.tsx.snap @@ -0,0 +1,36 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` should show a thumbnail while image is being downloaded 1`] = ` +
+
+
+
+
+
+
+
+
+
+
+
+
+`;