From 1eaaf4d8cb4a3f9cb571489fe76b4d0883df9fad Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Tue, 18 Aug 2026 17:23:38 +0200 Subject: [PATCH] fix(avatar): clear the load-error latch when the resolved media url arrives --- .../components/room-avatar/AvatarImage.tsx | 7 ++-- .../room-avatar/RoomAvatar.test.tsx | 34 +++++++++++++++++++ src/app/components/room-avatar/RoomAvatar.tsx | 14 ++++++-- .../user-avatar/UserAvatar.test.tsx | 34 +++++++++++++++++++ src/app/components/user-avatar/UserAvatar.tsx | 7 ++-- 5 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 src/app/components/room-avatar/RoomAvatar.test.tsx create mode 100644 src/app/components/user-avatar/UserAvatar.test.tsx diff --git a/src/app/components/room-avatar/AvatarImage.tsx b/src/app/components/room-avatar/AvatarImage.tsx index 9dbf134ac..f19e43e4f 100644 --- a/src/app/components/room-avatar/AvatarImage.tsx +++ b/src/app/components/room-avatar/AvatarImage.tsx @@ -4,7 +4,6 @@ import { useState } from 'react'; import bgColorImg from '$utils/bgColorImg'; import { settingsAtom } from '$state/settings'; import { useSetting } from '$state/hooks/settings'; -import { useRenderableMediaUrl } from '$hooks/useRenderableMediaUrl'; import * as css from './RoomAvatar.css'; type AvatarImageProps = { @@ -17,8 +16,6 @@ type AvatarImageProps = { export function AvatarImage({ src, alt, uniformIcons, onError }: AvatarImageProps) { const [uniformIconsSetting] = useSetting(settingsAtom, 'uniformIcons'); const [image, setImage] = useState(undefined); - const resolvedSrc = useRenderableMediaUrl(src); - const mediaSrc = resolvedSrc ?? src; const useUniformIcons = uniformIconsSetting && uniformIcons === true; const normalizedBg = useUniformIcons && image ? bgColorImg(image) : undefined; @@ -28,13 +25,13 @@ export function AvatarImage({ src, alt, uniformIcons, onError }: AvatarImageProp setImage(evt.currentTarget); }; - const isBlobUrl = mediaSrc.startsWith('blob:'); + const isBlobUrl = src.startsWith('blob:'); return ( ({ + useRenderableMediaUrl: vi.fn<(url: string | undefined) => string | undefined>(), +})); + +vi.mock('$hooks/useRenderableMediaUrl', () => media); + +const RAW_SRC = 'https://example.org/_matrix/client/v1/media/thumbnail/example.org/avatar'; + +describe('RoomAvatar', () => { + beforeEach(() => { + vi.resetModules(); + media.useRenderableMediaUrl.mockReset(); + }); + + it('shows the image once the resolved url arrives after a failed raw request', async () => { + media.useRenderableMediaUrl.mockReturnValue(undefined); + const { RoomAvatar } = await import('./RoomAvatar'); + + const { rerender } = render( + 'RM'} /> + ); + + fireEvent.error(screen.getByRole('img')); + expect(screen.queryByRole('img')).not.toBeInTheDocument(); + + media.useRenderableMediaUrl.mockReturnValue('blob:resolved-avatar'); + rerender( 'RM'} />); + + expect(screen.getByRole('img')).toHaveAttribute('src', 'blob:resolved-avatar'); + }); +}); diff --git a/src/app/components/room-avatar/RoomAvatar.tsx b/src/app/components/room-avatar/RoomAvatar.tsx index 18cc39fa5..4c46e7ebe 100644 --- a/src/app/components/room-avatar/RoomAvatar.tsx +++ b/src/app/components/room-avatar/RoomAvatar.tsx @@ -12,6 +12,7 @@ import { getRoomStandaloneIconComponent, } from '$components/icons/roomIcons'; import colorMXID from '$utils/colorMXID'; +import { useRenderableMediaUrl } from '$hooks/useRenderableMediaUrl'; import * as css from './RoomAvatar.css'; import { AvatarImage } from './AvatarImage'; @@ -25,12 +26,14 @@ type RoomAvatarProps = { export function RoomAvatar({ roomId, src, alt, renderFallback, uniformIcons }: RoomAvatarProps) { const [error, setError] = useState(false); + const resolvedSrc = useRenderableMediaUrl(src); + const mediaSrc = resolvedSrc ?? src; useEffect(() => { setError(false); - }, [src]); + }, [mediaSrc]); - if (!src || error) { + if (!mediaSrc || error) { return ( setError(true)} /> + setError(true)} + /> ); } diff --git a/src/app/components/user-avatar/UserAvatar.test.tsx b/src/app/components/user-avatar/UserAvatar.test.tsx new file mode 100644 index 000000000..c0e24d223 --- /dev/null +++ b/src/app/components/user-avatar/UserAvatar.test.tsx @@ -0,0 +1,34 @@ +import { fireEvent, render, screen } from '@testing-library/react'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; + +const media = vi.hoisted(() => ({ + useRenderableMediaUrl: vi.fn<(url: string | undefined) => string | undefined>(), +})); + +vi.mock('$hooks/useRenderableMediaUrl', () => media); + +const RAW_SRC = 'https://example.org/_matrix/client/v1/media/thumbnail/example.org/avatar'; + +describe('UserAvatar', () => { + beforeEach(() => { + vi.resetModules(); + media.useRenderableMediaUrl.mockReset(); + }); + + it('shows the image once the resolved url arrives after a failed raw request', async () => { + media.useRenderableMediaUrl.mockReturnValue(undefined); + const { UserAvatar } = await import('./UserAvatar'); + + const { rerender } = render( + 'US'} /> + ); + + fireEvent.error(screen.getByRole('img')); + expect(screen.queryByRole('img')).not.toBeInTheDocument(); + + media.useRenderableMediaUrl.mockReturnValue('blob:resolved-avatar'); + rerender( 'US'} />); + + expect(screen.getByRole('img')).toHaveAttribute('src', 'blob:resolved-avatar'); + }); +}); diff --git a/src/app/components/user-avatar/UserAvatar.tsx b/src/app/components/user-avatar/UserAvatar.tsx index 9c7a66e7c..2247adbd6 100644 --- a/src/app/components/user-avatar/UserAvatar.tsx +++ b/src/app/components/user-avatar/UserAvatar.tsx @@ -29,12 +29,13 @@ export function UserAvatar({ }: UserAvatarProps) { const [error, setError] = useState(false); const resolvedSrc = useRenderableMediaUrl(src); + const mediaSrc = resolvedSrc ?? src; useEffect(() => { setError(false); - }, [src]); + }, [mediaSrc]); - if (!src || error) { + if (!mediaSrc || error) { return (