mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-28 04:46:28 -08:00
fix(avatar): clear the load-error latch when the resolved media url arrives
This commit is contained in:
5 files changed
+85
-11
No files matched your search
@@ -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<HTMLImageElement | undefined>(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 (
|
||||
<FoldsAvatarImage
|
||||
className={css.RoomAvatar}
|
||||
style={{ backgroundColor: useUniformIcons ? normalizedBg : undefined }}
|
||||
src={mediaSrc}
|
||||
src={src}
|
||||
crossOrigin={isBlobUrl ? undefined : 'anonymous'}
|
||||
alt={alt}
|
||||
loading="lazy"
|
||||
|
||||
@@ -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('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(
|
||||
<RoomAvatar roomId="!room:example.org" src={RAW_SRC} renderFallback={() => 'RM'} />
|
||||
);
|
||||
|
||||
fireEvent.error(screen.getByRole('img'));
|
||||
expect(screen.queryByRole('img')).not.toBeInTheDocument();
|
||||
|
||||
media.useRenderableMediaUrl.mockReturnValue('blob:resolved-avatar');
|
||||
rerender(<RoomAvatar roomId="!room:example.org" src={RAW_SRC} renderFallback={() => 'RM'} />);
|
||||
|
||||
expect(screen.getByRole('img')).toHaveAttribute('src', 'blob:resolved-avatar');
|
||||
});
|
||||
});
|
||||
@@ -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 (
|
||||
<AvatarFallback
|
||||
style={{ backgroundColor: colorMXID(roomId ?? ''), color: color.Surface.Container }}
|
||||
@@ -42,7 +45,12 @@ export function RoomAvatar({ roomId, src, alt, renderFallback, uniformIcons }: R
|
||||
}
|
||||
|
||||
return (
|
||||
<AvatarImage src={src} alt={alt} uniformIcons={uniformIcons} onError={() => setError(true)} />
|
||||
<AvatarImage
|
||||
src={mediaSrc}
|
||||
alt={alt}
|
||||
uniformIcons={uniformIcons}
|
||||
onError={() => setError(true)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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(
|
||||
<UserAvatar userId="@user:example.org" src={RAW_SRC} renderFallback={() => 'US'} />
|
||||
);
|
||||
|
||||
fireEvent.error(screen.getByRole('img'));
|
||||
expect(screen.queryByRole('img')).not.toBeInTheDocument();
|
||||
|
||||
media.useRenderableMediaUrl.mockReturnValue('blob:resolved-avatar');
|
||||
rerender(<UserAvatar userId="@user:example.org" src={RAW_SRC} renderFallback={() => 'US'} />);
|
||||
|
||||
expect(screen.getByRole('img')).toHaveAttribute('src', 'blob:resolved-avatar');
|
||||
});
|
||||
});
|
||||
@@ -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 (
|
||||
<AvatarFallback
|
||||
style={{
|
||||
@@ -51,7 +52,7 @@ export function UserAvatar({
|
||||
return (
|
||||
<AvatarImage
|
||||
className={classNames(css.UserAvatar, className)}
|
||||
src={resolvedSrc ?? src}
|
||||
src={mediaSrc}
|
||||
alt={alt}
|
||||
loading="lazy"
|
||||
decoding="async"
|
||||
|
||||
Reference in new issue
Block a user