From 62803959281c9323c8e212b98dcabf4a99cc0504 Mon Sep 17 00:00:00 2001 From: Kerry Date: Thu, 13 Apr 2023 13:22:16 +1200 Subject: [PATCH] Fix: Images no longer reserve their space in the timeline correctly (#10571) * test case * fix + lint * refix strictNullChecks issue * add comment about max image size * tweak * use safer magic number --- src/components/views/messages/MImageBody.tsx | 14 ++++---- .../views/messages/MImageBody-test.tsx | 19 ++++++++++ .../__snapshots__/MImageBody-test.tsx.snap | 36 +++++++++++++++++++ 3 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 test/components/views/messages/__snapshots__/MImageBody-test.tsx.snap diff --git a/src/components/views/messages/MImageBody.tsx b/src/components/views/messages/MImageBody.tsx index ad99aa142e..84e4e763de 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 29825f128e..6534828bd6 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 0000000000..553090de62 --- /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`] = ` +
+
+
+
+
+
+
+
+
+
+
+
+
+`;