Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-animated-avatars.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
default: patch
---

Fix animated avatars not looping.
55 changes: 51 additions & 4 deletions src/app/components/room-avatar/AvatarImage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AvatarImage as FoldsAvatarImage } from 'folds';
import { ReactEventHandler, useState } from 'react';
import { ReactEventHandler, useState, useEffect } from 'react';
import bgColorImg from '$utils/bgColorImg';
import { settingsAtom } from '$state/settings';
import { useSetting } from '$state/hooks/settings';
Expand All @@ -15,20 +15,67 @@ type AvatarImageProps = {
export function AvatarImage({ src, alt, uniformIcons, onError }: AvatarImageProps) {
const [uniformIconsSetting] = useSetting(settingsAtom, 'uniformIcons');
const [image, setImage] = useState<HTMLImageElement | undefined>(undefined);
const normalizedBg = image ? bgColorImg(image) : undefined;
const [processedSrc, setProcessedSrc] = useState<string>(src);

const useUniformIcons = uniformIconsSetting && uniformIcons === true;
const normalizedBg = useUniformIcons && image ? bgColorImg(image) : undefined;

useEffect(() => {
let isMounted = true;
let objectUrl: string | null = null;

const processImage = async () => {
try {
const res = await fetch(src, { mode: 'cors' });
const contentType = res.headers.get('content-type');

if (contentType && contentType.includes('image/svg+xml')) {
const text = await res.text();
const parser = new DOMParser();
const doc = parser.parseFromString(text, 'image/svg+xml');

const animations = doc.querySelectorAll('animate, animateTransform, animateMotion');
animations.forEach((anim) => anim.setAttribute('repeatCount', 'indefinite'));

const style = doc.createElementNS('http://www.w3.org/2000/svg', 'style');
style.textContent = '* { animation-iteration-count: infinite !important; }';
doc.documentElement.appendChild(style);

const serializer = new XMLSerializer();
const newSvgString = serializer.serializeToString(doc);
const blob = new Blob([newSvgString], { type: 'image/svg+xml' });

objectUrl = URL.createObjectURL(blob);
if (isMounted) setProcessedSrc(objectUrl);
} else if (isMounted) setProcessedSrc(src);
} catch {
if (isMounted) setProcessedSrc(src);
}
};

processImage();

return () => {
isMounted = false;
if (objectUrl) {
URL.revokeObjectURL(objectUrl);
}
};
}, [src]);

const handleLoad: ReactEventHandler<HTMLImageElement> = (evt) => {
evt.currentTarget.setAttribute('data-image-loaded', 'true');
setImage(evt.currentTarget);
};

const isBlobUrl = processedSrc.startsWith('blob:');

return (
<FoldsAvatarImage
className={css.RoomAvatar}
style={{ backgroundColor: useUniformIcons ? normalizedBg : undefined }}
src={src}
crossOrigin="anonymous"
src={processedSrc}
crossOrigin={isBlobUrl ? undefined : 'anonymous'}
alt={alt}
onError={() => {
setImage(undefined);
Expand Down
Loading